记一次Http请求Netty错误

项目使用Netty作为后台服务的入口,使用Http协议进行访问,但是在HttpClient请求Netty时会报:

ip:port fail to respond

错误,进过排查,修改如下代码

ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(this.bossGroup, this.workerGroup).channel(NioServerSocketChannel.class).childHandler(
                    new ChannelInitializer<SocketChannel>() {
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            if (sslContext != null) {
                                p.addLast(new ChannelHandler[]{sslContext.newHandler(ch.alloc())});
                            }
                            p.addLast(new ChannelHandler[]{new HttpRequestDecoder()});
                            // 原先是p.addLast(new ChannelHandler[]{new HttpResponseDecoder()});修改后如下
                            p.addLast(new ChannelHandler[]{new HttpResponseEncoder()});
                            p.addLast(new ChannelHandler[]{new HttpObjectAggregator(1048576)});
                            p.addLast(new ChannelHandler[]{new HttpContentCompressor()});
                            p.addLast(new ChannelHandler[]{new ChunkedWriteHandler()});
                            p.addLast(new LoggingHandler(LogLevel.INFO));
                            p.addLast(handler);
                        }
                    }
            ).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, false).childOption(ChannelOption.TCP_NODELAY, true);
            Channel channel = serverBootstrap.bind(this.PORT).sync().channel();
# Java 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×