|
@@ -6,20 +6,14 @@ import ch.qos.logback.classic.LoggerContext;
|
|
|
import cn.reghao.devops.agent.config.ConfigFile;
|
|
import cn.reghao.devops.agent.config.ConfigFile;
|
|
|
import cn.reghao.devops.agent.config.AgentConfig;
|
|
import cn.reghao.devops.agent.config.AgentConfig;
|
|
|
import cn.reghao.devops.agent.ws.WebSocketClient;
|
|
import cn.reghao.devops.agent.ws.WebSocketClient;
|
|
|
-import cn.reghao.devops.common.docker.Docker;
|
|
|
|
|
-import cn.reghao.devops.common.docker.DockerImpl;
|
|
|
|
|
import cn.reghao.devops.common.docker.DockerManager;
|
|
import cn.reghao.devops.common.docker.DockerManager;
|
|
|
import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
-import cn.reghao.jutil.jdk.string.StringRegexp;
|
|
|
|
|
-import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.net.*;
|
|
import java.net.*;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
|
|
-import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class AgentApp {
|
|
public class AgentApp {
|
|
@@ -29,25 +23,6 @@ public class AgentApp {
|
|
|
rootLogger.setLevel(Level.INFO);
|
|
rootLogger.setLevel(Level.INFO);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static void tryConnect(String host, int port) {
|
|
|
|
|
- try {
|
|
|
|
|
- boolean isAddress = StringRegexp.matchIPv4Address(host);
|
|
|
|
|
- InetAddress inetAddress;
|
|
|
|
|
- if (isAddress) {
|
|
|
|
|
- inetAddress = InetAddress.getByAddress(host.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
- } else {
|
|
|
|
|
- inetAddress = InetAddress.getByName(host);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- InetSocketAddress socketAddress = new InetSocketAddress(inetAddress, port);
|
|
|
|
|
- Socket socket = new Socket();
|
|
|
|
|
- socket.connect(socketAddress);
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- String errorMsg = String.format("尝试建立到 %s:%s 的 Socket 连接失败", host, port);
|
|
|
|
|
- throw new RuntimeException(errorMsg);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
private static void shutdownGracefully(WebSocketClient webSocketClient) {
|
|
private static void shutdownGracefully(WebSocketClient webSocketClient) {
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
log.info("收到退出信号,正在执行清理...");
|
|
log.info("收到退出信号,正在执行清理...");
|
|
@@ -114,7 +89,6 @@ public class AgentApp {
|
|
|
AgentConfig agentConfig = JsonConverter.jsonFileToObject(new File(configFilePath), AgentConfig.class);
|
|
AgentConfig agentConfig = JsonConverter.jsonFileToObject(new File(configFilePath), AgentConfig.class);
|
|
|
// 4. 启动核心服务 (心跳、监控、WebSocket)
|
|
// 4. 启动核心服务 (心跳、监控、WebSocket)
|
|
|
WebSocketClient webSocketClient = new WebSocketClient(agentConfig);
|
|
WebSocketClient webSocketClient = new WebSocketClient(agentConfig);
|
|
|
- tryConnect(agentConfig.getHost(), agentConfig.getPort());
|
|
|
|
|
webSocketClient.start();
|
|
webSocketClient.start();
|
|
|
shutdownGracefully(webSocketClient);
|
|
shutdownGracefully(webSocketClient);
|
|
|
// 6. 阻塞主线程,防止应用退出
|
|
// 6. 阻塞主线程,防止应用退出
|