netty中常见的option、childOption设置

  • ChannelOption.SO_BACKLOG
    Socket参数,服务端接受连接的队列长度,如果队列已满,客户端连接将被拒绝。默认值,Windows为200,其他为128。
    属于 ServerSocketChannal 参数,backlog 大小包括了两个队列的大小
    包括sync queue - 半连接队列和accept queue - 全连接队列,关注后者即可

1.半连接队列
大小通过 /proc/sys/net/ipv4/tcp_max_syn_backlog 指定,在 syncookies 启用的情况下,逻辑上没有最大值限制,这个设置便被忽略

2.全连接队列
其大小通过 /proc/sys/net/core/somaxconn 指定,在使用 listen 函数时,内核会根据传入的 backlog 参数与系统参数,取二者的较小值
如果 accpet queue 队列满了,server 将发送一个拒绝连接的错误信息到 client。netty 中,可以通过 option(ChannelOption.SO_BACKLOG, 值) 来设置大小

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Spring Boot 使用 Netty 的示例: 1. 首先,添加以下依赖到 pom.xml 文件: ```xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.48.Final</version> </dependency> ``` 2. 创建一个 Netty 服务器类,并实现 ChannelInboundHandlerAdapter 接口: ```java @Component @ChannelHandler.Sharable public class NettyServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 处理客户端请求 ByteBuf byteBuf = (ByteBuf) msg; byte[] bytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(bytes); String request = new String(bytes, "UTF-8"); System.out.println("收到客户端请求:" + request); // 响应客户端请求 String response = "Hello, " + request; ByteBuf responseBuf = Unpooled.copiedBuffer(response.getBytes()); ctx.write(responseBuf); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } } ``` 3. 创建 Netty 服务器启动类,并使用 @PostConstruct 注解启动 Netty 服务器: ```java @Component public class NettyServer { private final NettyServerHandler nettyServerHandler; @Autowired public NettyServer(NettyServerHandler nettyServerHandler) { this.nettyServerHandler = nettyServerHandler; } @PostConstruct public void start() throws InterruptedException { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(nettyServerHandler); } }); ChannelFuture future = serverBootstrap.bind(8080).sync(); if (future.isSuccess()) { System.out.println("Netty 服务器启动成功"); } } } ``` 以上示例,我们创建了一个 Netty 服务器,监听 8080 端口。当客户端连接到服务器时,服务器会收到客户端请求,并响应客户端请求。 注意:如果你的 Spring Boot 应用部署在 Tomcat 或者 Jetty 容器,则需要在启动方法上添加 @Bean 注解,以确保正确启动 Netty 服务器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值