我正在尝试使用Netty连接到Docker UNIX域套接字。到目前为止,这是我的尝试。

@PostConstruct
public void init() throws Exception {
    io.netty.bootstrap.Bootstrap bootstrap = new io.netty.bootstrap.Bootstrap();
    bootstrap
            .group(new NioEventLoopGroup())
            .channel(NioSocketChannel.class)
            .option(ChannelOption.SO_KEEPALIVE, true)
            .remoteAddress(new DomainSocketAddress("/var/run/docker.sock"))
    .handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel socketChannel) throws Exception {
            socketChannel
                    .pipeline()
                    .addLast(new SimpleChannelInboundHandler<HttpObject>() {
                        @Override
                        protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception {
                            System.out.println(httpObject);
                        }
                    });
        }
    });
    final Channel channel = bootstrap.connect().sync().channel();

    final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/services", Unpooled.EMPTY_BUFFER);
    request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    channel.writeAndFlush(request);
    channel.closeFuture().sync();
    System.out.println("DONE");
}

此刻我正在



是否存在有关如何使用Netty与UDS进行HTTP连接的示例?到目前为止,我只发现了原始的UDS和TCP HTTP,但没有发现它们的组合。

最佳答案

这是一个可行的实现。

        io.netty.bootstrap.Bootstrap bootstrap = new io.netty.bootstrap.Bootstrap();
        final EpollEventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup();
        try {
            bootstrap
                    .group(epollEventLoopGroup)
                    .channel(EpollDomainSocketChannel.class)
                    .handler(new ChannelInitializer<UnixChannel>() {
                        @Override
                        public void initChannel(UnixChannel ch) throws Exception {
                            ch
                                    .pipeline()
                                    .addLast(new HttpClientCodec())
                                    .addLast(new HttpContentDecompressor())
                                    .addLast(new SimpleChannelInboundHandler<HttpObject>() {
                                        private StringBuilder messageBuilder = new StringBuilder();
                                        @Override
                                        public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
                                            if (msg instanceof HttpContent) {
                                                HttpContent content = (HttpContent) msg;
                                                messageBuilder.append(content.content().toString(StandardCharsets.UTF_8));
                                                if (msg instanceof LastHttpContent) {
                                                    System.out.println(messageBuilder);
                                                }
                                            } else {
                                                System.out.println(msg.getClass());
                                            }
                                        }
                                    });
                        }
                    });
            final Channel channel = bootstrap.connect(new DomainSocketAddress("/var/run/docker.sock")).sync().channel();

            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/services", Unpooled.EMPTY_BUFFER);
            request.headers().set(HttpHeaderNames.HOST, "daemon");
            channel.writeAndFlush(request);
            channel.closeFuture().sync();
        } finally {
            epollEventLoopGroup.shutdownGracefully();
        }

几件事要注意:
  • EpollEventLoopGroupEpollDomainSocketChannelChannelInitializer<UnixChannel>一起使用。
  • HTTP需要管道中的new HttpCodec()才能使用Netty HTTP对象。
  • 数据可能已分块,因此您需要对其进行汇编并等待LastHttpContent对象

  • https://github.com/trajano/netty-docker-daemon-socket

    关于docker - 如何使用Netty连接到运行HTTP服务器的UNIX域套接字?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58455529/

    10-14 11:08