单机百万连接时可行的,短板可能是带宽。二进制协议的压缩可以节省带宽,但也会增加开发难度。
操作系统优化
更改进程最大文件句柄数
# 1 048 576为2的20次方,当前shell有效
ulimit -n 1048576
修改单个进程可分配的最大文件数
# 2097152是2的21次方
echo 2097152 > /proc/sys/fs/nr_open
修改/etc/security/limits.conf文件,永久生效
* soft nofile 1048576
* hard nofile 1048576
* soft nproc unlimited
root soft nproc unlimited
配置详解
<domain> <type> <item> <value>
domain
是指生效实体
- 用户名
- 也可以通过@group指定用户组
- 使用*表示默认值
type
指限制类型
- soft软限制
- hard硬限制
item限制资源
- core同ulimit -c
- data同ulimit -d
- fsize同ulimit -f
- memloc同ulimit -l
- nofile同ulimit -n
- stack同ulimit -s
- cpu 同ulimit -t
- nproc同ulimit -u
- maxlogins指定用户可以同时登陆的数量
- maxsyslogins系统可以同时登陆的用户数
- priority用户进程运行的优先级
- locks用户可以锁定的文件最大值
- sigpengding同ulimit -i
- msgqueue同ulimit -q
# /etc/security/limits.conf
#
#This file sets the resource limits for the users logged in via PAM.
#It does not affect resource limits of the system services.
#
#Also note that configuration files in /etc/security/limits.d directory,
#which are read in alphabetical order, override the settings in this
#file in case the domain is the same or more specific.
#That means for example that setting a limit for wildcard domain here
#can be overriden with a wildcard setting in a config file in the
#subdirectory, but a user specific setting here can be overriden only
#with a user specific setting in the subdirectory.
#
#Each line describes a limit for a user in the form:
#
#<domain> <type> <item> <value>
#
#Where:
#<domain> can be:
# - a user name 用户名
# - a group name, with @group syntax 组名
# - the wildcard *, for default entry 默认
# - the wildcard %, can be also used with %group syntax,for maxlogin limit 用于最大登陆限制
#
#<type> can have the two values:
# - "soft" for enforcing the soft limits 软限制
# - "hard" for enforcing hard limits 硬限制
#
#<item> can be one of the following:
# - core - limits the core file size (KB) 限制核心文件大小 (KB)
# - data - max data size (KB) 最大数据大小 (KB)
# - fsize - maximum filesize (KB) 最大文件大小 (KB)
# - memlock - max locked-in-memory address space (KB) 最大锁定内存地址空间 (KB)
# - nofile - max number of open file descriptors 打开文件描述符的最大数量
# - rss - max resident set size (KB) 最大驻留集大小 (KB)
# - stack - max stack size (KB) 最大堆栈大小 (KB)
# - cpu - max CPU time (MIN) 最大 CPU 时间 (MIN)
# - nproc - max number of processes 最大进程数
# - as - address space limit (KB) 地址空间限制 (KB)
# - maxlogins - max number of logins for this user 此用户的最大登录次数
# - maxsyslogins - max number of logins on the system 系统上的最大登录数
# - priority - the priority to run user process with 运行用户进程的优先级
# - locks - max number of file locks the user can hold 用户可以持有的最大文件锁数
# - sigpending - max number of pending signals 挂起信号的最大数量
# - msgqueue - max memory used by POSIX message queues (bytes) POSIX 消息队列使用的最大内存(字节)
# - nice - max nice priority allowed to raise to values: [-20, 19] 允许提升到值的最大优先级:[-20, 19]
# - rtprio - max realtime priority 最大实时优先级
#
#<domain> <type> <item> <value>
#
#* soft core 0
#* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
#@student - maxlogins 4
# End of file
清理掉/etc/security/limits.d/*下的配置
rm -rf /etc/security/limits.d/*
网络优化
打开/etc/sysctl.conf,添加配置
然后执行,使用sysctl生效
#单个进程可分配的最大文件数
fs.nr_open=2097152
#系统最大文件句柄数
fs.file-max = 1048576
#backlog 设置
net.core.somaxconn=32768
net.ipv4.tcp_max_syn_backlog=16384
net.core.netdev_max_backlog=16384
#可用知名端口范围配置
net.ipv4.ip_local_port_range='1000 65535'
#TCP Socket 读写 Buffer 设置
net.core.rmem_default=262144
net.core.wmem_default=262144
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.core.optmem_max=16777216
net.ipv4.tcp_rmem='1024 4096 16777216'
net.ipv4.tcp_wmem='1024 4096 16777216'
#TCP 连接追踪设置
net.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_tcp_timeout_time_wait=30
#TIME-WAIT Socket 最大数量、回收与重用设置
net.ipv4.tcp_max_tw_buckets=1048576
# FIN-WAIT-2 Socket 超时设置
net.ipv4.tcp_fin_timeout = 15
评论区