linux netty udp服务端,Linux下Netty实现高性能UDP服务(SO_REUSEPORT)

参考:

https://www.jianshu.com/p/61df929aa98b

SO_REUSEPORT学习笔记:http://www.blogjava.net/yongboy/archive/2015/02/12/422893.html

代码示例:https://www.programcreek.com/java-api-examples/index.php?api=io.netty.channel.epoll.EpollDatagramChannel

Linux下UDP丢包问题分析思路:https://www.jianshu.com/p/22b0f89937ef

当前Linux网络应用程序问题

运行在Linux系统上网络应用程序,为了利用多核的优势,一般使用以下比较典型的多进程/多线程服务器模型:

单线程listen/accept,多个工作线程接收任务分发,虽CPU的工作负载不再是问题,但会存在:

单线程listener,在处理高速率海量连接时,一样会成为瓶颈

CPU缓存行丢失套接字结构(socket structure)现象严重

所有工作线程都accept()在同一个服务器套接字上呢,一样存在问题:

多线程访问server socket锁竞争严重

高负载下,线程之间处理不均衡,有时高达3:1不均衡比例

导致CPU缓存行跳跃(cache line bouncing)

在繁忙CPU上存在较大延迟

上面模型虽然可以做到线程和CPU核绑定,但都会存在:

单一listener工作线程在高速的连接接入处理时会成为瓶颈

缓存行跳跃

很难做到CPU之间的负载均衡

随着核数的扩展,性能并没有随着提升

SO_REUSEPORT解决了什么问题

linux man文档中一段文字描述其作用:

The new socket option allows multiple sockets on the same host to bind to the same port, and is intended to improve the performance of multithreaded network server applications running on top of multicore systems.

SO_REUSEPORT支持多个进程或者线程绑定到同一端口,提高服务器程序的性能,解决的问题:

允许多个套接字 bind()/listen() 同一个TCP/UDP端口

每一个线程拥有自己的服务器套接字

在服务器套接字上没有了锁的竞争

内核层面实现负载均衡

安全层面,监听同一个端口的套接字只能位于同一个用户下面

其核心的实现主要有三点:

扩展 socket option,增加 SO_REUSEPORT 选项,用来设置 reuseport。

修改 bind 系统调用实现,以便支持可以绑定到相同的 IP 和端口

修改处理新建连接的实现,查找 listener 的时候,能够支持在监听相同 IP 和端口的多个 sock 之间均衡选择。

Netty使用SO_REUSEPORT

要想在Netty中使用SO_REUSEPORT特性,需要满足以下两个前提条件

linux内核版本 >= 3.9

Netty版本 >= 4.0.16

替换Netty中的Nio组件为原生组件

直接在Netty启动类中替换为在Linux系统下的epoll组件

NioEventLoopGroup → EpollEventLoopGroup

NioEventLoop → EpollEventLoop

NioServerSocketChannel → EpollServerSocketChannel

NioSocketChannel → EpollSocketChannel

如下所示:

group = new EpollEventLoopGroup();//NioEventLoopGroup ->EpollEventLoopGroup

bootstrap = newBootstrap();

bootstrap.group(group)

.channel(EpollDatagramChannel.class) //NioServerSocketChannel -> EpollDatagramChannel

.option(ChannelOption.SO_BROADCAST, true)

.option(EpollChannelOption.SO_REUSEPORT,true) //配置EpollChannelOption.SO_REUSEPORT

.option(ChannelOption.SO_RCVBUF, 1024 * 1024 *bufferSize)

.handler(new ChannelInitializer() {

@Overrideprotected voidinitChannel(Channel channel)throwsException {

ChannelPipeline pipeline=channel.pipeline();//....

}

});

netty提供了方法Epoll.isAvailable()来判断是否可用epoll

多线程绑定同一个端口

使用原生epoll组件替换nio原来的组件后,需要多次绑定同一个端口。

if(Epoll.isAvailable()) {//linux系统下使用SO_REUSEPORT特性,使得多个线程绑定同一个端口

int cpuNum =Runtime.getRuntime().availableProcessors();

log.info("using epoll reuseport and cpu:" +cpuNum);for (int i = 0; i < cpuNum; i++) {

ChannelFuture future=bootstrap.bind(UDP_PORT).await();if (!future.isSuccess()) {throw new Exception("bootstrap bind fail port is " +UDP_PORT);

}

}

}

更多例子:https://www.programcreek.com/java-api-examples/index.php?api=io.netty.channel.epoll.EpollDatagramChannel

也可以参考:https://github.com/netty/netty/issues/1706

Bootstrap bootstrap = newBootstrap()

.group(new EpollEventLoopGroup(5))

.channel(EpollDatagramChannel.class)

.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)

.option(EpollChannelOption.SO_REUSEPORT,true)

.handler(channelInitializer);

ChannelFuture future;for(int i = 0; i < 5; ++i) {

future=bootstrap.bind(host, port).await();if(!future.isSuccess())throw new Exception(String.format("Fail to bind on [host = %s , port = %d].", host, port), future.cause());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值