[agent] start rabbitmq accomplish the register procedure

This commit is contained in:
zeaslity
2022-12-01 17:25:03 +08:00
parent fbcb193a57
commit d89bc2947a
36 changed files with 841 additions and 213 deletions

View File

@@ -0,0 +1,48 @@
package io.wdd.agent.config.rabbitmq;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import org.springframework.context.annotation.Configuration;
/**
* response chain design pattern
*/
@Configuration
public abstract class AbstractOctopusMessageHandler {
private AbstractOctopusMessageHandler next;
public void addHandler(AbstractOctopusMessageHandler handler) {
this.next = handler;
}
public AbstractOctopusMessageHandler getNextHandler() {
return next;
}
public static class Builder {
private AbstractOctopusMessageHandler head;
private AbstractOctopusMessageHandler tail;
public Builder addHandler(AbstractOctopusMessageHandler nextHandler) {
if (this.head == null) {
this.head = this.tail = nextHandler;
return this;
}
this.tail.addHandler(nextHandler);
this.tail = nextHandler;
return this;
}
public AbstractOctopusMessageHandler build(){
return this.head;
}
}
public abstract boolean handle(OctopusMessage octopusMessage);
}

View File

@@ -0,0 +1,14 @@
package io.wdd.agent.config.rabbitmq;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessageType;
public class OMHandlerAgent extends AbstractOctopusMessageHandler {
@Override
public boolean handle(OctopusMessage octopusMessage) {
if (!octopusMessage.getType().equals(OctopusMessageType.AGENT)) {
this.getNextHandler().handle(octopusMessage);
}
return false;
}
}

View File

@@ -0,0 +1,17 @@
package io.wdd.agent.config.rabbitmq;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class OMHandlerBlackHole extends AbstractOctopusMessageHandler {
@Override
public boolean handle(OctopusMessage octopusMessage) {
log.error("Octopus Message Handle error ! No Handler Find !");
return false;
}
}

View File

@@ -0,0 +1,15 @@
package io.wdd.agent.config.rabbitmq;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessageType;
public class OMHandlerExecutor extends AbstractOctopusMessageHandler {
@Override
public boolean handle(OctopusMessage octopusMessage) {
if (!octopusMessage.getType().equals(OctopusMessageType.EXECUTOR)) {
this.getNextHandler().handle(octopusMessage);
}
return false;
}
}

View File

@@ -0,0 +1,22 @@
package io.wdd.agent.config.rabbitmq;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessageType;
public class OMHandlerInit extends AbstractOctopusMessageHandler {
@Override
public boolean handle(OctopusMessage octopusMessage) {
if (!octopusMessage.getType().equals(OctopusMessageType.INIT)) {
this.getNextHandler().handle(octopusMessage);
}
// handle the init message
return false;
}
}

View File

@@ -0,0 +1,16 @@
package io.wdd.agent.config.rabbitmq;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessageType;
public class OMHandlerStatus extends AbstractOctopusMessageHandler {
@Override
public boolean handle(OctopusMessage octopusMessage) {
if (!octopusMessage.getType().equals(OctopusMessageType.STATUS)) {
this.getNextHandler().handle(octopusMessage);
}
return false;
}
}

View File

@@ -1,8 +0,0 @@
package io.wdd.agent.config.rabbitmq;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RuntimeMessageConfig {
}

View File

@@ -0,0 +1,35 @@
package io.wdd.agent.config.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.common.handler.MyRuntimeException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
//@Configuration
@Deprecated
public class BeanFactoryUtils implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Bean
public ObjectMapper getMapper() {
ObjectMapper objectMapper = (ObjectMapper) applicationContext.getBean("jacksonObjectMapper");
if (ObjectUtils.isEmpty(objectMapper)) {
throw new MyRuntimeException(" Collect server info error !");
}
return objectMapper;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

View File

@@ -0,0 +1,19 @@
package io.wdd.agent.config.utils;
import io.wdd.common.utils.OctopusObjectMapperConfig;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OctopusObjectMapper {
//注意该段代码并未覆盖SpringBoot自动装配的ObjectMapper对象而是加强其配置。
// use the common config of object mapper
@Bean
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
return OctopusObjectMapperConfig.common();
}
}

View File

