0 序言

  • 近期工作在搞压力测试,我负责开发维护的、基于sring-cloud-gateway大数据网关微服务,其底层是基于spring-webflux-->reactor-netty-->netty
  • 在压测过程中(200并发),发现大数据网关屡报ConnectException: finishConnect(..) failed: Connection refused错误。
Caused by: java.net.ConnectException: finishConnect(..) failed: Connection refused
at io.netty.channel.unix.Errors.newConnectException0(Errors.java:155) ~[netty-transport-native-unix-common-4.1.65.Final.jar!/:4.1.65.Final]
at io.netty.channel.unix.Errors.handleConnectErrno(Errors.java:128) ~[netty-transport-native-unix-common-4.1.65.Final.jar!/:4.1.65.Final]
at io.netty.channel.unix.Socket.finishConnect(Socket.java:278) ~[netty-transport-native-unix-common-4.1.65.Final.jar!/:4.1.65.Final]
at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.doFinishConnect(AbstractEpollChannel.java:710) [netty-transport-native-epoll-4.1.65.Final-linux-x86_64.jar!/:4.1.65.Final]

关键行:io.netty.channel.unix.Socket.finishConnect(Socket.java:278) ~[netty-transport-native-unix-common-4.1.65.Final.jar!/:4.1.65.Final]

  • 经过长时间、大量的分析(Debug spring-cloud-gateway 源码、观测服务所属主机资源),倾向于本微服务所属主机的net.core.somaxconn不足这种原因。

博主的做法:128 --> 1024

1 参数定义

  • net.core.somaxconn
  • Linux 操作系统全局参数,每个 TCP连接 监听端口的队列长度

somaxconn - INTEGER

  • Limit of socket listen() backlog,在用户空间中称为 SOMAXCONN。
  • 默认值为 4096。(在 linux-128.5 之前是 4)
  • 另请参阅tcp_max_syn_backlog,了解 TCP 套接字的其他调整。
  • CentOS/RHEL 7.9 中,默认值为128,对于高并发场景都建议调大该值。
  • 在RHEL 6和RHEL 7中,socket结构的sk_max_ack_backlog字段被定义为无符号短整型,它将值限制为16位,最大值为65535。
  • 在 RHEL 8 中,socket结构的sk_max_ack_backlog字段定义为 u32,它将值限制为 32 位,最大值为 2147483647。

2 查看方法

2.1 Linux

  • 方法1
cat /proc/sys/net/core/somaxconn
  • 方法2
sysctl -a | grep somaxconn

3 不同应用的配置建议

3.1 Oracle WebLogic Server

https://docs.oracle.com/communications/E96856-01/doc.74/e96850/installing-and-configuring-weblogic-server-cluster1.htm#OSMIG-GUID-87BEF43B-4673-4062-8451-228E436D421D

  • 客户端数:设置 somaxconn 应至少设置为 1024,以 允许大量客户端服务器连接。

排队的数据包数:将netdev_max_backlog设置为至少 32768,以最大程度地减少数据包丢失。

3.2 Apache Zookeeper

Configurable listen socket backlog for the client port - issues.apache.org/zookeeper

在 Linux 上,以下参数: net.core.somaxconn 需要大于“客户端端口积压工作”以上才能正确配置侦听套接字积压工作

3.3 TiDB

https://blog.csdn.net/weixin_43700866/articTiDBle/details/125667286

内核参数 - pingcap.com

  • 内核参数
检查各项内核参数的值:

`net.core.somaxconn: 32768`

3.4 Nginx

Tuning the Operating System - Nginx.com

net.core.somaxconn 内核参数的值从其默认值 (128) 增加到足以容纳大量流量突发的值。在此示例中,它增加到 4096

sudo sysctl -w net.core.somaxconn=4096
...

3.5 Netty

推荐文献

Caused by: io.netty.channel.ChannelException: Unable to create Channel from class class io.netty.channel.socket.nio.NioSocketChannel
...

优化建议

  • 在面对高并发场景下时,应当适当增大BACKLOG的值,使端口可以同时建立更多的TCP请求
  • 当服务的QPS不高,且服务端进程响应请求的时间较长时,可以适当减小BACKLOG的值,避免大量连接占据系统内存资源
  • 在设置BACKLOG参数时,还需要记得修改somaxconn参数,若设置的BACKLOG参数超过了系统somaxconn参数的值时,则无法生效。

