008netty中的NioEventLoopGroup

在这里插入图片描述

这个是源码中默认的线程数。

在这里插入图片描述

因为自己的cpu是8核心的,所以是cpu数量的2倍,这个是默认值

    EventLoopGroup bossGroup = new NioEventLoopGroup(2);
        EventLoopGroup workGroup = new NioEventLoopGroup(2);

如果我们这样设置
客户端有4个请求连接会是什么样

nioEventLoopGroup-3-1
ctx = ChannelHandlerContext(Nhandler#0, [id: 0xaee4fec7, L:/127.0.0.1:12312 - R:/127.0.0.1:55988])
client:大哥,大哥,大哥
address:/127.0.0.1:55988
nioEventLoopGroup-3-2
ctx = ChannelHandlerContext(Nhandler#0, [id: 0x231c2480, L:/127.0.0.1:12312 - R:/127.0.0.1:56039])
client:大哥,大哥,大哥
address:/127.0.0.1:56039
nioEventLoopGroup-3-1
ctx = ChannelHandlerContext(Nhandler#0, [id: 0xdc20672e, L:/127.0.0.1:12312 - R:/127.0.0.1:56078])
client:大哥,大哥,大哥
address:/127.0.0.1:56078
nioEventLoopGroup-3-2
ctx = ChannelHandlerContext(Nhandler#0, [id: 0x4e6be1b1, L:/127.0.0.1:12312 - R:/127.0.0.1:56116])
client:大哥,大哥,大哥
address:/127.0.0.1:56116

其实就是轮询。

        System.out.println("ctx = " + ctx);
        System.out.println("ctx.channel() = " + ctx.channel());
        System.out.println("ctx.pipeline() = " + ctx.pipeline());

ctx就是有web的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot集成Netty可以通过以下步骤实现: 1. 添加Netty依赖 在pom.xml文件添加以下依赖: ```xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.25.Final</version> </dependency> ``` 2. 创建Netty服务 创建一个Netty服务类,实现ChannelInboundHandlerAdapter: ```java public class NettyServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("Channel Active"); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("Channel Read"); ctx.writeAndFlush(msg); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { System.out.println("Exception Caught"); cause.printStackTrace(); ctx.close(); } } ``` 3. 创建Netty配置类 创建一个Netty配置类,实现InitializingBean和DisposableBean接口: ```java @Configuration public class NettyConfig implements InitializingBean, DisposableBean { private EventLoopGroup bossGroup = new NioEventLoopGroup(); private EventLoopGroup workerGroup = new NioEventLoopGroup(); private ServerBootstrap serverBootstrap; @Autowired private NettyServerHandler nettyServerHandler; @Override public void afterPropertiesSet() throws Exception { serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(nettyServerHandler); } }); serverBootstrap.bind(8080).sync(); } @Override public void destroy() throws Exception { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } ``` 4. 测试Netty服务 启动Spring Boot应用,访问http://localhost:8080,在控制台可以看到Netty服务已经启动。 参考资料: https://www.baeldung.com/spring-boot-netty
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值