1.服务器
IDC机房提供的物理机:单机 40核,64G内存,带宽100M,最后带宽增加至300M
2.redis 7.2配置
timeout=600 #空闲连接超时时间,0表示不断开
maxclients=100000 #最大连接数
3.Mysql 5.7配置,按宝塔16-32G优化方案并调整:
table_open_cache = 4096
max_connections = 15000
4.Nginx 1.24配置
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 1000000;
multi_accept on;
}
5.php 7.2配置
php.ini:memory_limit=30720M
tp6: config/swoole.php配置server:
php
//配置选项:https://geekdaxue.co/read/swoole-wiki/doc-2.3-Configuration-Options.md
'server' => [
'host' => env('SWOOLE_HOST', '0.0.0.0'), // 监听地址
'port' => env('SWOOLE_PORT', 8324), // 监听端口
'mode' => SWOOLE_PROCESS, // 运行模式 默认为SWOOLE_PROCESS
'sock_type' => SWOOLE_SOCK_TCP, // sock type 默认为SWOOLE_SOCK_TCP
'options' => [
'pid_file' => runtime_path() . 'swoole.pid',
'log_file' => runtime_path() . 'swoole.log',
'daemonize' => false,
// Normally this value should be 1~4 times larger according to your cpu cores.
'reactor_num' => swoole_cpu_num(),
'worker_num' => swoole_cpu_num(),
'task_worker_num' => swoole_cpu_num(),
'task_enable_coroutine' => false,
'task_max_request' => 20000, //修改了这里
'enable_static_handler' => true,
'document_root' => root_path('public'),
'package_max_length' => 50 * 1024 * 1024,
'buffer_output_size' => 10 * 1024 * 1024,
'socket_buffer_size' => 128 * 1024 * 1024,
'max_request' => 30000, //修改了这里
'max_connection' => 30000, //修改了这里
'send_yield' => true,
'reload_async' => true,
],
],
//连接池
'pool' => [
//数据库
'db' => [
'enable' => true,
'max_active' => 10, //修改了这里 - 原来是3,最大不超32
'max_wait_time' => 5,
],
//缓存
'cache' => [
'enable' => true,
'max_active' => 10, //修改了这里 - 原来是3,最大不超32
'max_wait_time' => 5,
],
],
6.进程管理器 supervisor 配置
bash
[supervisord]
minfds=1000000
minprocs=50000
stopwaitsecs=60 #你也可以调的更大
7.解决 Swoole 服务报错 Too many open files 文件句柄超出系统限制
- 1.在 Swoole 的日志中遇到了 Too many open files 这种报错,说明你的程序以达到 Linux 所允许的打开文件数上限。需要修改 ulimit 设置,修改 /etc/security/limits.conf,加入
bash
* soft nofile 262140
* hard nofile 262140
root soft nofile 262140
root hard nofile 262140
* soft core unlimited
* hard core unlimited
root soft core unlimited
root hard core unlimited
需要重启系统生效
bash
或者:ulimit -n 1000000 对当前命令窗口有效
- 2.# 将 PID 修改为你要检查的进程ID
bash
cat /proc/PID/limits
如果这里的Max open files过小,也是需要进行修改的
- 3.systemd 需要修改LimitNOFILE,修改systemd全局limit:
/etc/systemd/system.conf
8.Swoole优化内核参数调整,修改文件/etc/sysctl.conf:
bash
vm.overcommit_memory = 1
net.core.somaxconn = 99999
net.unix.max_dgram_qlen = 10000
net.ipv4.tcp_mem = 379008 505344 758016
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.tcp_rmem = 4096 87380 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fin_timeout = 60
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 81920
net.ipv4.tcp_synack_retries = 3
net.ipv4.tcp_syn_retries = 3
net.ipv4.tcp_keepalive_time = 300
net.ipv4.ip_local_port_range = 20000 65000
net.ipv4.tcp_max_tw_buckets = 200000
net.ipv4.route.max_size = 5242880
kernel.msgmnb = 42035200
kernel.msgmni = 99999
kernel.msgmax = 819200
net.local.dgram.maxdgram = 819200
net.local.dgram.recvspace = 200000
kernel.core_pattern = /data/core_files/core-%e-%p-%t
内核参数调整方案2:
bash
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
vm.swappiness = 0
net.ipv4.neigh.default.gc_stale_time=120
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_announce=2
#net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
kernel.sysrq = 1
#优化
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 10000 65535
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_keepalive_intvl = 10
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_retries2=3
net.ipv4.tcp_orphan_retries=2
需执行sysctl -p命令使配置生效或重启服务器,参考:https://www.bookstack.cn/read/SwooleDoc/10.md
9.ab工具并发测试:
bash
./ab -c10000 -n10000 http://127.0.0.1:8088/