netty 源码

netty : 4.1.65

  • 默认: ChannelOption.SO_BACKLOG = io.netty.util.NetUtil#SOMAXCONN = /proc/sys/net/core/somaxconn(Windows中默认值:200,Linux中默认值:128)
io.netty.util.NetUtil
SOMAXCONN
https://github.com/netty/netty/issues/4936
/proc/sys/net/core/somaxconn io.netty.channel.epoll.EpollServerChannelConfig extends EpollChannelConfig implements ServerSocketChannelConfig
private volatile int backlog = NetUtil.SOMAXCONN; D:/Program_Data/maven_repository/io/netty/netty-common/4.1.65.Final/netty-common-4.1.65.Final-sources.jar!/io/netty/util/NetUtil.java:134
io.netty.util.NetUtil#SOMAXCONN
static {
...
int somaxconn = PlatformDependent.isWindows() ? 200 : 128;
File file = new File("/proc/sys/net/core/somaxconn");
...
} //io.netty.util.internal.logging.InternalLogger | 如何将 netty 日志开关打开?

如下是:gateway-service [Debug 源码分析] 以本地电脑(8 core cpu)在启动后的1次请求调用为例(与 somaxcon 无关,仅是记录;NioEventLoop 取决于 CPU 核数)

io.netty.bootstrap.Bootstrap#config

reactor.netty.resources.DefaultLoopResources#cacheNioServerLoops

D:/Program_Data/maven_repository/io/projectreactor/netty/reactor-netty/0.9.20.RELEASE/reactor-netty-0.9.20.RELEASE-sources.jar!/reactor/netty/resources/DefaultLoopResources.java:195
reactor.netty.resources.DefaultLoopResources#cacheNioServerLoops
watch reactor.netty.resources.DefaultLoopResources cacheNioServerLoops returnObj
io.netty.channel.nio.NioEventLoopGroup
io.netty.channel.nio.NioEventLoop



4 配置方法

4.1.临时生效

sysctl -w net.core.somaxconn=10240

4.2.永久生效

echo "net.core.somaxconn = 10240" >>/etc/sysctl.conf
sysctl -p

X 参考文献

/**
* *******************************************************************
* 如果不设置超时,连接会一直占用本地线程,端口,连接客户端一多,阻塞在那里,会导致本地端口用尽及CPU压力
*/
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000); 未设置超时:
30
io.netty.channel.ConnectTimeoutException: connection timed out: /192.168.1.1:8866
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:206)
at io.netty.util.concurrent.PromiseTask$RunnableAdapter.call(PromiseTask.java:38)
at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:123)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:354)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
at java.lang.Thread.run(Thread.java:745)
Process finished with exit code 0 设置后:
5
io.netty.channel.ConnectTimeoutException: connection timed out: /192.168.1.1:8866
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:206)
at io.netty.util.concurrent.PromiseTask$RunnableAdapter.call(PromiseTask.java:38)
at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:123)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:354)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
at java.lang.Thread.run(Thread.java:745)
Process finished with exit code 0

