[ agent ] [ status ] - accomplish agent status - 4
This commit is contained in:
@@ -39,14 +39,6 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!-- Operating System and Hardware Information library for Java -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.github.oshi</groupId>
|
|
||||||
<artifactId>oshi-core-java11</artifactId>
|
|
||||||
<version>6.4.0</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
|||||||
@@ -4,8 +4,12 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import io.wdd.agent.config.beans.init.AgentServerInfo;
|
import io.wdd.agent.config.beans.init.AgentServerInfo;
|
||||||
import io.wdd.agent.config.utils.TimeUtils;
|
import io.wdd.agent.config.utils.TimeUtils;
|
||||||
import io.wdd.agent.status.hardware.CpuInfo;
|
import io.wdd.common.beans.status.CpuInfo;
|
||||||
import io.wdd.agent.status.hardware.MemoryInfo;
|
import io.wdd.common.beans.status.DiskInfo;
|
||||||
|
import io.wdd.common.beans.status.MemoryInfo;
|
||||||
|
import io.wdd.common.beans.status.NetworkInfo;
|
||||||
|
import io.wdd.common.beans.status.AgentStatus;
|
||||||
|
import io.wdd.common.utils.FormatUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.redis.connection.stream.StreamRecords;
|
import org.springframework.data.redis.connection.stream.StreamRecords;
|
||||||
import org.springframework.data.redis.connection.stream.StringRecord;
|
import org.springframework.data.redis.connection.stream.StringRecord;
|
||||||
@@ -13,6 +17,7 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import oshi.SystemInfo;
|
import oshi.SystemInfo;
|
||||||
|
import oshi.hardware.HWDiskStore;
|
||||||
import oshi.hardware.HardwareAbstractionLayer;
|
import oshi.hardware.HardwareAbstractionLayer;
|
||||||
import oshi.software.os.OperatingSystem;
|
import oshi.software.os.OperatingSystem;
|
||||||
|
|
||||||
@@ -20,6 +25,7 @@ import javax.annotation.Resource;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -61,13 +67,19 @@ public class AgentStatusCollector {
|
|||||||
agentStatus.setCpuInfo(new CpuInfo(hardware.getProcessor(), 1000));
|
agentStatus.setCpuInfo(new CpuInfo(hardware.getProcessor(), 1000));
|
||||||
|
|
||||||
/* Memory */
|
/* Memory */
|
||||||
agentStatus.setMemoryInfo(new MemoryInfo().build(hardware.getMemory()));
|
agentStatus.setMemoryInfo(
|
||||||
|
MemoryInfo.build(hardware.getMemory())
|
||||||
|
);
|
||||||
|
|
||||||
/* Storage */
|
/* Storage */
|
||||||
agentStatus.setDiskStoreInfo(hardware.getDiskStores());
|
agentStatus.setDiskStoreInfo(
|
||||||
|
DiskInfo.mapFromDiskStore(hardware.getDiskStores())
|
||||||
|
);
|
||||||
|
|
||||||
/* Network */
|
/* Network */
|
||||||
agentStatus.setNetworkInfo(hardware.getNetworkIFs(false));
|
agentStatus.setNetworkInfo(
|
||||||
|
NetworkInfo.mapFromNetworkIFS(hardware.getNetworkIFs(false))
|
||||||
|
);
|
||||||
|
|
||||||
/* operating system info */
|
/* operating system info */
|
||||||
agentStatus.setOsInfo(os);
|
agentStatus.setOsInfo(os);
|
||||||
@@ -78,10 +90,21 @@ public class AgentStatusCollector {
|
|||||||
return agentStatus;
|
return agentStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* when server first time boot up
|
||||||
|
* the server info are not collected completely
|
||||||
|
* this will be executed to update or complete the octopus agent server info
|
||||||
|
*/
|
||||||
|
@Scheduled(initialDelay = 180000)
|
||||||
|
public void updateAgentServerInfo(){
|
||||||
|
|
||||||
// agent boot up 60s then start to report its status
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// agent boot up 120s then start to report its status
|
||||||
// at the fix rate of 15s
|
// at the fix rate of 15s
|
||||||
@Scheduled(initialDelay = 60000, fixedRate = 5000)
|
@Scheduled(initialDelay = 60000, fixedRate = 15000)
|
||||||
public void sendAgentStatusToRedis() {
|
public void sendAgentStatusToRedis() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -101,5 +124,4 @@ public class AgentStatusCollector {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
package io.wdd.agent.status.hardware;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import lombok.experimental.SuperBuilder;
|
|
||||||
import oshi.hardware.NetworkIF;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
@SuperBuilder(toBuilder = true)
|
|
||||||
public class NetworkInfo {
|
|
||||||
|
|
||||||
|
|
||||||
public static List<NetworkInfo> mapFromNetworkIFS(List<NetworkIF> networkIFList){
|
|
||||||
|
|
||||||
return networkIFList.stream().map(
|
|
||||||
networkIF -> NetworkInfo.builder()
|
|
||||||
.build()
|
|
||||||
).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -19,6 +19,18 @@
|
|||||||
<java.version>11</java.version>
|
<java.version>11</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- Operating System and Hardware Information library for Java -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.oshi</groupId>
|
||||||
|
<artifactId>oshi-core-java11</artifactId>
|
||||||
|
<version>6.4.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<!-- <build>-->
|
<!-- <build>-->
|
||||||
<!-- <plugins>-->
|
<!-- <plugins>-->
|
||||||
<!-- <plugin>-->
|
<!-- <plugin>-->
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
package io.wdd.agent.status;
|
package io.wdd.common.beans.status;
|
||||||
|
|
||||||
|
|
||||||
import io.wdd.agent.status.hardware.CpuInfo;
|
|
||||||
import io.wdd.agent.status.hardware.MemoryInfo;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.experimental.SuperBuilder;
|
import lombok.experimental.SuperBuilder;
|
||||||
import oshi.hardware.HWDiskStore;
|
|
||||||
import oshi.hardware.NetworkIF;
|
|
||||||
import oshi.software.os.OperatingSystem;
|
import oshi.software.os.OperatingSystem;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -30,9 +26,9 @@ public class AgentStatus {
|
|||||||
|
|
||||||
MemoryInfo memoryInfo;
|
MemoryInfo memoryInfo;
|
||||||
|
|
||||||
List<HWDiskStore> diskStoreInfo;
|
List<DiskInfo> diskStoreInfo;
|
||||||
|
|
||||||
List<NetworkIF> networkInfo;
|
List<NetworkInfo> networkInfo;
|
||||||
|
|
||||||
OperatingSystem osInfo;
|
OperatingSystem osInfo;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package io.wdd.agent.status.hardware;
|
package io.wdd.common.beans.status;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package io.wdd.agent.status.hardware;
|
package io.wdd.common.beans.status;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package io.wdd.common.beans.status;
|
||||||
|
|
||||||
|
import io.wdd.common.utils.FormatUtils;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
import oshi.hardware.HWDiskStore;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@SuperBuilder(toBuilder = true)
|
||||||
|
public class DiskInfo {
|
||||||
|
|
||||||
|
String name;
|
||||||
|
|
||||||
|
String model;
|
||||||
|
|
||||||
|
String serial;
|
||||||
|
|
||||||
|
String size;
|
||||||
|
|
||||||
|
|
||||||
|
private List<PartitionInfo> partitionInfoList;
|
||||||
|
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@SuperBuilder(toBuilder = true)
|
||||||
|
private static class PartitionInfo{
|
||||||
|
|
||||||
|
String path;
|
||||||
|
|
||||||
|
String type;
|
||||||
|
|
||||||
|
String size;
|
||||||
|
|
||||||
|
String mountPoint;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<DiskInfo> mapFromDiskStore(List<HWDiskStore> hwDiskStoreList){
|
||||||
|
|
||||||
|
return hwDiskStoreList.stream().map(
|
||||||
|
hwDiskStore -> DiskInfo.builder()
|
||||||
|
.name(hwDiskStore.getName())
|
||||||
|
.model(hwDiskStore.getModel())
|
||||||
|
.serial(hwDiskStore.getSerial())
|
||||||
|
.size(FormatUtils.formatData(hwDiskStore.getSize()))
|
||||||
|
.partitionInfoList(
|
||||||
|
// partition should also be got from stream
|
||||||
|
hwDiskStore.getPartitions().stream().map(partition -> DiskInfo.PartitionInfo.builder()
|
||||||
|
.path(partition.getIdentification())
|
||||||
|
.size(FormatUtils.formatData(partition.getSize()))
|
||||||
|
.type(partition.getType())
|
||||||
|
.mountPoint(partition.getMountPoint())
|
||||||
|
.build()
|
||||||
|
).collect(Collectors.toList())
|
||||||
|
)
|
||||||
|
.build()
|
||||||
|
).collect(Collectors.toList());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package io.wdd.agent.status.hardware;
|
package io.wdd.common.beans.status;
|
||||||
|
|
||||||
import io.wdd.agent.status.hardware.config.FormatUtils;
|
import io.wdd.common.utils.FormatUtils;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
@@ -30,7 +30,7 @@ public class MemoryInfo {
|
|||||||
|
|
||||||
String swapUsage;
|
String swapUsage;
|
||||||
|
|
||||||
public MemoryInfo build(GlobalMemory memory) {
|
public static MemoryInfo build(GlobalMemory memory) {
|
||||||
|
|
||||||
VirtualMemory virtualMemory = memory.getVirtualMemory();
|
VirtualMemory virtualMemory = memory.getVirtualMemory();
|
||||||
return MemoryInfo.builder()
|
return MemoryInfo.builder()
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package io.wdd.common.beans.status;
|
||||||
|
|
||||||
|
|
||||||
|
import io.wdd.common.utils.FormatUtils;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
import oshi.hardware.NetworkIF;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@SuperBuilder(toBuilder = true)
|
||||||
|
public class NetworkInfo {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String displayName;
|
||||||
|
|
||||||
|
private String macAddr;
|
||||||
|
|
||||||
|
private String mtu;
|
||||||
|
|
||||||
|
private String[] ipv4Addr;
|
||||||
|
private String[] ipv6Addr;
|
||||||
|
|
||||||
|
private String trafficRecv;
|
||||||
|
|
||||||
|
private String trafficSend;
|
||||||
|
|
||||||
|
public static List<NetworkInfo> mapFromNetworkIFS(List<NetworkIF> networkIFList) {
|
||||||
|
|
||||||
|
return networkIFList.stream().map(networkIF -> NetworkInfo.builder().name(networkIF.getName()).displayName(networkIF.getDisplayName()).mtu(String.valueOf(networkIF.getMTU())).macAddr(networkIF.getMacaddr()).ipv4Addr(generateIPDICRFromNetworkIFList(networkIF, 4)).ipv6Addr(generateIPDICRFromNetworkIFList(networkIF, 6)).trafficSend(FormatUtils.formatData(networkIF.getBytesSent())).trafficRecv(FormatUtils.formatData(networkIF.getBytesRecv())).build()).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String[] generateIPDICRFromNetworkIFList(NetworkIF networkIF, int Ipv4OrIpv6) {
|
||||||
|
|
||||||
|
String[] iPAddr;
|
||||||
|
Short[] subnetMasks;
|
||||||
|
|
||||||
|
if (Ipv4OrIpv6 == 4) {
|
||||||
|
iPAddr = networkIF.getIPv4addr();
|
||||||
|
subnetMasks = networkIF.getSubnetMasks();
|
||||||
|
} else {
|
||||||
|
iPAddr = networkIF.getIPv6addr();
|
||||||
|
subnetMasks = networkIF.getPrefixLengths();
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] result = new String[iPAddr.length];
|
||||||
|
for (int index = 0; index < iPAddr.length; index++) {
|
||||||
|
result[index] = iPAddr[index] + "/" + subnetMasks[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package io.wdd.agent.status.hardware.config;
|
package io.wdd.common.utils;
|
||||||
|
|
||||||
import org.springframework.util.unit.DataSize;
|
import org.springframework.util.unit.DataSize;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package io.wdd.agent.status.hardware.config;
|
package io.wdd.common.utils;
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
|
|
||||||
22
pom.xml
22
pom.xml
@@ -35,10 +35,10 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<dependency>
|
<!--<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
</dependency>
|
</dependency>-->
|
||||||
|
|
||||||
<!-- spring cloud -->
|
<!-- spring cloud -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -67,10 +67,10 @@
|
|||||||
<version>${alibaba-cloud.version}</version>
|
<version>${alibaba-cloud.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<!--<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-quartz</artifactId>
|
<artifactId>spring-boot-starter-quartz</artifactId>
|
||||||
</dependency>
|
</dependency>-->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
@@ -126,15 +126,15 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>spring-boot-devtools</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<scope>runtime</scope>
|
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
@@ -147,7 +147,7 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
<dependencyManagement>
|
<!--<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.shell</groupId>
|
<groupId>org.springframework.shell</groupId>
|
||||||
@@ -157,7 +157,7 @@
|
|||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>-->
|
||||||
|
|
||||||
<!--<build>
|
<!--<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|||||||
@@ -8,10 +8,9 @@ import org.springframework.context.ApplicationContextAware;
|
|||||||
import org.springframework.data.redis.stream.StreamMessageListenerContainer;
|
import org.springframework.data.redis.stream.StreamMessageListenerContainer;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import static io.wdd.rpc.execute.result.RedisStreamReaderConfig.REDIS_STREAM_LISTENER_CONTAINER;
|
import static io.wdd.rpc.execute.result.RedisStreamReaderConfig.COMMAND_RESULT_REDIS_STREAM_LISTENER_CONTAINER;
|
||||||
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@@ -25,7 +24,7 @@ public class CreateStreamReader implements ApplicationContextAware {
|
|||||||
private RedisStreamReaderConfig redisStreamReaderConfig;
|
private RedisStreamReaderConfig redisStreamReaderConfig;
|
||||||
|
|
||||||
|
|
||||||
private HashMap<String, StreamMessageListenerContainer> REDIS_STREAM_LISTENER_CONTAINER_CACHE = new HashMap<>(16);
|
private final HashMap<String, StreamMessageListenerContainer> REDIS_STREAM_LISTENER_CONTAINER_CACHE = new HashMap<>(16);
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -33,7 +32,7 @@ public class CreateStreamReader implements ApplicationContextAware {
|
|||||||
this.applicationContext = applicationContext;
|
this.applicationContext = applicationContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerStreamReader(String streamKey) {
|
public void registerStreamReader(String redisStreamListenerContainerBeanName, String streamKey) {
|
||||||
|
|
||||||
// prepare the environment
|
// prepare the environment
|
||||||
prepareEnv();
|
prepareEnv();
|
||||||
@@ -51,7 +50,7 @@ public class CreateStreamReader implements ApplicationContextAware {
|
|||||||
modifyStreamReader(streamKey);
|
modifyStreamReader(streamKey);
|
||||||
|
|
||||||
// re-create the REDIS_STREAM_LISTENER_CONTAINER
|
// re-create the REDIS_STREAM_LISTENER_CONTAINER
|
||||||
createStreamReader(streamKey);
|
createStreamReader(redisStreamListenerContainerBeanName, streamKey);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,12 +71,12 @@ public class CreateStreamReader implements ApplicationContextAware {
|
|||||||
this.beanFactory = applicationContext.getAutowireCapableBeanFactory();
|
this.beanFactory = applicationContext.getAutowireCapableBeanFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createStreamReader(String streamKey) {
|
private void createStreamReader(String redisStreamListenerContainerBeanName, String streamKey) {
|
||||||
|
|
||||||
log.debug("start to create the redis stream listener container");
|
log.debug("start to create the redis stream listener container");
|
||||||
// create the lazy bean
|
// create the lazy bean
|
||||||
|
|
||||||
StreamMessageListenerContainer streamMessageListenerContainer = applicationContext.getBean(REDIS_STREAM_LISTENER_CONTAINER, StreamMessageListenerContainer.class);
|
StreamMessageListenerContainer streamMessageListenerContainer = applicationContext.getBean(redisStreamListenerContainerBeanName, StreamMessageListenerContainer.class);
|
||||||
|
|
||||||
REDIS_STREAM_LISTENER_CONTAINER_CACHE.put(streamKey, streamMessageListenerContainer);
|
REDIS_STREAM_LISTENER_CONTAINER_CACHE.put(streamKey, streamMessageListenerContainer);
|
||||||
|
|
||||||
@@ -102,7 +101,6 @@ public class CreateStreamReader implements ApplicationContextAware {
|
|||||||
|
|
||||||
private void destroyStreamReader(String streamKey) {
|
private void destroyStreamReader(String streamKey) {
|
||||||
|
|
||||||
log.debug("start to destroy {}", REDIS_STREAM_LISTENER_CONTAINER);
|
|
||||||
|
|
||||||
String oldStreamKey = redisStreamReaderConfig.getStreamKey();
|
String oldStreamKey = redisStreamReaderConfig.getStreamKey();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package io.wdd.rpc.execute.result;
|
package io.wdd.rpc.execute.result;
|
||||||
|
|
||||||
|
|
||||||
|
import io.wdd.rpc.status.AgentStatusStreamReader;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -22,7 +23,9 @@ public class RedisStreamReaderConfig {
|
|||||||
@Resource
|
@Resource
|
||||||
private RedisConnectionFactory redisConnectionFactory;
|
private RedisConnectionFactory redisConnectionFactory;
|
||||||
|
|
||||||
public static final String REDIS_STREAM_LISTENER_CONTAINER = "redisStreamListenerContainer";
|
public static final String COMMAND_RESULT_REDIS_STREAM_LISTENER_CONTAINER = "commandResultRedisStreamListenerContainer";
|
||||||
|
|
||||||
|
public static final String AGENT_STATUS_REDIS_STREAM_LISTENER_CONTAINER = "agentStatusRedisStreamListenerContainer";
|
||||||
|
|
||||||
private String streamKey = "cccc";
|
private String streamKey = "cccc";
|
||||||
|
|
||||||
@@ -34,10 +37,10 @@ public class RedisStreamReaderConfig {
|
|||||||
return streamKey;
|
return streamKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean(value = REDIS_STREAM_LISTENER_CONTAINER)
|
@Bean(value = COMMAND_RESULT_REDIS_STREAM_LISTENER_CONTAINER)
|
||||||
@Scope("prototype")
|
@Scope("prototype")
|
||||||
@Lazy
|
@Lazy
|
||||||
public StreamMessageListenerContainer<String, MapRecord<String, String, String>> redisStreamListenerContainer(){
|
public StreamMessageListenerContainer<String, MapRecord<String, String, String>> commandResultRedisStreamListenerContainer(){
|
||||||
|
|
||||||
StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, MapRecord<String, String, String>> options = StreamMessageListenerContainer.StreamMessageListenerContainerOptions
|
StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, MapRecord<String, String, String>> options = StreamMessageListenerContainer.StreamMessageListenerContainerOptions
|
||||||
.builder()
|
.builder()
|
||||||
@@ -60,6 +63,31 @@ public class RedisStreamReaderConfig {
|
|||||||
return listenerContainer;
|
return listenerContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean(value = AGENT_STATUS_REDIS_STREAM_LISTENER_CONTAINER)
|
||||||
|
@Scope("prototype")
|
||||||
|
@Lazy
|
||||||
|
public StreamMessageListenerContainer<String, MapRecord<String, String, String>> agentStatusRedisStreamListenerContainer(){
|
||||||
|
|
||||||
|
StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, MapRecord<String, String, String>> options = StreamMessageListenerContainer.StreamMessageListenerContainerOptions
|
||||||
|
.builder()
|
||||||
|
.pollTimeout(Duration.ofSeconds(2))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
StreamMessageListenerContainer<String, MapRecord<String, String, String>> listenerContainer = StreamMessageListenerContainer.create(redisConnectionFactory, options);
|
||||||
|
|
||||||
|
listenerContainer.receive(
|
||||||
|
|
||||||
|
StreamOffset.create(streamKey, ReadOffset.lastConsumed()),
|
||||||
|
|
||||||
|
new AgentStatusStreamReader(
|
||||||
|
"OctopusServer",
|
||||||
|
"OctopusServer",
|
||||||
|
"OctopusServer")
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
return listenerContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ import java.time.format.DateTimeFormatter;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static io.wdd.rpc.execute.result.RedisStreamReaderConfig.COMMAND_RESULT_REDIS_STREAM_LISTENER_CONTAINER;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class CoreExecutionServiceImpl implements CoreExecutionService {
|
public class CoreExecutionServiceImpl implements CoreExecutionService {
|
||||||
@@ -68,7 +70,7 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
|
|||||||
log.debug("set consumer group for the stream key with => [ {} ]", resultKey);
|
log.debug("set consumer group for the stream key with => [ {} ]", resultKey);
|
||||||
|
|
||||||
// change the redis stream listener container
|
// change the redis stream listener container
|
||||||
createStreamReader.registerStreamReader(resultKey);
|
createStreamReader.registerStreamReader(COMMAND_RESULT_REDIS_STREAM_LISTENER_CONTAINER, resultKey);
|
||||||
|
|
||||||
// send the message
|
// send the message
|
||||||
messageSender.send(octopusMessage);
|
messageSender.send(octopusMessage);
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ import javax.annotation.Nullable;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static io.wdd.rpc.execute.result.RedisStreamReaderConfig.AGENT_STATUS_REDIS_STREAM_LISTENER_CONTAINER;
|
||||||
|
import static io.wdd.rpc.execute.result.RedisStreamReaderConfig.COMMAND_RESULT_REDIS_STREAM_LISTENER_CONTAINER;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("octopus/server/executor")
|
@RequestMapping("octopus/server/executor")
|
||||||
public class ExecutionController {
|
public class ExecutionController {
|
||||||
@@ -47,7 +50,17 @@ public class ExecutionController {
|
|||||||
@RequestParam(value = "streamKey") String streamKey
|
@RequestParam(value = "streamKey") String streamKey
|
||||||
) {
|
) {
|
||||||
|
|
||||||
createStreamReader.registerStreamReader(streamKey);
|
createStreamReader.registerStreamReader(COMMAND_RESULT_REDIS_STREAM_LISTENER_CONTAINER ,streamKey);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/agentStatusStream")
|
||||||
|
public void getAgentStatus(
|
||||||
|
@RequestParam(value = "streamKey") String streamKey
|
||||||
|
) {
|
||||||
|
|
||||||
|
createStreamReader.registerStreamReader(AGENT_STATUS_REDIS_STREAM_LISTENER_CONTAINER ,streamKey);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package io.wdd.rpc.status;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import io.wdd.common.beans.status.AgentStatus;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.data.redis.connection.stream.MapRecord;
|
||||||
|
import org.springframework.data.redis.connection.stream.RecordId;
|
||||||
|
import org.springframework.data.redis.stream.StreamListener;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Slf4j
|
||||||
|
public class AgentStatusStreamReader implements StreamListener<String, MapRecord<String,String, String >> {
|
||||||
|
|
||||||
|
// https://medium.com/nerd-for-tech/event-driven-architecture-with-redis-streams-using-spring-boot-a81a1c9a4cde
|
||||||
|
|
||||||
|
//https://segmentfault.com/a/1190000040946712
|
||||||
|
|
||||||
|
//https://docs.spring.io/spring-data/redis/docs/2.5.5/reference/html/#redis.streams.receive.containers
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消费者类型:独立消费、消费组消费
|
||||||
|
*/
|
||||||
|
private String consumerType;
|
||||||
|
/**
|
||||||
|
* 消费组
|
||||||
|
*/
|
||||||
|
private String group;
|
||||||
|
/**
|
||||||
|
* 消费组中的某个消费者
|
||||||
|
*/
|
||||||
|
private String consumerName;
|
||||||
|
|
||||||
|
|
||||||
|
public AgentStatusStreamReader(String consumerType, String group, String consumerName) {
|
||||||
|
this.consumerType = consumerType;
|
||||||
|
this.group = group;
|
||||||
|
this.consumerName = consumerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessage(MapRecord<String, String, String> message) {
|
||||||
|
|
||||||
|
String streamKey = message.getStream();
|
||||||
|
RecordId messageId = message.getId();
|
||||||
|
String key = (String) message.getValue().keySet().toArray()[0];
|
||||||
|
String value = message.getValue().get(key);
|
||||||
|
|
||||||
|
|
||||||
|
log.info("Octopus Agent [ {} ] status of [ {} ] Time is [ {} ] stream recordId is [{}]", streamKey, key, key, messageId);
|
||||||
|
|
||||||
|
// print to console
|
||||||
|
printPrettyAgentStatus(value);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void printPrettyAgentStatus(String valueString){
|
||||||
|
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
String tmp = objectMapper.readValue(valueString, new TypeReference<String>() {
|
||||||
|
});
|
||||||
|
|
||||||
|
AgentStatus agentStatus = objectMapper.readValue(tmp, new TypeReference<AgentStatus>() {
|
||||||
|
});
|
||||||
|
|
||||||
|
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(agentStatus));
|
||||||
|
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ import io.wdd.server.coreService.CoreServerService;
|
|||||||
import io.wdd.server.service.*;
|
import io.wdd.server.service.*;
|
||||||
import io.wdd.server.utils.EntityUtils;
|
import io.wdd.server.utils.EntityUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
|
||||||
@@ -20,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -85,8 +87,22 @@ public class CoreServerServiceImpl implements CoreServerService {
|
|||||||
@Override
|
@Override
|
||||||
public boolean serverCreateOrUpdate(ServerInfoVO serverInfoVO) {
|
public boolean serverCreateOrUpdate(ServerInfoVO serverInfoVO) {
|
||||||
|
|
||||||
ServerInfoPO serverInfoPO = EntityUtils.cvToTarget(serverInfoVO, ServerInfoPO.class);
|
ServerInfoPO po = new LambdaQueryChainWrapper<ServerInfoPO>(serverInfoService.getBaseMapper())
|
||||||
return serverInfoService.saveOrUpdate(serverInfoPO);
|
.eq(ServerInfoPO::getServerName, serverInfoVO.getServerName()).one();
|
||||||
|
|
||||||
|
if (ObjectUtils.isNotEmpty(po)) {
|
||||||
|
try {
|
||||||
|
org.apache.commons.beanutils.BeanUtils.copyProperties(po, serverInfoVO);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
po = EntityUtils.cvToTarget(serverInfoVO, ServerInfoPO.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return serverInfoService.saveOrUpdate(po);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user