美文网首页
从零手写实现 nginx-35-proxy_pass netty

从零手写实现 nginx-35-proxy_pass netty

作者: 老马啸西风2020 | 来源:发表于2024-07-27 00:35 被阅读0次

    前言

    大家好,我是老马。很高兴遇到你。

    我们为 java 开发者实现了 java 版本的 nginx

    https://github.com/houbb/nginx4j

    如果你想知道 servlet 如何处理的,可以参考我的另一个项目:

    手写从零实现简易版 tomcat minicat

    手写 nginx 系列

    如果你对 nginx 原理感兴趣,可以阅读:

    从零手写实现 nginx-01-为什么不能有 java 版本的 nginx?

    从零手写实现 nginx-02-nginx 的核心能力

    从零手写实现 nginx-03-nginx 基于 Netty 实现

    从零手写实现 nginx-04-基于 netty http 出入参优化处理

    从零手写实现 nginx-05-MIME类型(Multipurpose Internet Mail Extensions,多用途互联网邮件扩展类型)

    从零手写实现 nginx-06-文件夹自动索引

    从零手写实现 nginx-07-大文件下载

    从零手写实现 nginx-08-范围查询

    从零手写实现 nginx-09-文件压缩

    从零手写实现 nginx-10-sendfile 零拷贝

    从零手写实现 nginx-11-file+range 合并

    从零手写实现 nginx-12-keep-alive 连接复用

    从零手写实现 nginx-13-nginx.conf 配置文件介绍

    从零手写实现 nginx-14-nginx.conf 和 hocon 格式有关系吗?

    从零手写实现 nginx-15-nginx.conf 如何通过 java 解析处理?

    从零手写实现 nginx-16-nginx 支持配置多个 server

    从零手写实现 nginx-17-nginx 默认配置优化

    从零手写实现 nginx-18-nginx 请求头+响应头操作

    从零手写实现 nginx-19-nginx cors

    从零手写实现 nginx-20-nginx 占位符 placeholder

    从零手写实现 nginx-21-nginx modules 模块信息概览

    从零手写实现 nginx-22-nginx modules 分模块加载优化

    从零手写实现 nginx-23-nginx cookie 的操作处理

    从零手写实现 nginx-24-nginx IF 指令

    从零手写实现 nginx-25-nginx map 指令

    从零手写实现 nginx-26-nginx rewrite 指令

    从零手写实现 nginx-27-nginx return 指令

    从零手写实现 nginx-28-nginx error_pages 指令

    从零手写实现 nginx-29-nginx try_files 指令

    从零手写实现 nginx-30-nginx proxy_pass upstream 指令

    从零手写实现 nginx-31-nginx load-balance 负载均衡

    从零手写实现 nginx-32-nginx load-balance 算法 java 实现

    从零手写实现 nginx-33-nginx http proxy_pass 测试验证

    从零手写实现 nginx-34-proxy_pass 配置加载处理

    从零手写实现 nginx-35-proxy_pass netty 如何实现?

    netty 如何实现反向代理?

    整体思路

    1. 根据原始的 request 请求,构建新的请求对象 forwardedRequest

    2. 根据指定的路由策略,获取一个目标服务器。

    3. 根据目标服务器的 host+port,用 netty 直接模拟 http 客户端,直接访问远程服务端,然后把远程的响应写回到当前的客户端 resp

    实现代码

    核心实现如下:

    /**
     * netty 实现反向代理
     * 
     * @since 0.27.0
     * @author 老马啸西风
     */
    public class NginxRequestDispatchProxyPass extends AbstractNginxRequestDispatch {
    
        private static final Log logger = LogFactory.getLog(NginxRequestDispatchProxyPass.class);
    
        @Override
        public void doDispatch(NginxRequestDispatchContext context) {
            // 原始的请求
            final FullHttpRequest request = context.getRequest();
            final ChannelHandlerContext ctx = context.getCtx();
    
            // 创建一个新的 FullHttpRequest 转发到目标服务器
            FullHttpRequest forwardedRequest = new DefaultFullHttpRequest(
                    request.protocolVersion(), request.method(), request.uri(), request.content().retainedDuplicate());
            forwardedRequest.headers().set(request.headers());
    
            final NginxLoadBalanceConfig nginxLoadBalanceConfig = context.getBalanceConfig();
    
            // 创建一个新的 Bootstrap 进行 HTTP 请求
            Bootstrap b = new Bootstrap();
            b.group(ctx.channel().eventLoop())
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<Channel>() {
                        @Override
                        protected void initChannel(Channel ch) throws Exception {
                            //...
    
                            ch.pipeline().addLast(new SimpleChannelInboundHandler<FullHttpResponse>() {
                                @Override
                                protected void channelRead0(ChannelHandlerContext clientCtx, FullHttpResponse response) throws Exception {
                                    // 将目标服务器的响应写回到客户端
                                    FullHttpResponse clientResponse = new DefaultFullHttpResponse(
                                            response.protocolVersion(), response.status(), response.content().retainedDuplicate());
    
                                    clientResponse.headers().set(response.headers());
    
                                    ctx.writeAndFlush(clientResponse).addListener(ChannelFutureListener.CLOSE);
                                    clientCtx.close();
                                }
    
                                @Override
                                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                                    logger.error("exceptionCaught meet ex", cause);
                                    ctx.close();
                                }
                            });
                        }
                    });
    
            // 连接到目标服务器并发送请求
            final IServer server = getActualServer(nginxLoadBalanceConfig);
            b.connect(server.host(), server.port()).addListener((ChannelFutureListener) future -> {
                if (future.isSuccess()) {
                    future.channel().writeAndFlush(forwardedRequest);
                } else {
                    ctx.close();
                }
            });
        }
    }
    

    负载均衡

    负载均衡策略,可以看我以前的文章:

    从零手写实现 nginx-32-load balance 负载均衡算法 java 实现

    小结

    到这里开始,我们基本实现了反向代理。

    当然,其中还有很多细节需要处理。

    相关文章

      网友评论

          本文标题:从零手写实现 nginx-35-proxy_pass netty

          本文链接:https://www.haomeiwen.com/subject/gfivhjtx.html