• Java:Netty一款异步的、基于事件驱动的网络应用程序框架


    Netty 是一款异步的、基于事件驱动的网络应用程序框架,用以快速开发高性能、高可靠性的网络 IO 程序。

    引入依赖

    <dependency>
        <groupId>io.nettygroupId>
        <artifactId>netty-allartifactId>
        <version>4.1.77.Finalversion>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    服务端

    服务端监听 8081 端口

    package com.example.demo.nettydemo;
    
    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.channel.socket.nio.NioSocketChannel;
    import io.netty.handler.codec.string.StringDecoder;
    
    import java.util.Date;
    
    public class HelloWorldServer {
        public static void main(String[] args) {
            // 创建服务端启动引导器
            ServerBootstrap bootstrap = new ServerBootstrap();
            // 配置线程模型
            bootstrap.group(new NioEventLoopGroup());
            // 指定服务端的 IO 模型
            bootstrap.channel(NioServerSocketChannel.class);
            // 定义处理器 Handler
            bootstrap.childHandler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(NioSocketChannel channel) throws Exception {
                    // 解码
                    channel.pipeline().addLast(new StringDecoder());
    
                    channel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            System.out.println(ctx.channel() + ",hello world");
                        }
    
                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            System.out.println(new Date() + ":" + msg);
                        }
                    });
                }
            });
    
            // 绑定 8081 端口
            bootstrap.bind(8081);
        }
    }
    
    
    • 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

    客户端

    客户端每隔5秒给服务端发送一条信息

    package com.example.demo.nettydemo;
    
    import io.netty.bootstrap.Bootstrap;
    import io.netty.channel.Channel;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioSocketChannel;
    import io.netty.handler.codec.string.StringEncoder;
    
    import java.util.concurrent.TimeUnit;
    
    public class HelloWorldClient {
        public static void main(String[] args) throws InterruptedException {
            // 客户端引导器
            Bootstrap bootstrap = new Bootstrap();
            // 配置线程组
            bootstrap.group(new NioEventLoopGroup());
            // 指定 IO 类型为 NIO
            bootstrap.channel(NioSocketChannel.class);
            // 配置 IO 处理器
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new StringEncoder());
                }
            });
            
            // 建立连接
            Channel channel = bootstrap.connect("127.0.0.1",8081).channel();
            // 发送消息
            while (true) {
                channel.writeAndFlush("hello world..");
                TimeUnit.SECONDS.sleep(5);
            }
        }
    }
    
    
    • 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

    在这里插入图片描述

    参考文章

    1. Netty 入门 — 亘古不变的Hello World
  • 相关阅读:
    Boundary Loss 原理与代码解析
    手机玻璃盖板为什么需要透光率检测
    【Java SE】“方法”论 — 《方法的重载与递归》
    微信小程序一对多个页面间传递数据进行通信,事件触发的实现方法
    Flutter实战-请求封装(五)之Isolate线程改造
    git推送错误:Failed to connect to github.com port 443
    10道高频webpack面试题快问快答
    DirectX12初始化三——DirectX图形基础结构,功能支持检测,资源驻留
    初识Golang的语法、特性
    windows安装多版本python和切换默认版本
  • 原文地址:https://blog.csdn.net/mouday/article/details/133807422