【HBZ分享】Netty的启动引导类Bootstrap

什么是ServerBootstrap

  1. 是Netty的启动引导类,作用是注入所需要的参数

  2. 设置Channel类型, BIO 或 OIO等,每个Channel就是一个TCP连接

  3. 初始化子通道childHandler,即接收一个连接后,对这个连接的处理

  4. 配置option参数,作用于每新建一个通道Channel。设置TCP连接中的一些参数,比如常用的有
    ==ChannelOption.SO_BACKLOG: ==存放【已完成】的三次握手请求的队列最大长度,如果SO_BACKLOG配置比SOMAXCONN大,则会以SOMAXCONN为准,所以SO_BACKLOG要小于SOMAXCONN,是TCP全连接队列数

    ==ChannelOption.TCP_NODELAY: ==解决Nagle算法问题,即TCP每次发送的请求包大小,默认为false,即TPC请求包到来后累计到一定大小后一起发送。如果设置为true,则是追求高实时性,有数据立刻发送

  5. remoteAddress(客户端的参数): 配置要连接的服务端ip,port, 与服务端进行连接

服务端源码:

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class EchoServer {

    private int port;
    public EchoServer(int port){
        this.port = port;

    }

    public void run() throws InterruptedException {
        // 配置服务端线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workGroup)

                    .channel(NioServerSocketChannel.class)

                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            // 收到客户端内容,并处理逻辑在这里
                            // 直接使用EchoServerHandler自定义类的处理逻辑
                            socketChannel.pipeline().addLast(new EchoServerHandler());
                        }
                    });

            System.out.println("服务端启动ing");

            // 绑定端口,同步等待成功
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();

            // 等待服务端监听端口关闭
            channelFuture.channel().closeFuture().sync();
        }finally {

            // 关闭线程组
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }



    }
    public static void main(String[] args) throws InterruptedException {

        int port = 8080;
        if(args != null && args.length > 0){
            port = Integer.parseInt(args[0]);
        }

        new EchoServer(port).run();


    }
}

客户端源码:

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

public class EchoClient {

    private String host;

    private Integer port;

    public EchoClient(String host, Integer port){
        this.host = host;
        this.port = port;
    }

    public void start() throws InterruptedException {

        EventLoopGroup eventExecutors = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventExecutors)

                    .channel(NioSocketChannel.class)

                    .remoteAddress(new InetSocketAddress(host, port))
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new EchoClientHandler());
                        }
                    });
            System.out.println("服务端启动ing");

            // 绑定端口,同步等待成功
            ChannelFuture channelFuture = bootstrap.connect().sync();

            // 等待服务端监听端口关闭
            channelFuture.channel().closeFuture().sync();
        }finally {

            // 关闭线程组
            eventExecutors.shutdownGracefully();
        }

    }

    public static void main(String[] args) throws InterruptedException {

        new EchoClient("127.0.0.1", 8080).start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值