[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);
}
}

View File

@@ -0,0 +1,47 @@
package io.wdd.rpc.message.handler;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.handler.MyRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.io.IOException;
@Configuration
@Slf4j(topic = "Octopus Message Handler")
public class OctopusMessageHandlerServer {
@Resource
ObjectMapper objectMapper;
@RabbitHandler
@RabbitListener(queues = "${octopus.message.octopus_to_server}"
)
public void HandleOctopusMessageFromAgent(Message message){
OctopusMessage octopusMessage;
try {
octopusMessage = objectMapper.readValue(message.getBody(), OctopusMessage.class);
} catch (IOException e) {
throw new MyRuntimeException("Octopus Message Wrong !");
}
// Octopus Message Handler
log.info("received from agent : {} ", octopusMessage);
// todo what to do after received the result
// log info ?
// update the database
// handle the result
}
}

View File

@@ -1,11 +1,11 @@
package io.wdd.rpc.message;
package io.wdd.rpc.message.sender;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessageType;
import io.wdd.common.handler.MyRuntimeException;
import io.wdd.rpc.init.FromServerMessageBinding;
import io.wdd.rpc.init.InitRabbitMQConfig;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
@@ -18,14 +18,14 @@ import javax.annotation.Resource;
* provide override method to convert Object and send to rabbitmq
*/
@Component
@Slf4j(topic = "order to agent ")
@Slf4j(topic = "To Octopus Server Message ")
public class ToAgentOrder {
@Resource
RabbitTemplate rabbitTemplate;
@Resource
FromServerMessageBinding fromServerMessageBinding;
InitRabbitMQConfig initRabbitMQConfig;
@Resource
@@ -47,7 +47,7 @@ public class ToAgentOrder {
// send to Queue -- InitFromServer
log.info("send INIT OrderCommand to Agent = {}", message);
rabbitTemplate.convertAndSend(fromServerMessageBinding.INIT_EXCHANGE, fromServerMessageBinding.INIT_FROM_SERVER_KEY, writeData(message));
rabbitTemplate.convertAndSend(initRabbitMQConfig.INIT_EXCHANGE, initRabbitMQConfig.INIT_FROM_SERVER_KEY, writeData(message));
}

View File

@@ -74,8 +74,8 @@ public class CoreServerServiceImpl implements CoreServerService {
@Override
public boolean serverCreate(ServerInfoVO serverInfoVO) {
ServerInfoPO serverInfoPO = new ServerInfoPO();
BeanUtils.copyProperties(serverInfoVO, serverInfoPO);
// BeanUtils.copyProperties(serverInfoVO, serverInfoPO);
ServerInfoPO serverInfoPO = EntityUtils.cvToTarget(serverInfoVO, ServerInfoPO.class);
return serverInfoService.save(serverInfoPO);
}

View File

@@ -20,8 +20,10 @@ public class MyBatisAutoInsertInterceptor implements MetaObjectHandler {
public void insertFill(MetaObject metaObject) {
log.info("MyBaitsPlus start to insert manually !");
this.strictInsertFill(metaObject, "registerTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
this.strictInsertFill(metaObject, "create_time", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
this.strictInsertFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
}
@Override
@@ -30,7 +32,5 @@ public class MyBatisAutoInsertInterceptor implements MetaObjectHandler {
log.info("MyBaitsPlus start to update manually !");
this.strictInsertFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
this.strictInsertFill(metaObject, "update_time", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
}
}

View File

@@ -39,10 +39,10 @@ public class DaemonDatabaseOperator {
*/
public boolean saveInitOctopusAgentInfo(ServerInfoVO serverInfoVO) {
log.info("simulate store the Octopus Agent Server info");
// log.info("simulate store the Octopus Agent Server info");
return true;
// return coreServerService.serverCreate(serverInfoVO);
// return true;
return coreServerService.serverCreate(serverInfoVO);
}

View File

@@ -15,6 +15,11 @@ octopus:
init_from_server_key: InitFromServerKey
# initialization register time out (unit ms) default is 5 min
init_ttl: "300000"
# Octopus Exchange Name == server comunicate with agent
octopus_exchange: OctopusExchange
# Octopus Message To Server == all agent send info to server queue and topic
octopus_to_server: OctopusToServer
spring:
rabbitmq: