使用Netty构建http代理服务器的简单方法? [英] Simple way to use Netty to build an http proxy server?

查看:214
本文介绍了使用Netty构建http代理服务器的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Netty的新手,正在考虑使用它来制作一个简单的http代理服务器,该服务器接收来自客户端的请求,将请求转发到另一台服务器,然后将响应复制回原始请求的响应. .一个额外的要求是我能够支持超时,以便如果代理服务器花费的时间太长,则代理将自行响应并关闭与代理服务器的连接.

I'm new to Netty, and am looking at using it to make a simple http proxy server that receives requests from a client, forwards the requests to another server, and then copies the response back to the response for the original request. One extra requirement is that I be able to support a timeout, so that if the proxied server takes too long to respond the proxy will respond by itself and close the connection to the proxied server.

我已经使用Jetty实现了这样的应用程序,但是使用Jetty时,我需要使用过多的线程来阻止入站请求被阻止(这是一个使用很少的内存或cpu的轻量级应用程序,但是代理服务器足够高,以至于流量突发会导致代理服务器排队或需要太多线程.

I've already implemented such an application using Jetty, but with Jetty I need to use too many threads to keep inbound requests from getting blocked (this is a lightweight app that uses very little memory or cpu, but the latency of the proxied server is high enough that bursts in traffic cause either queueing in the proxy server, or require too many threads).

根据我的理解,我可以使用Netty构建一个管道,其中每个阶段执行少量计算,然后释放它的线程并等待数据准备就绪,以便可以在管道中的下一个阶段执行.

According to my understanding, I can use Netty to build a pipeline in which each stage performs a small amount of computation, then releases it's thread and waits until data is ready for the next stage in the pipeline to be executed.

我的问题是,有没有这样一个应用程序的简单示例?到目前为止,我只是对基本Netty教程的服务器代码进行了简单的修改,但是它缺少对客户端的所有支持.我看到了netty客户端教程,但是不确定如何将两者的代码混合在一起以创建简单的代理应用.

My question is, is there a simple example of such an application? What I have so far is a simple modification of the server code for the basic Netty tutorial, but it lacks all support for a client. I saw the netty client tutorial, but am not sure how to mix code from the two to create a simple proxy app.

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();
    }
}

推荐答案

您必须查看 LittleProxy 看看他们是如何做的,就像在Netty上写的一样.

you would have to look at LittleProxy to see how they did it as it is written on top of Netty.

这篇关于使用Netty构建http代理服务器的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