当前位置: 首页 > article >正文

Netty的线程模型

文章目录

  • Netty的线程模型
    • 什么是线程模型
    • 线程模型实践
      • 开发环境
      • 服务器代码
      • 服务端处理器
      • 测试服务器

Netty的线程模型

Netty是一个高性能、异步事件驱动的网络编程框架。它提供了一个基于NIO的抽象层,使得开发者可以轻松地构建可伸缩、可扩展的网络应用。

在Netty中,线程模型是一个重要的概念。它定义了Netty如何处理网络请求和响应,并且影响着应用程序的可伸缩性和性能。

什么是线程模型

线程模型指的是Netty在处理网络I/O事件时使用的线程池模型。它包括两个方面:线程池类型和线程池大小。

线程池类型有三种:

  • 单线程池模型:所有的I/O操作都由一个线程来处理。
  • 多线程池模型:所有的I/O操作都由多个线程来处理,每个线程都有自己的事件循环。
  • 主从多线程池模型:一个线程池用于处理连接请求,另一个线程池用于处理I/O操作。这种模型可以减少连接请求处理对I/O操作的干扰,从而提高系统并发性能。

线程池大小根据应用程序的负载情况进行调整。主要有两种方式:

  • 固定线程池:固定线程池适用于负载比较稳定的场景,可以预先设置线程池大小来保证系统的性能稳定。
  • 弹性线程池:弹性线程池适用于负载波动较大的场景,可以根据系统的负载情况动态地调整线程池的大小。

线程模型实践

接下来,我们将演示如何使用Netty的线程模型来开发一个简单的Echo服务器。Echo服务器会对客户端发送的消息进行回复。

开发环境

  • JDK 1.8
  • Netty 4.1.63.Final

服务器代码

public class EchoServer {

    private final int port;
    private final EventLoopGroup group;

    public EchoServer(int port) {
        this.port = port;
        this.group = new NioEventLoopGroup();
    }

    public void run() throws InterruptedException {
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(group)
             .channel(NioServerSocketChannel.class)
             .localAddress(new InetSocketAddress(port))
             .childHandler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel ch) throws Exception {
                     ch.pipeline().addLast(new EchoServerHandler());
                 }
             });

            ChannelFuture f = b.bind().sync();
            System.out.println("EchoServer started and listening on " + f.channel().localAddress());

            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println("Usage: " + EchoServer.class.getSimpleName() + " <port>");
            return;
        }

        int port = Integer.parseInt(args[0]);
        new EchoServer(port).run();
    }
}

服务端处理器

public class EchoServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf in = (ByteBuf) msg;
        System.out.println("Server received: " + in.toString(CharsetUtil.UTF_8));
        ctx.write(in);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
            .addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}

测试服务器

使用telnet命令测试Echo服务器的运行情况。

$ telnet localhost 8080
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Hello World!
Hello World!
Connection closed by foreign host

通过上述代码,我们实现了一个简单的Echo服务器,并使用了Netty的线程模型。在这个例子中,我们使用了默认的多线程池模型和固定线程池大小。

除此之外,Netty还提供了一个主从多线程模型,可以通过创建两个EventLoopGroup来实现:

public class EchoServer {
    private final int port;
    private final EventLoopGroup bossGroup;
    private final EventLoopGroup workerGroup;

    public EchoServer(int port) {
        this.port = port;
        this.bossGroup = new NioEventLoopGroup(1);
        this.workerGroup = new NioEventLoopGroup();
    }

    public void run() throws InterruptedException {
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .localAddress(new InetSocketAddress(port))
             .childHandler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel ch) throws Exception {
                     ch.pipeline().addLast(new EchoServerHandler());
                 }
             });

            ChannelFuture f = b.bind().sync();
            System.out.println("EchoServer started and listening on " + f.channel().localAddress());

            f.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

// ...
}

以上就是Netty线程模型的简单介绍和实践。当然,线程模型不是一成不变的,需要根据应用程序的负载情况进行调整。有了合适的线程模型,可以提高系统的性能和可伸缩性。


http://www.kler.cn/news/10378.html

相关文章:

  • 这样也行,在lambda表达式中优雅的处理checked exception
  • 大数据系列——Hive理论
  • Faster-RCNN代码解读2:快速上手使用
  • 【数据结构与算法】栈和队列(StackQueue)
  • 【BAT】查询局域内所有的IP
  • 2023 年十大 API 管理趋势
  • 基于matlab进行雷达信号模拟
  • 朝花夕拾 - 卷王的自白(光头祭天,法力无边》
  • 独立看门狗(IWDG)实验
  • 第十三届蓝桥杯大赛软件赛省赛 C/C++ 大学 B 组思考+总结
  • ChatGPT宝藏插件丨装上之后,上网、语音聊天、一键分享对话……简直让你爽到起飞!
  • Java题目训练——年终奖和迷宫问题
  • 自己再造一个大规模预训练语言模型?可以的
  • 【C++】Vector
  • 软件测试培训
  • JUC源码系列-AQS的Condition的接口实现
  • 程序员在职场中如何让自己的技能快速提高,WEB前端开发工程师如何让自己快速成为团队的核心开发人员?
  • Follow My Heart Of Apirl. 2023
  • LAZADA平台开放接口的接入和参数说明(目前支持以下基本接口:item_get 获得淘宝商品详情item_search 获得淘宝商品详情)
  • 为何ChatGPT如此擅长编造故事?
  • 操作系统 - 学习笔记
  • 不一样的websocket封装简洁版
  • 设计模式之迭代器模式(C++)
  • 新品国产C2000,独立双核32位CPU,主频高达400MHz,QX320F280049
  • 数据分析-统计基础
  • 29岁,普通功能测试,我是如何在一周内拿到5份Offer的?
  • Linux 内存回收,思维导图记录
  • CMake项目使用ctest+gtest进行单元测试
  • Vulnhub:Digitalworld.local (Development)靶机
  • C++ 23 实用工具(二)绑定工具