[ Service ] [ Executor ] 修改OctopusObjectMapper

This commit is contained in:
zeaslity
2023-08-11 15:47:54 +08:00
parent 06170c1024
commit 0a78f9a02b
15 changed files with 175 additions and 152 deletions

View File

@@ -136,7 +136,7 @@ func executorOMHandler(octopusMessage *OctopusMessage) {
// send back the result log // send back the result log
octopusMessage.Result = resultLog octopusMessage.Result = resultLog
octopusMessage.ACTime = utils.ParseISOLocalDateTime() octopusMessage.ACTime = utils.ParseDateTimeTime()
// Send // Send
octopusMessage.SendToOctopusServer() octopusMessage.SendToOctopusServer()

View File

@@ -15,6 +15,7 @@ func ParseDateTimeTime() string {
return now.Format(time.DateTime) return now.Format(time.DateTime)
} }
// ParseISOLocalDateTime 时间格式为2023-08-11T10:48:15+08:00
func ParseISOLocalDateTime() string { func ParseISOLocalDateTime() string {
now := time.Now() now := time.Now()
return now.Format(time.RFC3339) return now.Format(time.RFC3339)

View File

@@ -0,0 +1,105 @@
package io.wdd.common.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
@Component
public class OctopusObjectMapperConfig {
private static final String STANDARD_PATTERN = "yyyy-MM-dd HH:mm:ss";
private static final String DATE_PATTERN = "yyyy-MM-dd";
private static final String TIME_PATTERN = "HH:mm:ss";
public static ObjectMapper OctopusObjectMapper = null;
@PostConstruct
public void setOctopusObjectMapper() {
OctopusObjectMapper = new ObjectMapper();
/*
* 1. java.util.Date yyyy-MM-dd HH:mm:ss
* 2. 支持JDK8 LocalDateTime、LocalDate、 LocalTime
* 3. Jdk8Module模块支持如Stream、Optional等类
* 4. 序列化时包含所有字段
* 5. 在序列化一个空对象时时不抛出异常
* 6. 忽略反序列化时在json字符串中存在, 但在java对象中不存在的属性
* 7. 数字序列化成字符穿且调用BigDecimal.toPlainString()方法
*/
OctopusObjectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 初始化JavaTimeModule
JavaTimeModule javaTimeModule = new JavaTimeModule();
//处理LocalDateTime
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern(STANDARD_PATTERN);
javaTimeModule.addSerializer(
LocalDateTime.class,
new LocalDateTimeSerializer(dateTimeFormatter)
);
javaTimeModule.addDeserializer(
LocalDateTime.class,
new LocalDateTimeDeserializer(dateTimeFormatter)
);
//处理LocalDate
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
javaTimeModule.addSerializer(
LocalDate.class,
new LocalDateSerializer(dateFormatter)
);
javaTimeModule.addDeserializer(
LocalDate.class,
new LocalDateDeserializer(dateFormatter)
);
//处理LocalTime
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(TIME_PATTERN);
javaTimeModule.addSerializer(
LocalTime.class,
new LocalTimeSerializer(timeFormatter)
);
javaTimeModule.addDeserializer(
LocalTime.class,
new LocalTimeDeserializer(timeFormatter)
);
OctopusObjectMapper.registerModule(javaTimeModule);
OctopusObjectMapper.setDateFormat(
new SimpleDateFormat(STANDARD_PATTERN)
);
OctopusObjectMapper.configure(
SerializationFeature.FAIL_ON_EMPTY_BEANS,
false
);
OctopusObjectMapper.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false
);
}
}

View File

@@ -1,54 +0,0 @@
package io.wdd.common.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
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 javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class OctopusObjectMapperConfig {
public static ObjectMapper OctopusObjectMapper = null;
@Resource
ObjectMapper objectMapper;
@PostConstruct
public void setOctopusObjectMapper() {
OctopusObjectMapper = objectMapper;
}
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

@@ -109,6 +109,8 @@ public class TimeUtils {
} }
/** /**
* yyyy-MM-dd HH:mm:ss
*
* @return 格式化 去掉时间中的毫秒数 * @return 格式化 去掉时间中的毫秒数
*/ */
public static LocalDateTime currentFormatTime() { public static LocalDateTime currentFormatTime() {

View File

@@ -12,6 +12,8 @@ import javax.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static io.wdd.rpc.message.handler.sync.OMessageHandler.StopWaitingResult;
import static io.wdd.rpc.message.handler.sync.OMessageHandler.WaitFromAgent;
import static io.wdd.rpc.status.CommonAndStatusCache.ALL_AGENT_TOPIC_NAME_SET; import static io.wdd.rpc.status.CommonAndStatusCache.ALL_AGENT_TOPIC_NAME_SET;
@Service @Service
@@ -51,18 +53,26 @@ public class ExecutionServiceImpl implements ExecutionService {
// send the message // send the message
oMessageToAgentSender.send(octopusMessage); oMessageToAgentSender.send(octopusMessage);
System.out.println("originOctopusMessage = " + octopusMessage.hashCode());
// 需要返回结果 // 需要返回结果
if (!durationTask) { if (!durationTask) {
// 等待结果
WaitFromAgent(octopusMessage);
synchronized (octopusMessage) { synchronized (octopusMessage) {
try { try {
octopusMessage.wait(10000); octopusMessage.wait(10000);
log.debug("等待结束!");
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}
}
// 转换结果 // 转换结果
commandResultLog = (ArrayList<String>) octopusMessage.getResult(); commandResultLog = (ArrayList<String>) octopusMessage.getResult();
@@ -76,6 +86,9 @@ public class ExecutionServiceImpl implements ExecutionService {
} }
// 释放等待队列
StopWaitingResult(octopusMessage);
return commandResultLog; return commandResultLog;
} }
@@ -85,7 +98,7 @@ public class ExecutionServiceImpl implements ExecutionService {
return OctopusMessage return OctopusMessage
.builder() .builder()
.octopusMessageType(OctopusMessageType.EXECUTOR) .octopusMessageType(OctopusMessageType.EXECUTOR)
.init_time(TimeUtils.currentFormatTime()) .init_time(TimeUtils.currentTime())
.uuid(agentTopicName) .uuid(agentTopicName)
.content( .content(
executionMessage executionMessage

View File

@@ -27,7 +27,7 @@ import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static io.wdd.common.utils.OctopusObjectMapperConfig.OctopusObjectMapper; import static io.wdd.common.config.OctopusObjectMapperConfig.OctopusObjectMapper;
/** /**
* The type Accept boot up info message. * The type Accept boot up info message.

View File

@@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import static io.wdd.rpc.message.handler.sync.OMessageToServerListener.FROM_AGENT_MATCH_TO_AGENT_MAP; import static io.wdd.rpc.message.handler.sync.OMessageToServerListener.FROM_AGENT_MATCH_TO_AGENT_MAP;
import static io.wdd.rpc.message.handler.sync.OMessageToServerListener.OCTOPUS_MESSAGE_FROM_AGENT; import static io.wdd.rpc.message.handler.sync.OMessageToServerListener.OCTOPUS_MESSAGE_FROM_AGENT;
@@ -43,6 +44,42 @@ public class OMessageHandler {
} }
public static void WaitFromAgent(OctopusMessage octopusMessage) {
// 构建 MatchKey
String matchKey = GenerateOMessageMatchKey(
octopusMessage.getOctopusMessageType(),
octopusMessage.getInit_time()
);
// 开始等待
FROM_AGENT_MATCH_TO_AGENT_MAP.put(
matchKey,
octopusMessage
);
//debug
log.info(
"wait from agent map is => {}",
FROM_AGENT_MATCH_TO_AGENT_MAP
);
}
public static void StopWaitingResult(OctopusMessage octopusMessage) {
// 构建 MatchKey
String matchKey = GenerateOMessageMatchKey(
octopusMessage.getOctopusMessageType(),
octopusMessage.getInit_time()
);
// 开始等待
if (FROM_AGENT_MATCH_TO_AGENT_MAP.containsKey(matchKey)) {
FROM_AGENT_MATCH_TO_AGENT_MAP.remove(matchKey);
}
}
/** /**
* 解析所有从Agent传回的消息中央集中化处理 * 解析所有从Agent传回的消息中央集中化处理
*/ */
@@ -54,7 +91,7 @@ public class OMessageHandler {
if (OCTOPUS_MESSAGE_FROM_AGENT.isEmpty()) { if (OCTOPUS_MESSAGE_FROM_AGENT.isEmpty()) {
try { try {
OCTOPUS_MESSAGE_FROM_AGENT.wait(5000); TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@@ -70,6 +107,7 @@ public class OMessageHandler {
replayOMessage.getOctopusMessageType(), replayOMessage.getOctopusMessageType(),
replayOMessage.getInit_time() replayOMessage.getInit_time()
); );
if (!FROM_AGENT_MATCH_TO_AGENT_MAP.containsKey(matchKey)) { if (!FROM_AGENT_MATCH_TO_AGENT_MAP.containsKey(matchKey)) {
// 没有这个Key,说明等待结果已经超时了,直接丢弃,然后继续循环 // 没有这个Key,说明等待结果已经超时了,直接丢弃,然后继续循环
// todo 错误的数据需要放置于某处 // todo 错误的数据需要放置于某处
@@ -93,32 +131,4 @@ public class OMessageHandler {
} }
} }
protected void waitFor(OctopusMessage octopusMessage) {
// 构建 MatchKey
String matchKey = GenerateOMessageMatchKey(
octopusMessage.getOctopusMessageType(),
octopusMessage.getInit_time()
);
// 开始等待
FROM_AGENT_MATCH_TO_AGENT_MAP.put(
matchKey,
octopusMessage
);
}
public void stopWaiting(OctopusMessage octopusMessage) {
// 构建 MatchKey
String matchKey = GenerateOMessageMatchKey(
octopusMessage.getOctopusMessageType(),
octopusMessage.getInit_time()
);
// 开始等待
FROM_AGENT_MATCH_TO_AGENT_MAP.remove(matchKey);
}
} }

View File

@@ -15,7 +15,7 @@ import java.io.IOException;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.HashMap; import java.util.HashMap;
import static io.wdd.common.utils.OctopusObjectMapperConfig.OctopusObjectMapper; import static io.wdd.common.config.OctopusObjectMapperConfig.OctopusObjectMapper;
@Configuration @Configuration
@Slf4j(topic = "Octopus Message Listener") @Slf4j(topic = "Octopus Message Listener")
@@ -72,6 +72,8 @@ public class OMessageToServerListener {
octopusMessage octopusMessage
); );
System.out.println("receivedOctopusMessage = " + octopusMessage.hashCode());
// 获取Agent的版本信息 // 获取Agent的版本信息
if (octopusMessage if (octopusMessage
.getUuid() .getUuid()
@@ -96,9 +98,5 @@ public class OMessageToServerListener {
// 将收到的消息,直接存储到 缓存队列中 // 将收到的消息,直接存储到 缓存队列中
log.debug("cache the octopus message to inner cache list !"); log.debug("cache the octopus message to inner cache list !");
OCTOPUS_MESSAGE_FROM_AGENT.offer(octopusMessage); OCTOPUS_MESSAGE_FROM_AGENT.offer(octopusMessage);
oMessageHandler.waitFor(octopusMessage);
// 唤醒等待线程
OCTOPUS_MESSAGE_FROM_AGENT.notify();
} }
} }

View File

@@ -2,7 +2,6 @@ package io.wdd.rpc.message.sender;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.common.handler.MyRuntimeException; import io.wdd.common.handler.MyRuntimeException;
import io.wdd.rpc.init.InitRabbitMQConfig; import io.wdd.rpc.init.InitRabbitMQConfig;
import io.wdd.rpc.message.OctopusMessage; import io.wdd.rpc.message.OctopusMessage;
@@ -15,7 +14,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import static io.wdd.common.utils.OctopusObjectMapperConfig.OctopusObjectMapper; import static io.wdd.common.config.OctopusObjectMapperConfig.OctopusObjectMapper;
/** /**
* adaptor * adaptor
@@ -31,8 +30,6 @@ public class OMessageToAgentSender {
@Resource @Resource
InitRabbitMQConfig initRabbitMQConfig; InitRabbitMQConfig initRabbitMQConfig;
@Resource
ObjectMapper objectMapper;
/** /**
* send to Queue -- InitFromServer * send to Queue -- InitFromServer
@@ -106,7 +103,7 @@ public class OMessageToAgentSender {
@SneakyThrows @SneakyThrows
private byte[] writeData(Object data) { private byte[] writeData(Object data) {
return objectMapper.writeValueAsBytes(data); return OctopusObjectMapper.writeValueAsBytes(data);
} }
} }

View File

@@ -12,7 +12,7 @@ import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Map; import java.util.Map;
import static io.wdd.common.utils.OctopusObjectMapperConfig.OctopusObjectMapper; import static io.wdd.common.config.OctopusObjectMapperConfig.OctopusObjectMapper;
import static io.wdd.rpc.status.CommonAndStatusCache.ALL_HEALTHY_AGENT_TOPIC_NAME_LIST; import static io.wdd.rpc.status.CommonAndStatusCache.ALL_HEALTHY_AGENT_TOPIC_NAME_LIST;
/** /**

View File

@@ -10,7 +10,7 @@ import lombok.experimental.SuperBuilder;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import static io.wdd.common.utils.OctopusObjectMapperConfig.OctopusObjectMapper; import static io.wdd.common.config.OctopusObjectMapperConfig.OctopusObjectMapper;
@Data @Data
@AllArgsConstructor @AllArgsConstructor

View File

@@ -22,7 +22,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static io.wdd.common.utils.OctopusObjectMapperConfig.OctopusObjectMapper; import static io.wdd.common.config.OctopusObjectMapperConfig.OctopusObjectMapper;
import static io.wdd.rpc.status.OctopusStatusMessage.*; import static io.wdd.rpc.status.OctopusStatusMessage.*;
@Slf4j @Slf4j

View File

@@ -67,6 +67,8 @@ public class ServerInfoVO {
@TableField(fill = FieldFill.INSERT_UPDATE) @TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime; private LocalDateTime updateTime;
private Integer proxyType;
/** /**
* server location , type City Country * server location , type City Country
*/ */
@@ -149,4 +151,5 @@ public class ServerInfoVO {
private String agentVersion; private String agentVersion;
} }

View File

@@ -5,8 +5,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest @SpringBootTest
class ServerApplicationTests { class ServerApplicationTests {
@@ -18,57 +16,7 @@ class ServerApplicationTests {
@Test @Test
void testCoreExecutionCompleteScript() { void testCoreExecutionCompleteScript() {
ArrayList<String> command1 = new ArrayList<>(
List.of(
"echo",
"yes"
)
);
ArrayList<String> command2 = new ArrayList<>(
List.of(
"apt-get",
"update"
)
);
ArrayList<String> command3 = new ArrayList<>(
List.of(
"echo",
"\"no\""
)
);
ArrayList<String> command4 = new ArrayList<>(
List.of(
"apt-get",
"install",
"nginx",
"-y"
)
);
List<List<String>> completeScript = new ArrayList<>();
completeScript.add(command1);
completeScript.add(command2);
completeScript.add(command3);
completeScript.add(command4);
ArrayList<String> targetMachineList = new ArrayList<>(
List.of(
"Chengdu-amd64-98-98066f"
)
);
List<String> resultList = asyncExecutionService.SyncSendCommandToAgentComplete(
targetMachineList,
"Scheduled Script",
completeScript
);
System.out.println("resultList = " + resultList);
} }
} }