Docker采集更新
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
package com.tongran.agentserver.server.collect.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
||||
import com.github.dockerjava.core.DockerClientImpl;
|
||||
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
|
||||
import com.github.dockerjava.transport.DockerHttpClient;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
public class DockerClientManager {
|
||||
|
||||
private static DockerClient dockerClient;
|
||||
|
||||
public static synchronized DockerClient getDockerClient() {
|
||||
if (dockerClient == null) {
|
||||
dockerClient = createDockerClient();
|
||||
}
|
||||
return dockerClient;
|
||||
}
|
||||
|
||||
private static DockerClient createDockerClient() {
|
||||
try {
|
||||
DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
||||
.withDockerHost("unix:///var/run/docker.sock")
|
||||
.withDockerTlsVerify(false) // 根据你的配置调整
|
||||
.build();
|
||||
|
||||
DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
|
||||
.dockerHost(config.getDockerHost())
|
||||
.sslConfig(config.getSSLConfig())
|
||||
.maxConnections(100)
|
||||
.connectionTimeout(Duration.ofSeconds(30))
|
||||
.responseTimeout(Duration.ofSeconds(45))
|
||||
.build();
|
||||
|
||||
return DockerClientImpl.getInstance(config, httpClient);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to create Docker client", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getDockerHost() {
|
||||
// 可以从环境变量、配置文件等获取
|
||||
String host = System.getenv("DOCKER_HOST");
|
||||
if (host == null || host.trim().isEmpty()) {
|
||||
host = "unix:///var/run/docker.sock"; // 默认值
|
||||
}
|
||||
return host;
|
||||
}
|
||||
|
||||
public static void close() {
|
||||
if (dockerClient != null) {
|
||||
try {
|
||||
dockerClient.close();
|
||||
} catch (Exception e) {
|
||||
// 记录日志
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
-2
@@ -2,7 +2,10 @@ package com.tongran.agentserver.server.collect.docker.impl;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.model.Container;
|
||||
import com.tongran.agentserver.server.collect.docker.DockerClientManager;
|
||||
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
||||
import com.github.dockerjava.core.DockerClientImpl;
|
||||
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
|
||||
import com.github.dockerjava.transport.DockerHttpClient;
|
||||
import com.tongran.agentserver.server.collect.docker.DockerService;
|
||||
import com.tongran.agentserver.server.collect.vo.DockerVO;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -11,6 +14,7 @@ import org.springframework.stereotype.Service;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -20,7 +24,20 @@ public class DockerServiceImpl implements DockerService {
|
||||
public List<DockerVO> query(){
|
||||
List<DockerVO> list = new ArrayList<>();
|
||||
// 配置Docker客户端
|
||||
DockerClient dockerClient = DockerClientManager.getDockerClient();
|
||||
DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
||||
.withDockerHost("unix:///var/run/docker.sock")
|
||||
.withDockerTlsVerify(false) // 根据你的配置调整
|
||||
.build();
|
||||
|
||||
DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
|
||||
.dockerHost(config.getDockerHost())
|
||||
.sslConfig(config.getSSLConfig())
|
||||
.maxConnections(100)
|
||||
.connectionTimeout(Duration.ofSeconds(30))
|
||||
.responseTimeout(Duration.ofSeconds(45))
|
||||
.build();
|
||||
|
||||
DockerClient dockerClient = DockerClientImpl.getInstance(config, httpClient);
|
||||
try {
|
||||
// 获取正在运行的容器列表
|
||||
List<Container> containers = dockerClient.listContainersCmd()
|
||||
|
||||
Reference in New Issue
Block a user