编辑
2023-08-06
码农场
0
请注意,本文编写于 480 天前,最后修改于 479 天前,其中某些信息可能已经过时。

目录

一、安装
二、配置
1.supervisor:
2.gunicorn:
三、操作

项目正式上线后对可用性和可靠性的要求更高,而python的短板正在于此,该如何解决程序崩溃后自动重启以及通过负载均衡实现高并发控制呢,目前借助supervisor+gunicorn可以弥补这方面弱项。supervisor负责管理崩溃重启,gunicorn是一个python WSGI http server,其优势在于它使用了pre-fork worker模式。

一、安装

省略。supervisor用pip ,gunicorn用pip。

二、配置

1.supervisor:

首先打开sudoers文件,命令是sudo visudo,然后在文件最后添加一行:username ALL=(ALL) NOPASSWD: ALL,其中username是你的用户名,保存退出即可。 然后配置主文件/etc/supervisor/supervisord.conf和从文件/etc/supervisor/conf.d/*.conf。

配置文件路径在/etc/supervisor/conf.d/
; supervisor config file [unix_http_server] file=/var/run/supervisor.sock ; (the path to the socket file) chmod=0700 ; sockef file mode (default 0700) [supervisord] logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP) ; the below section must remain in the config file for RPC ; (supervisorctl/web interface) to work, additional interfaces may be ; added by defining them in separate rpcinterface: sections [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket ; The [include] section can just contain the "files" setting. This ; setting can list multiple files (separated by whitespace or ; newlines). It can also contain wildcards. The filenames are ; interpreted as relative to this file. Included files *cannot* ; include files themselves. [include] files = /etc/supervisor/conf.d/*.conf

2.gunicorn:

gunicornconf.py(该文件路径可以为任意)
# 并行工作进程数 workers = 4 # 指定每个工作者的线程数 threads = 2 # 监听内网端口5554 bind = '0.0.0.0:5000' # 设置守护进程,将进程交给supervisor管理 daemon = 'false' # 工作模式协程 worker_class = 'gevent' timeout = 600 # 设置最大并发量 worker_connections = 2000 # 设置进程文件目录 pidfile = '/home/hero/heroes_assemble_gunicorn.pid' # 设置访问日志和错误信息日志路径 accesslog = '/home/hero/log/heroes_assemble_gunicorn_access.log' errorlog = '/home/hero/log/heroes_assemble_gunicorn_error.log' access_log_format = '"%({X-Forwarded-For}i)s" %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" ' # 设置日志记录水平 loglevel = 'warning'

三、操作

supervisor重启命令是sudo supervisorctl restart all 停止命令是sudo supervisorctl stop all 重加载命令是sudo supervisorctl reload reload命令会重启配置文件修改过的程序,不会重启没有修改过的程序。

如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:来自火星

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!