Ver código fonte

修改 docker bug

reghao 5 anos atrás
pai
commit
da00ea03b0

+ 1 - 1
common/src/main/java/cn/reghao/autodop/common/amqp/RabbitProducer.java

@@ -44,7 +44,7 @@ public class RabbitProducer {
         String msg = JsonConverter.objectToJson(mqMessage);
         String result = (String) rabbitTemplate.convertSendAndReceive(exchange, routeKey, msg);
         if (result == null) {
-            return RpcResult.fail("RPC timeout...");
+            return RpcResult.fail("RPC 调用超时...");
         } else {
             return (RpcResult) JsonConverter.jsonToObject(result, RpcResult.class);
         }

+ 8 - 0
common/src/main/java/cn/reghao/autodop/common/dockerc/Docker.java

@@ -389,4 +389,12 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
         // TODO 可能抛出 null 异常
         return containerInspect.getGraphDriver().getData().getMergedDir();
     }
+
+    public static void main(String[] args) {
+        try (Docker docker = new Docker()) {
+            docker.pull("localhost:5000/iq3x/dnkt-mgr:dfc43519");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 }

+ 13 - 13
common/src/main/java/cn/reghao/autodop/common/dockerc/unixdomain/HttpClient.java

@@ -13,10 +13,10 @@ import java.util.List;
  * @date 2020-01-15 10:13:21
  */
 public class HttpClient {
-    private UnixSocketClient client;
+    private UnixSocketClient unixSocketClient;
 
-    public HttpClient(UnixSocketClient client) {
-        this.client = client;
+    public HttpClient(UnixSocketClient unixSocketClient) {
+        this.unixSocketClient = unixSocketClient;
     }
 
     public FullHttpResponse get(String uri, List<DockerHeader> headers) {
@@ -30,8 +30,8 @@ public class HttpClient {
         }
 
         try {
-            return client.syncSend(request);
-        } catch (InterruptedException e) {
+            return unixSocketClient.syncSend(request);
+        } catch (Exception e) {
             return null;
         }
     }
@@ -47,8 +47,8 @@ public class HttpClient {
         }
 
         try {
-            return client.syncSend(request);
-        } catch (InterruptedException e) {
+            return unixSocketClient.syncSend(request);
+        } catch (Exception e) {
             return null;
         }
     }
@@ -69,8 +69,8 @@ public class HttpClient {
         request.content().clear().writeBytes(byteBuf);
 
         try {
-            return client.syncSend(request);
-        } catch (InterruptedException e) {
+            return unixSocketClient.syncSend(request);
+        } catch (Exception e) {
             return null;
         }
     }
@@ -91,7 +91,7 @@ public class HttpClient {
                 });
             }
 
-            return client.syncSend(request);
+            return unixSocketClient.syncSend(request);
         } catch (Exception e) {
             e.printStackTrace();
             return null;
@@ -122,13 +122,13 @@ public class HttpClient {
         }
 
         try {
-            return client.syncSend(request);
-        } catch (InterruptedException e) {
+            return unixSocketClient.syncSend(request);
+        } catch (Exception e) {
             return null;
         }
     }
 
     public void close() {
-        client.close();
+        unixSocketClient.close();
     }
 }

+ 12 - 11
common/src/main/java/cn/reghao/autodop/common/dockerc/unixdomain/HttpClientHandler.java

@@ -5,6 +5,7 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
 import io.netty.channel.ChannelPromise;
 import io.netty.handler.codec.http.FullHttpRequest;
 import io.netty.handler.codec.http.FullHttpResponse;
+import io.netty.handler.codec.http.HttpResponseStatus;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -20,13 +21,20 @@ public class HttpClientHandler extends ChannelInboundHandlerAdapter {
     private FullHttpResponse response;
 
     public ChannelPromise sendRequest(FullHttpRequest request) {
-        while (context == null) {
+        int count = 5;
+        // TODO 禁止无限循环
+        while (context == null && count > 0) {
             try {
-                // 等待 channel 建立
+                // 等待 5s 建立 channel,超时则结束
                 Thread.sleep(1_000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
+            count--;
+        }
+
+        if (context == null) {
+            return null;
         }
 
         promise = context.newPromise();
@@ -48,17 +56,10 @@ public class HttpClientHandler extends ChannelInboundHandlerAdapter {
         if (msg instanceof FullHttpResponse) {
             response = (FullHttpResponse) msg;
             int statusCode = response.status().code();
-            // TODO 不返回 1xx 响应
-            if (statusCode >= 200) {
+            // 不返回 1xx 响应
+            if (statusCode >= HttpResponseStatus.OK.code()) {
                 promise.setSuccess();
             }
-            /*if (statusCode >= 200 && statusCode < 400) {
-                promise.setSuccess();
-            } else if (statusCode >= 400) {
-                String message = "failed";
-                Throwable t = new Throwable(message);
-                promise.setFailure(t);
-            }*/
         } else {
             log.info("Not FullHttpResponse -> {}", msg.getClass());
             System.out.println(msg.getClass());

+ 11 - 3
common/src/main/java/cn/reghao/autodop/common/dockerc/unixdomain/UnixSocketClient.java

@@ -43,7 +43,6 @@ public class UnixSocketClient {
                     @Override
                     public void initChannel(UnixChannel ch) throws Exception {
                         ChannelPipeline pipeline = ch.pipeline();
-                        //pipeline.addLast(new LoggingHandler(LogLevel.ERROR));
                         pipeline.addLast(new HttpClientCodec());
                         pipeline.addLast(new HttpObjectAggregator(65536*8192));
                         pipeline.addLast(clientHandler);
@@ -56,7 +55,8 @@ public class UnixSocketClient {
                log.info("已连接到 Unix Socket");
                channel = channelFuture.channel();
            } else {
-               throw new IOException("Unix Socket 连接失败...");
+               log.error("Unix Socket 连接失败...");
+               //throw new IOException("Unix Socket 连接失败...");
            }
         });
 
@@ -74,8 +74,16 @@ public class UnixSocketClient {
         }
     }
 
-    public FullHttpResponse syncSend(FullHttpRequest request) throws InterruptedException {
+    public FullHttpResponse syncSend(FullHttpRequest request) throws InterruptedException, IOException {
+        if (channel == null) {
+            throw new IOException("Unix Socket 连接失败...");
+        }
+
         ChannelPromise promise = clientHandler.sendRequest(request);
+        if (promise == null) {
+            return null;
+        }
+
         promise.await(30, TimeUnit.SECONDS);
         return clientHandler.response();
     }

+ 7 - 0
dmaster/pom.xml

@@ -23,6 +23,13 @@
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-devtools</artifactId>
+            <optional>true</optional>
+            <scope>true</scope>
+        </dependency>
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-security</artifactId>

+ 0 - 7
pom.xml

@@ -31,13 +31,6 @@
     </properties>
 
     <dependencies>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-            <scope>true</scope>
-        </dependency>
-
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-amqp</artifactId>