@@ -4,16 +4,14 @@ package io.wdd.agent.initialization.beans;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder(toBuilder = true)
@Configuration
public class ServerInfo {
@Component
public class AgentServerInfo {
@Value("${serverName}")
private String serverName;

View File

@@ -155,7 +155,7 @@ check_sys() {
# 判断系统的包管理工具 apt, yum, or zypper
getPackageManageTool() {
if [[ -n $(command -v apt-get) ]]; then
if [[ -n $(command -v apt-getMapper) ]]; then
CMD_INSTALL="apt-get -y -qq install"
CMD_UPDATE="apt-get -qq update"
CMD_REMOVE="apt-get -y remove"
@@ -364,7 +364,7 @@ InstallDocker() {
fi
colorEcho ${BLUE} "正在执行更新操作!!"
apt-get update
apt-getMapper update
colorEcho ${GREEN} "----------更新完成----------"
FunctionSuccess
colorEcho ${BLUE} "可以安装的docker-ce的版本为"
@@ -923,7 +923,7 @@ generateSystemInfo() {
deployOctopusAgent() {
FunctionStart
# get the latest version of Octopus agent
# getMapper the latest version of Octopus agent
# poll the start up shell
echo "docker run -d \

View File

@@ -1,7 +1,7 @@
package io.wdd.agent.initialization.bootup;
import io.wdd.agent.initialization.beans.ServerInfo;
import io.wdd.agent.initialization.beans.AgentServerInfo;
import io.wdd.common.handler.MyRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
@@ -29,7 +29,9 @@ public class CollectSystemInfo implements ApplicationContextAware {
private ApplicationContext context;
@Resource
InitConfiguration initConfiguration;
OctopusAgentInitService octopusAgentInitService;
public AgentServerInfo agentServerInfo;
@Bean
@Lazy
@@ -88,16 +90,16 @@ public class CollectSystemInfo implements ApplicationContextAware {
log.info("Starting getInjectServerInfo");
ServerInfo serverInfo = (ServerInfo) context.getBean("serverInfo");
agentServerInfo = (AgentServerInfo) context.getBean("agentServerInfo");
if (ObjectUtils.isEmpty(serverInfo)) {
if (ObjectUtils.isEmpty(agentServerInfo)) {
throw new MyRuntimeException(" Collect server info error !");
}
log.info("host server info has been collected == {}", serverInfo);
log.info("host server info has been collected == {}", agentServerInfo);
// start to send message to Octopus Server
initConfiguration.SendInfoToServer(serverInfo);
octopusAgentInitService.SendInfoToServer(agentServerInfo);
log.info("init server info has been send to octopus server !");
}

View File

@@ -1,94 +0,0 @@
package io.wdd.agent.initialization.bootup;
import io.wdd.agent.initialization.beans.ServerInfo;
import io.wdd.agent.initialization.rabbitmq.InitialRabbitMqConnector;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
@Service
@Lazy
public class InitConfiguration {
@Resource
RabbitTemplate rabbitTemplate;
@Resource
InitialRabbitMqConnector initialRabbitMqConnector;
@Value("${octopus.message.init_ttl}")
String defaultInitRegisterTimeOut;
private class InitMessagePostProcessor implements MessagePostProcessor {
private String initMessageTTL;
public InitMessagePostProcessor(Long initMessageTTL) {
this.initMessageTTL = String.valueOf(initMessageTTL);
}
public InitMessagePostProcessor(String initMessageTTL) {
this.initMessageTTL = initMessageTTL;
}
@Override
public Message postProcessMessage(Message message) throws AmqpException {
// set init register expiration time
MessageProperties messageProperties = message.getMessageProperties();
messageProperties.setExpiration(initMessageTTL);
return message;
}
}
public void SendInfoToServer(ServerInfo serverInfo){
// set init agent register ttl
InitMessagePostProcessor initMessagePostProcessor = new InitMessagePostProcessor(defaultInitRegisterTimeOut);
rabbitTemplate.convertAndSend("hello wmm !");
// send the register server info to EXCHANGE:INIT_EXCHANGE QUEUE: init_to_server
rabbitTemplate.convertAndSend(initialRabbitMqConnector.INIT_EXCHANGE, initialRabbitMqConnector.INIT_TO_SERVER_KEY, String.valueOf(serverInfo).getBytes(StandardCharsets.UTF_8), initMessagePostProcessor);
}
/**
* listen to the init queue from octopus server
*
* @RabbitListener : 用于标记当前方法是一个RabbitMQ的消息监听方法可以持续性的自动接收消息
* @param message
* 该方法不需要手动调用Spring会自动运行这个监听方法
*
* 注意如果该监听方法正常结束那么Spring会自动确认消息
* 如果出现异常则Spring不会确认消息该消息一直存在于消息队列中
*/
@RabbitHandler
@RabbitListener(
bindings =
@QueueBinding(
value = @Queue(name = "${octopus.message.init_from_server}" ),
exchange = @Exchange(name = "${octopus.message.init_exchange}", type = "direct"),
key = {"${octopus.message.init_from_server_key}"}
)
,
ackMode = "MANUAL"
)
public void ReceiveInitInfoFromServer(String message){
System.out.println("message = " + message);
}
}

View File

@@ -0,0 +1,133 @@
package io.wdd.agent.initialization.bootup;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import io.wdd.agent.initialization.beans.AgentServerInfo;
import io.wdd.agent.initialization.rabbitmq.InitialRabbitMqConnector;
import io.wdd.agent.message.handler.OctopusMessageHandler;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.handler.MyRuntimeException;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
@Lazy
@Slf4j
public class OctopusAgentInitService {
@Resource
RabbitTemplate rabbitTemplate;
@Resource
InitialRabbitMqConnector initialRabbitMqConnector;
@Autowired
ObjectMapper objectMapper;
@Autowired
OctopusMessageHandler octopusMessageHandler;
@Value("${octopus.message.init_ttl}")
String defaultInitRegisterTimeOut;
@SneakyThrows
public void SendInfoToServer(AgentServerInfo agentServerInfo) {
// set init agent register ttl
InitMessagePostProcessor initMessagePostProcessor = new InitMessagePostProcessor(defaultInitRegisterTimeOut);
log.info("send INIT AgentServerInfo to Server = {}", agentServerInfo);
// send the register server info to EXCHANGE:INIT_EXCHANGE QUEUE: init_to_server
rabbitTemplate.convertAndSend(initialRabbitMqConnector.INIT_EXCHANGE, initialRabbitMqConnector.INIT_TO_SERVER_KEY, objectMapper.writeValueAsString(agentServerInfo), initMessagePostProcessor);
}
/**
* listen to the init queue from octopus server
*
* @param message 该方法不需要手动调用Spring会自动运行这个监听方法
* <p>
* 注意如果该监听方法正常结束那么Spring会自动确认消息
* 如果出现异常则Spring不会确认消息该消息一直存在于消息队列中
* @RabbitListener : 用于标记当前方法是一个RabbitMQ的消息监听方法可以持续性的自动接收消息
*/
@SneakyThrows
@RabbitHandler
@RabbitListener(
bindings =
@QueueBinding(
value = @Queue(name = "${octopus.message.init_from_server}"),
exchange = @Exchange(name = "${octopus.message.init_exchange}", type = "direct"),
key = {"${octopus.message.init_from_server_key}"}
)
,
ackMode = "MANUAL"
)
public void ReceiveInitInfoFromServer(Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
try {
OctopusMessage octopusMessage = objectMapper.readValue(message.getBody(), OctopusMessage.class);
// response chain to handle all kind of type of octopus message
if (!octopusMessageHandler.handle(octopusMessage)) {
throw new MyRuntimeException(" Handle Octopus Message Error !");
}
} catch (Exception e) {
// reject the message
channel.basicNack(deliveryTag, false, true);
// long deliveryTag, boolean requeue
// channel.basicReject(deliveryTag,true);
Thread.sleep(1000); // 这里只是便于出现死循环时查看
throw new MyRuntimeException("Octopus Agent Initialization Error, please check !");
}
// ack the info
channel.basicAck(deliveryTag, false);
}
private class InitMessagePostProcessor implements MessagePostProcessor {
private final String initMessageTTL;
public InitMessagePostProcessor(Long initMessageTTL) {
this.initMessageTTL = String.valueOf(initMessageTTL);
}
public InitMessagePostProcessor(String initMessageTTL) {
this.initMessageTTL = initMessageTTL;
}
@Override
public Message postProcessMessage(Message message) throws AmqpException {
// set init register expiration time
MessageProperties messageProperties = message.getMessageProperties();
messageProperties.setExpiration(initMessageTTL);
return message;
}
}
}

View File

@@ -115,7 +115,7 @@ check_sys() {
# 判断系统的包管理工具 apt, yum, or zypper
getPackageManageTool() {
if [[ -n $(command -v apt-get) ]]; then
if [[ -n $(command -v apt-getMapper) ]]; then
CMD_INSTALL="apt-get -y -qq install"
CMD_UPDATE="apt-get -qq update"
CMD_REMOVE="apt-get -y remove"
@@ -350,7 +350,7 @@ InstallDocker() {
fi
colorEcho ${BLUE} "正在执行更新操作!!"
apt-get update
apt-getMapper update
colorEcho ${GREEN} "----------更新完成----------"
FunctionSuccess
colorEcho ${BLUE} "可以安装的docker-ce的19.03版本为:"
@@ -432,7 +432,7 @@ EOF
colorEcho ${GREEN} "----------添加完成----------"
colorEcho ${BLUE} "开始添加国内的阿里云源的kubernetes的apt源……"
colorEcho ${BLUE} "开始执行apt update 操作……"
apt-get update
apt-getMapper update
colorEcho ${GREEN} "--------------------------------------------------------------"
fi
echo ""

View File

@@ -0,0 +1,38 @@
package io.wdd.agent.initialization.webtest;
import io.wdd.agent.initialization.beans.AgentServerInfo;
import io.wdd.agent.initialization.bootup.CollectSystemInfo;
import io.wdd.agent.initialization.bootup.OctopusAgentInitService;
import io.wdd.common.beans.response.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("sendInfoToOctopusServer")
public class SendServerInfoController {
@Resource
CollectSystemInfo collectSystemInfo;
@Resource
OctopusAgentInitService octopusAgentInitService;
@PostMapping("sendAgentInfo")
public R<AgentServerInfo> send(){
AgentServerInfo agentServerInfo = collectSystemInfo.agentServerInfo;
octopusAgentInitService.SendInfoToServer(agentServerInfo);
return R.ok(agentServerInfo);
}
}

View File

@@ -0,0 +1,37 @@
package io.wdd.agent.message.handler;
import io.wdd.agent.config.rabbitmq.*;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@Service
public class OctopusMessageHandler {
private AbstractOctopusMessageHandler octopusMessageHandler;
@PostConstruct
private void registerAllHandler() {
AbstractOctopusMessageHandler.Builder builder = new AbstractOctopusMessageHandler.Builder();
octopusMessageHandler = builder
.addHandler(new OMHandlerExecutor())
.addHandler(new OMHandlerAgent())
.addHandler(new OMHandlerStatus())
.addHandler(new OMHandlerInit())
.build();
}
public boolean handle(OctopusMessage octopusMessage) {
return this.octopusMessageHandler.handle(octopusMessage);
}
}

View File

@@ -22,4 +22,4 @@ octopus:
# agent boot up default common exchange routing key
init_from_server_key: InitFromServerKey
# initialization register time out (unit ms) default is 5 min
init_ttl: "300000"
init_ttl: "3000000"

View File

@@ -1,6 +1,6 @@
package io.wdd.agent;
import io.wdd.agent.initialization.bootup.InitConfiguration;
import io.wdd.agent.initialization.bootup.OctopusAgentInitService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@@ -10,11 +10,10 @@ import javax.annotation.Resource;
public class InitRabbitMQTest {
@Resource
InitConfiguration initConfiguration;
OctopusAgentInitService octopusAgentInitService;
@Test
void testInitSendInfo(){
initConfiguration.SendInfoToServer();
}
}

View File

@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.handler.MyRuntimeException;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
@@ -11,11 +12,13 @@ import java.io.IOException;
@Component
public class MessageUtils {
@Autowired
ObjectMapper objectMapper;
public static OctopusMessage convert(Message message) {
ObjectMapper objectMapper = new ObjectMapper();
OctopusMessage octopusMessage;
ObjectMapper objectMapper = new ObjectMapper();
try {
octopusMessage = objectMapper.readValue(message.getBody(), OctopusMessage.class);

View File

@@ -0,0 +1,40 @@
package io.wdd.common.utils;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class OctopusObjectMapperConfig {
public static Jackson2ObjectMapperBuilderCustomizer common() {
return jacksonObjectMapperBuilder -> {
//若POJO对象的属性值为null序列化时不进行显示
//jacksonObjectMapperBuilder.serializationInclusion(JsonInclude.Include.NON_NULL);
//针对于Date类型文本格式化
jacksonObjectMapperBuilder.simpleDateFormat("yyyy-MM-dd");
//
jacksonObjectMapperBuilder.failOnEmptyBeans(false);
jacksonObjectMapperBuilder.failOnUnknownProperties(false);
jacksonObjectMapperBuilder.autoDetectFields(true);
//针对于JDK新时间类。序列化时带有T的问题自定义格式化字符串
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
jacksonObjectMapperBuilder.modules(javaTimeModule);
};
}
}

View File

@@ -13,7 +13,7 @@ import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
@Configuration
@Deprecated
public class OctopusRabbitTemplateConfig {
@Resource

View File

@@ -1,4 +1,4 @@
package io.wdd.server;
package io.wdd;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;

View File

@@ -1,35 +1,70 @@
package io.wdd.rpc.init;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
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.common.utils.MessageUtils;
import io.wdd.server.beans.po.ServerInfoPO;
import io.wdd.rpc.message.ToAgentOrder;
import io.wdd.server.beans.vo.ServerInfoVO;
import io.wdd.server.utils.DaemonDatabaseOperator;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
/**
* The type Accept boot up info message.
*/
@Service
@Slf4j(topic = "octopus agent init ")
public class AcceptBootUpInfoMessage {
public static Set<String> ALL_SERVER_CITY_INFO = new HashSet<>(
Arrays.asList(
"HongKong", "Tokyo", "Seoul", "Phoenix", "London", "Shanghai", "Chengdu"
)
);
public static Set<String> ALL_SERVER_ARCH_INFO = new HashSet<>(
Arrays.asList(
"amd64", "arm64", "arm32", "xia32", "miples"
)
);
/**
* The Database operator.
*/
@Resource
DaemonDatabaseOperator databaseOperator;
@Resource
ObjectMapper objectMapper;
/**
* The To agent order.
*/
@Resource
ToAgentOrder toAgentOrder;
/**
* Handle octopus agent boot up info.
*
* @param message the message
*/
@SneakyThrows
@RabbitHandler
@RabbitListener(
bindings =
@@ -41,41 +76,138 @@ public class AcceptBootUpInfoMessage {
,
ackMode = "MANUAL"
)
public void handleOctopusAgentBootUpInfo(Message message) {
public void handleOctopusAgentBootUpInfo(Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
// manual ack the rabbit message
// https://stackoverflow.com/questions/38728668/spring-rabbitmq-using-manual-channel-acknowledgement-on-a-service-with-rabbit
JsonMapper jsonMapper = new JsonMapper();
ServerInfoVO serverInfoVO;
try {
serverInfoVO = jsonMapper.readValue(message.getBody(), ServerInfoVO.class);
serverInfoVO = objectMapper.readValue(message.getBody(), ServerInfoVO.class);
// 1. check if information is correct
if (!validateServerInfo(serverInfoVO)) {
throw new MyRuntimeException("server info validated failed !");
}
// 2. generate the unique topic for agent
String agentQueueTopic = generateAgentQueueTopic(serverInfoVO);
serverInfoVO.setTopicName(agentQueueTopic);
// cache enabled for agent re-register
if (!checkAgentAlreadyRegister(agentQueueTopic)) {
// 3. save the agent info into database
// backend fixed thread daemon to operate the database ensuring the operation is correct !
if (!databaseOperator.saveInitOctopusAgentInfo(serverInfoVO)) {
throw new MyRuntimeException("database save agent info error !");
}
}
// 4. send InitMessage to agent
sendInitMessageToAgent(serverInfoVO);
} catch (IOException e) {
throw new MyRuntimeException("parse rabbit server info error, please check !");
/**
* 有异常就绝收消息
* basicNack(long deliveryTag, boolean multiple, boolean requeue)
* requeue:true为将消息重返当前消息队列,还可以重新发送给消费者;
* false:将消息丢弃
*/
// long deliveryTag, boolean multiple, boolean requeue
channel.basicNack(deliveryTag, false, true);
// long deliveryTag, boolean requeue
// channel.basicReject(deliveryTag,true);
Thread.sleep(1000); // 这里只是便于出现死循环时查看
/*
* 一般实际异常情况下的处理过程:记录出现异常的业务数据,将它单独插入到一个单独的模块,
* 然后尝试3次如果还是处理失败的话就进行人工介入处理
*/
throw new MyRuntimeException(" Octopus Server Initialization Error, please check !");
}
// 1. check if information is correct
if(!validateServerInfo(serverInfoVO)){
throw new MyRuntimeException("server info validated failed !");
};
// 2. generate the unique topic for agent
String agentQueueTopic = generateAgentQueueTopic(serverInfoVO);
// 3. save the agent info into database
// backend fixed thread daemon to operate the database ensuring the operation is correct !
if(!databaseOperator.saveInitOctopusAgentInfo(serverInfoVO)){
throw new MyRuntimeException("database save agent info error !");
}
// 4. send InitMessage to agent
/**
* 无异常就确认消息
* basicAck(long deliveryTag, boolean multiple)
* deliveryTag:取出来当前消息在队列中的的索引;
* multiple:为true的话就是批量确认,如果当前deliveryTag为5,那么就会确认
* deliveryTag为5及其以下的消息;一般设置为false
*/
// ack the rabbitmq info
// If all logic is successful
channel.basicAck(deliveryTag, false);
}
private boolean checkAgentAlreadyRegister(String agentQueueTopic) {
Optional<String> first = databaseOperator.getAllServerName().stream().
filter(serverName -> agentQueueTopic.startsWith(serverName))
.findFirst();
return first.isEmpty();
}
private boolean sendInitMessageToAgent(ServerInfoVO serverInfoVO) {
OctopusMessage octopusMessage = OctopusMessage.builder()
.type(OctopusMessageType.INIT)
.content(serverInfoVO.getTopicName())
.init_time(LocalDateTime.now())
.uuid(serverInfoVO.getTopicName())
.build();
toAgentOrder.send(octopusMessage);
return true;
}
/**
* Generate Octopus Agent Server Communicate Unique Topic
* <p>
* Strategy:
* 1. total length 28 bytes( 28 english letters max)
* 2. hostname -- machine_id
* city-arch-num-machine_id(prefix 6 bytes)
* 12 1 5 1 2 1 6 == 28
* NewYork-amd64-01-53df13
* Seoul-arm64-01-9sdd45
*
* @param serverInfoVO server info
* @return
*/
private String generateAgentQueueTopic(ServerInfoVO serverInfoVO) {
return null;
// topic generate strategy
String serverName = serverInfoVO.getServerName();
serverName.replace(" ", "");
serverInfoVO.setServerName(serverName);
// validate serverName
String[] split = serverName.split("-");
if (split.length <= 2 || !ALL_SERVER_CITY_INFO.contains(split[0]) || !ALL_SERVER_ARCH_INFO.contains(split[1])) {
throw new MyRuntimeException(" server name not validated !");
}
String machineIdPrefixSixBytes = String.valueOf(serverInfoVO.getMachineId().toCharArray(), 0, 6);
return serverName + "-" + machineIdPrefixSixBytes;
}
private boolean validateServerInfo(ServerInfoVO serverInfoVO) {
return false;
log.info("server info validated success !");
return true;
}

View File

@@ -7,9 +7,8 @@ import io.wdd.common.beans.rabbitmq.OctopusMessageType;
import io.wdd.common.handler.MyRuntimeException;
import io.wdd.rpc.init.FromServerMessageBinding;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.amqp.RabbitHealthIndicator;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@@ -19,6 +18,7 @@ import javax.annotation.Resource;
* provide override method to convert Object and send to rabbitmq
*/
@Component
@Slf4j(topic = "order to agent ")
public class ToAgentOrder {
@Resource
@@ -27,6 +27,10 @@ public class ToAgentOrder {
@Resource
FromServerMessageBinding fromServerMessageBinding;
@Resource
ObjectMapper objectMapper;
/**
*
* send to Queue -- InitFromServer
@@ -41,6 +45,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));
@@ -48,7 +53,7 @@ public class ToAgentOrder {
@SneakyThrows
private byte[] writeData(Object data){
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsBytes(data);
}

View File

@@ -1,11 +1,12 @@
package io.wdd.server.beans.po;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
/**
@@ -50,28 +51,27 @@ public class ServerInfoPO implements Serializable {
*/
private String serverIpInV6;
/**
*
*/
private Date registerTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime registerTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime expireTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
/**
*
*/
private Date expireTime;
/**
*
*/
private Date updateTime;
/**
*
* server location , type City Country
*/
private String location;
/**
*
* server isp manager
*/
private String provider;
@@ -83,12 +83,42 @@ public class ServerInfoPO implements Serializable {
/**
*
*/
private Integer cpuCore;
private String cpuBrand;
/**
*
*/
private String cpuBrand;
private String cpuCore;
/**
*
*/
private String memoryTotal;
/**
*
*/
private String diskTotal;
/**
*
*/
private String diskUsage;
/**
*
*/
private String ioSpeed;
/**
*
*/
private String tcpControl;
/**
* server virtualization method
*/
private String virtualization;
/**
*
@@ -100,6 +130,16 @@ public class ServerInfoPO implements Serializable {
*/
private String osKernelInfo;
/**
* machine uuid from /etc/machineid
*/
private String machineId;
/**
* octopus message unique key name
*/
private String topicName;
/**
*
*/
@@ -139,13 +179,22 @@ public class ServerInfoPO implements Serializable {
&& (this.getRegisterTime() == null ? other.getRegisterTime() == null : this.getRegisterTime().equals(other.getRegisterTime()))
&& (this.getExpireTime() == null ? other.getExpireTime() == null : this.getExpireTime().equals(other.getExpireTime()))
&& (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()))
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
&& (this.getLocation() == null ? other.getLocation() == null : this.getLocation().equals(other.getLocation()))
&& (this.getProvider() == null ? other.getProvider() == null : this.getProvider().equals(other.getProvider()))
&& (this.getManagePort() == null ? other.getManagePort() == null : this.getManagePort().equals(other.getManagePort()))
&& (this.getCpuCore() == null ? other.getCpuCore() == null : this.getCpuCore().equals(other.getCpuCore()))
&& (this.getCpuBrand() == null ? other.getCpuBrand() == null : this.getCpuBrand().equals(other.getCpuBrand()))
&& (this.getCpuCore() == null ? other.getCpuCore() == null : this.getCpuCore().equals(other.getCpuCore()))
&& (this.getMemoryTotal() == null ? other.getMemoryTotal() == null : this.getMemoryTotal().equals(other.getMemoryTotal()))
&& (this.getDiskTotal() == null ? other.getDiskTotal() == null : this.getDiskTotal().equals(other.getDiskTotal()))
&& (this.getDiskUsage() == null ? other.getDiskUsage() == null : this.getDiskUsage().equals(other.getDiskUsage()))
&& (this.getIoSpeed() == null ? other.getIoSpeed() == null : this.getIoSpeed().equals(other.getIoSpeed()))
&& (this.getTcpControl() == null ? other.getTcpControl() == null : this.getTcpControl().equals(other.getTcpControl()))
&& (this.getVirtualization() == null ? other.getVirtualization() == null : this.getVirtualization().equals(other.getVirtualization()))
&& (this.getOsInfo() == null ? other.getOsInfo() == null : this.getOsInfo().equals(other.getOsInfo()))
&& (this.getOsKernelInfo() == null ? other.getOsKernelInfo() == null : this.getOsKernelInfo().equals(other.getOsKernelInfo()))
&& (this.getMachineId() == null ? other.getMachineId() == null : this.getMachineId().equals(other.getMachineId()))
&& (this.getTopicName() == null ? other.getTopicName() == null : this.getTopicName().equals(other.getTopicName()))
&& (this.getComment() == null ? other.getComment() == null : this.getComment().equals(other.getComment()))
&& (this.getIsDelete() == null ? other.getIsDelete() == null : this.getIsDelete().equals(other.getIsDelete()))
&& (this.getVersion() == null ? other.getVersion() == null : this.getVersion().equals(other.getVersion()));
@@ -164,13 +213,22 @@ public class ServerInfoPO implements Serializable {
result = prime * result + ((getRegisterTime() == null) ? 0 : getRegisterTime().hashCode());
result = prime * result + ((getExpireTime() == null) ? 0 : getExpireTime().hashCode());
result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
result = prime * result + ((getLocation() == null) ? 0 : getLocation().hashCode());
result = prime * result + ((getProvider() == null) ? 0 : getProvider().hashCode());
result = prime * result + ((getManagePort() == null) ? 0 : getManagePort().hashCode());
result = prime * result + ((getCpuCore() == null) ? 0 : getCpuCore().hashCode());
result = prime * result + ((getCpuBrand() == null) ? 0 : getCpuBrand().hashCode());
result = prime * result + ((getCpuCore() == null) ? 0 : getCpuCore().hashCode());
result = prime * result + ((getMemoryTotal() == null) ? 0 : getMemoryTotal().hashCode());
result = prime * result + ((getDiskTotal() == null) ? 0 : getDiskTotal().hashCode());
result = prime * result + ((getDiskUsage() == null) ? 0 : getDiskUsage().hashCode());
result = prime * result + ((getIoSpeed() == null) ? 0 : getIoSpeed().hashCode());
result = prime * result + ((getTcpControl() == null) ? 0 : getTcpControl().hashCode());
result = prime * result + ((getVirtualization() == null) ? 0 : getVirtualization().hashCode());
result = prime * result + ((getOsInfo() == null) ? 0 : getOsInfo().hashCode());
result = prime * result + ((getOsKernelInfo() == null) ? 0 : getOsKernelInfo().hashCode());
result = prime * result + ((getMachineId() == null) ? 0 : getMachineId().hashCode());
result = prime * result + ((getTopicName() == null) ? 0 : getTopicName().hashCode());
result = prime * result + ((getComment() == null) ? 0 : getComment().hashCode());
result = prime * result + ((getIsDelete() == null) ? 0 : getIsDelete().hashCode());
result = prime * result + ((getVersion() == null) ? 0 : getVersion().hashCode());
@@ -192,13 +250,22 @@ public class ServerInfoPO implements Serializable {
sb.append(", registerTime=").append(registerTime);
sb.append(", expireTime=").append(expireTime);
sb.append(", updateTime=").append(updateTime);
sb.append(", createTime=").append(createTime);
sb.append(", location=").append(location);
sb.append(", provider=").append(provider);
sb.append(", managePort=").append(managePort);
sb.append(", cpuCore=").append(cpuCore);
sb.append(", cpuBrand=").append(cpuBrand);
sb.append(", cpuCore=").append(cpuCore);
sb.append(", memoryTotal=").append(memoryTotal);
sb.append(", diskTotal=").append(diskTotal);
sb.append(", diskUsage=").append(diskUsage);
sb.append(", ioSpeed=").append(ioSpeed);
sb.append(", tcpControl=").append(tcpControl);
sb.append(", virtualization=").append(virtualization);
sb.append(", osInfo=").append(osInfo);
sb.append(", osKernelInfo=").append(osKernelInfo);
sb.append(", machineId=").append(machineId);
sb.append(", topicName=").append(topicName);
sb.append(", comment=").append(comment);
sb.append(", isDelete=").append(isDelete);
sb.append(", version=").append(version);

View File

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@@ -54,63 +55,97 @@ public class ServerInfoVO {
*
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime registerTime;
/**
*
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime expireTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
/**
*
* server location , type City Country
*/
private String location;
/**
*
* server isp manager
*/
private String provider;
/**
*
* split by ,
*/
@Nullable
private Integer managePort;
private String managePort;
/**
*
*/
private Integer cpuCore;
/**
*
*/
@Nullable
private String cpuBrand;
/**
*
*/
@Nullable
private String cpuCore;
/**
*
*/
private String memoryTotal;
/**
*
*/
private String diskTotal;
/**
*
*/
private String diskUsage;
/**
*
*/
private String ioSpeed;
/**
*
*/
private String tcpControl;
/**
* server virtualization method
*/
private String virtualization;
/**
*
*/
private String osInfo;
/**
*
*/
@Nullable
private String osKernelInfo;
/**
* machine uuid from /etc/machineid
*/
private String machineId;
/**
* octopus message unique key name
*/
private String topicName;
/**
*
*/
@Nullable
private String comment;
private Integer version;
}

View File

@@ -0,0 +1,19 @@
package io.wdd.server.config;
import io.wdd.common.utils.OctopusObjectMapperConfig;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OctopusObjectMapper {
//注意该段代码并未覆盖SpringBoot自动装配的ObjectMapper对象而是加强其配置。
// use the common config of object mapper
@Bean
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
return OctopusObjectMapperConfig.common();
}
}

View File

@@ -7,6 +7,7 @@ import io.wdd.server.beans.vo.DomainInfoVO;
import io.wdd.server.beans.vo.ServerInfoVO;
import java.util.List;
import java.util.Set;
public interface CoreServerService {
@@ -39,4 +40,5 @@ public interface CoreServerService {
boolean domainDelete(Long serverId, Long domainId);
}

View File

@@ -19,9 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
@@ -238,4 +236,6 @@ public class CoreServerServiceImpl implements CoreServerService {
public boolean domainDelete(Long serverId, Long domainId) {
return false;
}
}

View File

@@ -6,7 +6,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author wdd
* @description 针对表【server_info】的数据库操作Mapper
* @createDate 2022-11-27 13:46:54
* @createDate 2022-11-30 13:54:40
* @Entity io.wdd.server.beans.po.ServerInfoPO
*/
public interface ServerInfoMapper extends BaseMapper<ServerInfoPO> {

View File

@@ -6,7 +6,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author wdd
* @description 针对表【server_info】的数据库操作Service
* @createDate 2022-11-27 13:46:54
* @createDate 2022-11-30 13:54:40
*/
public interface ServerInfoService extends IService<ServerInfoPO> {

View File

@@ -9,7 +9,7 @@ import org.springframework.stereotype.Service;
/**
* @author wdd
* @description 针对表【server_info】的数据库操作Service实现
* @createDate 2022-11-27 13:46:54
* @createDate 2022-11-30 13:54:40
*/
@Service
public class ServerInfoServiceImpl extends ServiceImpl<ServerInfoMapper, ServerInfoPO>

View File

@@ -3,18 +3,22 @@ package io.wdd.server.utils;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.wdd.server.beans.vo.ServerInfoVO;
import io.wdd.server.coreService.CoreServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.stream.Collectors;
/**
* The type Daemon database operator.
*/
@Component
@Slf4j(topic = "Daemon Database Operator")
public class DaemonDatabaseOperator {
/**
@@ -35,7 +39,16 @@ public class DaemonDatabaseOperator {
*/
public boolean saveInitOctopusAgentInfo(ServerInfoVO serverInfoVO) {
return coreServerService.serverCreate(serverInfoVO);
log.info("simulate store the Octopus Agent Server info");
return true;
// return coreServerService.serverCreate(serverInfoVO);
}
public Set<String> getAllServerName(){
return coreServerService.serverGetAll().stream().map(serverInfoVO -> serverInfoVO.getServerName()).collect(Collectors.toSet());
}
@PostConstruct

View File

@@ -23,6 +23,15 @@ spring:
username: boge
password: boge14@Level5
virtual-host: /wddserver
listener:
simple:
retry:
# ack failed will reentrant the Rabbit Listener
max-attempts: 5
enabled: true
# retry interval unit ms
max-interval: 5000
initial-interval: 5000
redis:

View File

@@ -14,13 +14,22 @@
<result property="registerTime" column="register_time" jdbcType="TIMESTAMP"/>
<result property="expireTime" column="expire_time" jdbcType="TIMESTAMP"/>
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
<result property="location" column="location" jdbcType="VARCHAR"/>
<result property="provider" column="provider" jdbcType="VARCHAR"/>
<result property="managePort" column="manage_port" jdbcType="VARCHAR"/>
<result property="cpuCore" column="cpu_core" jdbcType="INTEGER"/>
<result property="cpuBrand" column="cpu_brand" jdbcType="VARCHAR"/>
<result property="cpuCore" column="cpu_core" jdbcType="VARCHAR"/>
<result property="memoryTotal" column="memory_total" jdbcType="VARCHAR"/>
<result property="diskTotal" column="disk_total" jdbcType="VARCHAR"/>
<result property="diskUsage" column="disk_usage" jdbcType="VARCHAR"/>
<result property="ioSpeed" column="io_speed" jdbcType="VARCHAR"/>
<result property="tcpControl" column="tcp_control" jdbcType="VARCHAR"/>
<result property="virtualization" column="virtualization" jdbcType="VARCHAR"/>
<result property="osInfo" column="os_info" jdbcType="VARCHAR"/>
<result property="osKernelInfo" column="os_kernel_info" jdbcType="VARCHAR"/>
<result property="machineId" column="machine_id" jdbcType="VARCHAR"/>
<result property="topicName" column="topic_name" jdbcType="VARCHAR"/>
<result property="comment" column="comment" jdbcType="VARCHAR"/>
<result property="isDelete" column="is_delete" jdbcType="TINYINT"/>
<result property="version" column="version" jdbcType="INTEGER"/>
@@ -30,9 +39,12 @@
server_id,server_name,server_ip_pb_v4,
server_ip_in_v4,server_ip_pb_v6,server_ip_in_v6,
register_time,expire_time,update_time,
location,provider,manage_port,
cpu_core,cpu_brand,os_info,
os_kernel_info,comment,is_delete,
create_time,location,provider,
manage_port,cpu_brand,cpu_core,
memory_total,disk_total,disk_usage,
io_speed,tcp_control,virtualization,
os_info,os_kernel_info,machine_id,
topic_name,comment,is_delete,
version
</sql>
</mapper>