[OS/Linux] Linux核心参数:net.core.somaxconn(高并发场景核心参数)的更多相关文章

  1. linux中对socket的理解 socket高并发

    1.socket是什么? 其实准确的来说,socket并不仅仅用于linux而已,它也应用于TCP/IP中.笼统的来说,socket就是指的“IP地址+端口号”.比如我有一个ssh服务器A,这时候我有 ...

  2. Linux下配置tomcat+apr+native应对高并发

    摘要:在慢速网络上Tomcat线程数开到300以上的水平,不配APR,基本上300个线程狠快就会用满,以后的请求就只好等待.但是配上APR之后,Tomcat将以JNI的形式调用Apache HTTP服 ...

  3. (转)Linux下配置tomcat+apr+native应对高并发

    摘要:在慢速网络上Tomcat线程数开到300以上的水平,不配APR,基本上300个线程狠快就会用满,以后的请求就只好等待.但是配上APR之后,Tomcat将以JNI的形式调用Apache HTTP服 ...

  4. [svc]高并发场景 LVS DR +KeepAlive高可用实现及ka的persistence_timeout参数

    LVS-DR+keepalived模式是一种非常经典的常用生产组合 高可用场景及LVS架构 一般都用一(负载)拖多(Server Array)方式 使用LVS架设的服务器集群系统有三个部分组成: (1 ...

  5. LVS : Linux Virtual Server 负载均衡,集群,高并发,robust

    1 LVS : Linux Virtual Server http://www.linuxvirtualserver.org/ http://www.linuxvirtualserver.org/zh ...

  6. linux系统多网卡热备实现高并发负载均衡

    #nmcli实现bonding #先停止NetworkManagerservice NetworkManager stop chkconfig NetworkManager off   //开机自启动 ...

  7. tomcat高并发优化的参数优化并查看tomcat线程数

    在Tomcat配置文件conf下面 server.xml 中的配置中和连接数相关的参数有: minProcessors:最小空闲连接线程数,用于提高系统处理性能,默认值为10 maxProcessor ...

  8. net.core.somaxconn net.ipv4.tcp_max_syn_backlog

    Linux参数-net.core.somaxconn与net.ipv4.tcp_max_syn_backlog_梁海江的博客-CSDN博客_net.ipv4.tcp_max_syn_backlog h ...

  9. Linux下解决高并发socket最大连接数所受的各种限制(解除IO限制)

    linux作为服务器系统,当运行高并发TCP程序时,通常会出现连接建立到一定个数后不能再建立连接的情况 本人在工作时,测试高并发tcp程序(GPS服务器端程序),多次测试,发现每次连接建立到3800左 ...

  10. Linux系统优化实现高并发

    ulimit -SHn 65535内核优化net.ipv4.ip_forward = 1            #开启路由功能net.ipv4.conf.default.rp_filter = 1   ...

随机推荐

  1. 基于element-ui进行二次封装的表格组件

    <!-- * @description 表格组件 * @fileName TableList.vue * @authorQ * @date 2021/05/15 15:13:45 --> ...

  2. vulnhub靶场之HARRYPOTTER: ARAGOG (1.0.2)

    准备: 攻击机:虚拟机kali.本机win10. 靶机:HarryPotter: Aragog (1.0.2),下载地址:https://download.vulnhub.com/harrypotte ...

  3. day01-2-依赖管理和自动配置

    依赖管理和自动配置 1.依赖管理 1.1什么是依赖管理 spring-boot-starter-parent 中还有父项目,声明了开发中常用的依赖的版本号 并且进行自动版本仲裁,即如果程序员没有指定某 ...

  4. 基于springboot实现SSM整合

    (1)SpringBoot整合Spring(不存在) (2)SpringBoot整合SpringMVC(不存在) (3)SpringBoot整合MyBatis(主要) 一.新建springboot项目 ...

  5. ByteHouse:基于 ClickHouse 的实时计算能力升级

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 ByteHouse 是火山引擎数智平台旗下云原生数据分析平台,为用户带来极速分析体验,能够支撑实时数据分析和海量离 ...

  6. 在Vue3+TypeScript 前端项目中使用事件总线Mitt

    事件总线Mitt使用非常简单,本篇随笔介绍在Vue3+TypeScript 前端项目中使用的一些场景和思路.我们在Vue 的项目中,经常会通过emits 触发事件来通知组件或者页面进行相应的处理,不过 ...

  7. Vue2模版编译(AST、Optimize 、Render)

    在Vue $mount过程中,我们需要把模版编译成render函数,整体实现可以分为三部分: parse:解析模版 template生成 AST语法树 optimize: 优化 AST语法树,标记静态 ...

  8. CTF-NEFU校赛-题解

    Write by NEFUNSI: ghosin 0ERROR 签到 signin 下载 signin.txt 打开得到一串 base64,解码得到 flag{we1come_t0_NEFUCTF!} ...

  9. Win系统下实现任意exe静态免杀

    Win系统下实现任意exe静态免杀?很简单 近几天用C++写了个远控,发现生成出来的exe都会被识别,可能是有人和我写的代码差不多,细想了一下,可能只有静态过不了,动态应该是可以过的,毕竟不可能巧到流 ...

  10. 聊聊spring中bean的作用域

    前言 今天分享一下spring bean的作用域,理解bean的作用域能够在使用过程中避免一些问题,bean的作用域也是spring bean创建过程中一个重要的点. Spring bean的作用域类 ...