• netty应用实践


    netty实现通信的步骤

    1、创建两个NIO线程组,一个用于接受客户端连接,另一个处理网络读写。
    2、创建一个ServerBootStrap,配置netty的一些列参数,例如接收传出数据的缓存大小等。
    3、创建一个实际处理数据的类ChannelInitializer,进行初始化的准备工作,比如设置传出数据的字符集、格式、已经实际处理数据的接口。
    4、绑定端口,执行同步阻塞方法等待服务器端启动即可。

        //服务端启动助手
        public static void startServer(String ip, int port) throws InterruptedException {
            //1.创建两个线程池对象
            NioEventLoopGroup bossGroup = new NioEventLoopGroup();
            NioEventLoopGroup workGroup = new NioEventLoopGroup();
    
            //2.创建服务端的启动引导对象
            ServerBootstrap serverBootstrap = new ServerBootstrap();
    
            //3.配置启动引导对象
            serverBootstrap.group(bossGroup, workGroup)
                    //设置通道为NIO
                    .channel(NioServerSocketChannel.class)
    
                    //创建监听channel
                    .childHandler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                            //获取管道对象
                            ChannelPipeline pipeline = nioSocketChannel.pipeline();
                            //给管道对象pipeLine 设置编码
                            pipeline.addLast(new StringEncoder());
                            pipeline.addLast(new StringDecoder());
                           // pipeline.addLast(new RpcDecoder(RpcRequest.class, new JSONSerializer()));
    
                            //把我们自顶一个ChannelHander添加到通道中
                            pipeline.addLast(new UserServiceHandler());
                        }
                    });
    
            //4.绑定端口
            serverBootstrap.bind(8999).sync();
        }
    
    • 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
    //客户端启动助手
     public static void initClient() throws InterruptedException {
            //1) 初始化UserClientHandler
            userClientHandler  = new UserClientHandler();
            //2)创建连接池对象
            EventLoopGroup group = new NioEventLoopGroup();
            //3)创建客户端的引导对象
            Bootstrap bootstrap =  new Bootstrap();
            //4)配置启动引导对象
            bootstrap.group(group)
                    //设置通道为NIO
                    .channel(NioSocketChannel.class)
                    //设置请求协议为TCP
                    .option(ChannelOption.TCP_NODELAY,true)
                    //监听channel 并初始化
                    .handler(new ChannelInitializer() {
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            //获取ChannelPipeline
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            //设置编码
    						pipeline.addLast(new StringEncoder());
                            //pipeline.addLast(new RpcEncoder(RpcRequest.class,new JSONSerializer()));
                            pipeline.addLast(new StringDecoder());
                            //添加自定义事件处理器
                            pipeline.addLast(userClientHandler);
                        }
                    });
    
            //5)连接服务端
            bootstrap.connect("127.0.0.1",8999).sync();
        }
    
    • 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
  • 相关阅读:
    读取服务器文件,并进行移动-ChannelSftp
    基于C语言实现一个社交系统
    【算法】欧拉筛 输出任意范围内的质数
    存储器IP核与DDS信号发生
    OpenCV之ellipse函数
    SLAM从入门到精通(ROS网络通信)
    内链外链抓取生成sitemap和主动推送的技巧
    阿里云 OSS
    JAVA项目点赞功能如何实现?如何利用缓存优化?如何防止刷赞?
    【Centos7.9通过systemctl启动zookeeper和kafka失败】
  • 原文地址:https://blog.csdn.net/chang_li/article/details/127746443