Sfoglia il codice sorgente

fix(docker): 解决不同环境下的Docker API版本兼容性问题

- 添加了环境变量注入用于区分当前运行环境
- 根据环境动态选择Docker API版本(v1.43用于测试环境,v1.44用于其他环境)
- 修复了因API版本固定导致的环境兼容性问题
- 确保测试环境和其他环境都能正确调用Docker日志查询接口
ChenYu 7 mesi fa
parent
commit
e3159c297f
1 ha cambiato i file con 14 aggiunte e 5 eliminazioni
  1. 14 5
      src/main/java/com/aitori/service/DockerLogService.java

+ 14 - 5
src/main/java/com/aitori/service/DockerLogService.java

@@ -3,6 +3,7 @@ package com.aitori.service;
 import com.aitori.config.DockerProperties;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import java.io.ByteArrayOutputStream;
@@ -24,6 +25,9 @@ public class DockerLogService {
 
     private final DockerProperties dockerProperties;
 
+    @Value("spring.profiles.active")
+    public String env;
+
     /**
      * 通过 Docker Engine API (Unix Socket) 查询容器日志并按关键词过滤。
      * 需要宿主机将 /var/run/docker.sock 挂载到容器内。
@@ -42,11 +46,16 @@ public class DockerLogService {
             throw new IllegalStateException(
                     "Docker socket 不存在: " + socketPath + ",请确认已挂载 -v /var/run/docker.sock:/var/run/docker.sock");
         }
-
-        String requestPath = String.format(
-                "/v1.43/containers/%s/logs?stdout=true&stderr=true&tail=%d&timestamps=true",
-                containerName, tailLines);
-
+        String requestPath;
+        if(env.equals("test")){
+            requestPath = String.format(
+                    "/v1.43/containers/%s/logs?stdout=true&stderr=true&tail=%d&timestamps=true",
+                    containerName, tailLines);
+        }else {
+            requestPath = String.format(
+                    "/v1.44/containers/%s/logs?stdout=true&stderr=true&tail=%d&timestamps=true",
+                    containerName, tailLines);
+        }
         byte[] rawResponse = callDockerApi(sockFile, requestPath);
         String body = extractHttpBody(rawResponse);