使用Netty构建HTTP代理服务器的简单方法?

5
我刚接触Netty,并希望使用它制作一个简单的HTTP代理服务器,该服务器接收来自客户端的请求,将请求转发到另一个服务器,然后将响应复制回原始请求的响应。另外一个要求是我必须支持超时,因此如果代理服务器响应时间过长,代理将自己响应并关闭与代理服务器的连接。
我已经使用Jetty实现了这样的应用程序,但是使用Jetty需要使用太多线程以防止入站请求被阻塞(这是一个轻量级应用程序,使用非常少的内存或CPU,但是代理服务器的延迟足够高,使得流量爆发会导致在代理服务器中排队,或者需要太多线程)。
根据我的理解,我可以使用Netty构建一个管道,在该管道中,每个阶段执行少量计算,然后释放其线程并等待数据准备就绪,以便执行管道中的下一个阶段。
我的问题是,是否有这样一个应用程序的简单示例? 我目前拥有的只是基本Netty教程的服务器代码的简单修改,但缺乏对客户端的所有支持。 我看到了Netty客户端教程,但不确定如何混合两者的代码以创建一个简单的代理应用程序。
public static void main(String[] args) throws Exception {
    ChannelFactory factory =
            new NioServerSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool());

    ServerBootstrap bootstrap = new ServerBootstrap(factory);

    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            return Channels.pipeline(
                    new HttpRequestDecoder(),
                    new HttpResponseEncoder(),
                    /* 
                     * Is there something I can put here to make a
                     * request to another server asynchronously and
                     * copy the result to the response inside
                     * MySimpleChannelHandler?
                     */
                    new MySimpleChannelHandler()
                    );
        }
    });

    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.keepAlive", true);

    bootstrap.bind(new InetSocketAddress(8080));
}

private static class MySimpleChannelHandler extends SimpleChannelHandler {

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        HttpRequest request = (HttpRequest) e.getMessage();
        HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        response.setContent(request.getContent());

        Channel ch = e.getChannel();
        ChannelFuture f = ch.write(response);
        f.addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future) {
                Channel ch = future.getChannel();
                ch.close();
            }
        });
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        e.getCause().printStackTrace();

        Channel ch = e.getChannel();
        ch.close();
    }
}
1个回答

3

如果您想了解如何实现这一点,可以查看LittleProxy的代码,因为它是基于Netty框架编写的。


2
The link is broken - Joel Shemtov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接