• 【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() {
                            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();
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    客户端源码:

    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() {
                            @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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
  • 相关阅读:
    FS2119A同步升压IC输出3.3V和FS2119B同步升压IC输出5V
    基于数学形态学的路面裂缝图像处理技术-含Matlab代码
    Qt QTextEdit 设置 QScrollBar 样式表不生效解决方案
    使用Unity的Input.GetAxis(““)控制物体移动、旋转
    【技术积累】HTML+CSS+JavaScript中的基础知识【四】
    Mybatis-plus使用update()/updateById()将字段更新为null或者空值时候不起作用
    MySQL子查询
    【论文导读】 - 关于联邦图神经网络的3篇文章
    Redis中常见数据结构和数据类型
    bootz 启动 kernel
  • 原文地址:https://blog.csdn.net/a645293829/article/details/126943473