2016-07-29 145 views
3

我想使用Netty 4.1编码高性能反向代理服务器。 我的代码是基于Feng-Zihao/protoxNetty Proxy Example的Java改编版本。Netty过滤反向代理

我第一次在处理100-CONTINUE时遇到了一些麻烦,但是在我的管道中添加了HttpObjectAggregator有点解决了这个问题。

serverBootstrap 
     .group(bossGroup, workerGroup) 
     .channel(NioServerSocketChannel.class) 
     .handler(new LoggingHandler(LogLevel.DEBUG)) 
     .childHandler(new ChannelInitializer<SocketChannel>() { 
      @Override 
      public void initChannel(SocketChannel ch) throws Exception { 
       ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG)); 
       ch.pipeline().addLast(new HttpRequestDecoder()); 
       ch.pipeline().addLast(new HttpResponseEncoder()); 
       ch.pipeline().addLast(new HttpObjectAggregator(1048576)); 
       ch.pipeline().addLast(new FrontendHandler()); 
      } 
     }) 
     //   .option(ChannelOption.SO_REUSEADDR, true) 
     //   .option(ChannelOption.SO_BACKLOG, 128) 
     //   .childOption(ChannelOption.SO_KEEPALIVE, true) 
     .childOption(ChannelOption.AUTO_READ, false) 
     .bind(port).sync(); 

在客户端,请求无限期地挂起。 事情是,AUTO_READ在false似乎阻止HttpObjectAggregator做他的工作,我的FrontendHandler只有收到channelActive事件,但从来没有channelRead

seems though,我需要确保我不会进入读取和远程对等连接之间的竞争条件。我想我最终的目标是选择转发还是不转发基于过滤器(可能是我的FrontendHandler之前的新处理程序)的请求,这需要读取完整的http内容。

我在这里错过了什么吗?

回答

2

当您的出站通道变为活动状态时打开自动读取功能,并让您的FrontendHandler在处理每封邮件时将其关闭。然后在准备好处理另一条消息时再打开它。

这将让HttpObjectAggregator继续读取尽可能多的消息,以创建FullHttpMessage,然后在您的FrontendHandler正在处理或等待某些客户端写入以调用侦听器时停止发送消息。

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 
    ctx.channel().config().setAutoRead(false); 
    ... 
    // This call should probably be in some event listener 
    ctx.channel().config().setAutoRead(true);