[server-agent] accomplish the register procedure

This commit is contained in:
zeaslity
2022-12-03 20:29:00 +08:00
parent d89bc2947a
commit ae11bc28b4
24 changed files with 506 additions and 109 deletions

View File

@@ -6,7 +6,7 @@ import com.rabbitmq.client.Channel;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessageType;
import io.wdd.common.handler.MyRuntimeException;
import io.wdd.rpc.message.ToAgentOrder;
import io.wdd.rpc.message.sender.ToAgentOrder;
import io.wdd.server.beans.vo.ServerInfoVO;
import io.wdd.server.utils.DaemonDatabaseOperator;
import lombok.SneakyThrows;
@@ -24,13 +24,17 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* The type Accept boot up info message.
*/
@Service
@Slf4j(topic = "octopus agent init ")
public class AcceptBootUpInfoMessage {
public class AcceptAgentInitInfo {
@Resource
InitRabbitMQConfig initRabbitMQConfig;
public static Set<String> ALL_SERVER_CITY_INFO = new HashSet<>(
@@ -125,7 +129,8 @@ public class AcceptBootUpInfoMessage {
// long deliveryTag, boolean requeue
// channel.basicReject(deliveryTag,true);
Thread.sleep(1000); // 这里只是便于出现死循环时查看
// 这里只是便于出现死循环时查看
TimeUnit.SECONDS.sleep(5);
/*
* 一般实际异常情况下的处理过程记录出现异常的业务数据将它单独插入到一个单独的模块
@@ -137,6 +142,7 @@ public class AcceptBootUpInfoMessage {
}
/**
* 无异常就确认消息
* 无异常就确认消息
* basicAck(long deliveryTag, boolean multiple)
* deliveryTag:取出来当前消息在队列中的的索引;
@@ -145,6 +151,7 @@ public class AcceptBootUpInfoMessage {
*/
// ack the rabbitmq info
// If all logic is successful
log.info("Agent [ {} ] has init successfully !", serverInfoVO.getTopicName());
channel.basicAck(deliveryTag, false);
}
@@ -154,14 +161,15 @@ public class AcceptBootUpInfoMessage {
filter(serverName -> agentQueueTopic.startsWith(serverName))
.findFirst();
return first.isEmpty();
return first.isPresent();
}
private boolean sendInitMessageToAgent(ServerInfoVO serverInfoVO) {
OctopusMessage octopusMessage = OctopusMessage.builder()
.type(OctopusMessageType.INIT)
.content(serverInfoVO.getTopicName())
// should be the OctopusExchange Name
.content(String.valueOf(initRabbitMQConfig.OCTOPUS_EXCHANGE))
.init_time(LocalDateTime.now())
.uuid(serverInfoVO.getTopicName())
.build();

View File

@@ -9,7 +9,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FromServerMessageBinding {
public class InitRabbitMQConfig {
@Value("${octopus.message.init_exchange}")
public String INIT_EXCHANGE;
@@ -26,6 +26,13 @@ public class FromServerMessageBinding {
@Value("${octopus.message.init_to_server_key}")
public String INIT_TO_SERVER_KEY;
@Value("${octopus.message.octopus_exchange}")
public String OCTOPUS_EXCHANGE;
@Value("${octopus.message.octopus_to_server}")
public String OCTOPUS_TO_SERVER;
@Bean
public DirectExchange initDirectExchange() {
return new DirectExchange(INIT_EXCHANGE);
@@ -40,7 +47,7 @@ public class FromServerMessageBinding {
/**
* 配置一个队列和交换机的绑定
*
* @param initDirectQueue : 需要绑定的队列对象参数名必须和某个@Bean的方法名完全相同这样就会进行自动注入对应 .bind()
* @param initFromServerQueue : 需要绑定的队列对象参数名必须和某个@Bean的方法名完全相同这样就会进行自动注入对应 .bind()
* @param initDirectExchange : 需要绑定的交换机对象参数名必须和某个@Bean的方法名完全相同这样就会进行自动注入对应 .to()
* .with() 方法对应的RoutingKey
* @return

View File

@@ -0,0 +1,43 @@
package io.wdd.rpc.init;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* generate the OCTOPUS_EXCHANGE at the beginning
*/
@Configuration
public class OctopusExchangeConfig {
@Value("${octopus.message.octopus_exchange}")
public String OCTOPUS_EXCHANGE;
@Value("${octopus.message.octopus_to_server}")
public String OCTOPUS_TO_SERVER;
@Bean
public TopicExchange octopusExchange(){
return new TopicExchange(OCTOPUS_EXCHANGE,true,false);
}
@Bean
public Queue octopusAgentToServerQueue(){
return new Queue(OCTOPUS_TO_SERVER);
}
@Bean
public Binding bindingToServerTopicQueue(TopicExchange octopusExchange, Queue octopusAgentToServerQueue){
return BindingBuilder
.bind(octopusAgentToServerQueue)
.to(octopusExchange)
.with(OCTOPUS_TO_SERVER);
}
}