前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Netty 服务转发Tcp请求

Netty 服务转发Tcp请求

原创
作者头像
8菠萝
发布2022-06-29 07:56:38
2.4K0
发布2022-06-29 07:56:38
举报
文章被收录于专栏:菠萝上市没有菠萝上市没有

背景

使用Netty转发Tcp请求。

例子

代码语言:java
复制
import io.netty.bootstrap.Bootstrap;  
import io.netty.bootstrap.ServerBootstrap;  
import io.netty.buffer.ByteBuf;  
import io.netty.channel.*;  
import io.netty.channel.nio.NioEventLoopGroup;  
import io.netty.channel.socket.SocketChannel;  
import io.netty.channel.socket.nio.NioServerSocketChannel;  
import io.netty.channel.socket.nio.NioSocketChannel;  
import io.netty.handler.logging.LogLevel;  
import io.netty.handler.logging.LoggingHandler;  
  
public class AgentProxyServer {  
    Bootstrap bootstrap;  
    ServerBootstrap server;  
  
    NioEventLoopGroup bossGroup;  
    NioEventLoopGroup workGroup;  
  
    class DataHandler extends ChannelInboundHandlerAdapter {  
        private Channel channel;  
  
        public DataHandler(Channel channel) {  
            this.channel = channel;  
        }  
  
        @Override  
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {  
            ByteBuf readBuffer = (ByteBuf) msg;  
            readBuffer.retain();  
            channel.writeAndFlush(readBuffer);  
        }  
    }  
  
    void init() {  
        // 初始化
        this.bossGroup = new NioEventLoopGroup();  
        this.workGroup = new NioEventLoopGroup();  
        this.server = new ServerBootstrap();  
        this.bootstrap = new Bootstrap();  
        bootstrap.channel(NioSocketChannel.class);  
        bootstrap.group(bossGroup);  
        this.server.group(bossGroup, workGroup);  

        
        server.channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))  
                .childHandler(new ChannelInitializer<SocketChannel>(  
                ) {  
                    @Override  
                    protected void initChannel(SocketChannel socketChannel) throws Exception {  
                        socketChannel.pipeline().addLast("serverHandler", new DataHandler(getClientChannel(socketChannel)));  
                    }  
                }).option(ChannelOption.SO_BACKLOG, 1024)  
                .option(ChannelOption.SO_RCVBUF, 16 * 1024);  
  
// 监听地址
server.bind(50050).syncUninterruptibly().addListener((ChannelFutureListener) channelFuture -> {  
            if (channelFuture.isSuccess()) {  
                System.out.println("服务区启动成功");  
            } else {  
                System.out.println("服务器启动失败");  
            }  
        });  
    }  
  
    private Channel getClientChannel(SocketChannel ch) throws InterruptedException {  
        this.bootstrap.handler(new ChannelInitializer<SocketChannel>() {  
            @Override  
            protected void initChannel(SocketChannel socketChannel) throws Exception {  
                socketChannel.pipeline().addLast("clientHandler", new DataHandler(ch));  
            }  
       });  
//  转发地址
        ChannelFuture sync = bootstrap.connect("192.168.20.49", 22).sync();  
        return sync.channel();  
    }  
  
    public static void main(String[] args) {  
        AgentProxyServer agentProxyServer = new AgentProxyServer();  
        agentProxyServer.init();  
    }  
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 例子
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档