Netty实现TCP转发

一、背景

公司网络部分故障,测试环境某些网段到Oracle连接失败,排查修复慢,故整了个TCP链接转发工具,放到好的网段去进行转发,使开发业务流程不中断。

二、引入依赖

<dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.50.Final</version>
        </dependency>

三、实现代码

public class TCPForwardingServer {

    Bootstrap bootstrap;
    ServerBootstrap server;

    NioEventLoopGroup bossGroup;
    NioEventLoopGroup workGroup;

    public static void main(String[] args) {
        TCPForwardingServer TCPForwardingServer = new TCPForwardingServer();
        TCPForwardingServer.init();
    }

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

    }

    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(1521).syncUninterruptibly().addListener((ChannelFutureListener) channelFuture -> {
            if (channelFuture.isSuccess()) {
                System.out.println("forward server start success");
            } else {
                System.out.println("forward server start failed");
            }
        });
    }

    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("目标地址域名/ip", 1521).sync();
        return sync.channel();
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值