¶
一、多个同样服务¶
作用:提高服务器的并发能力
优点:相比于docker-swarm的横向扩展节省了环境安装的空间能充分利用一个机器(容器)中的环境
缺点:相比于单个服务更占用内存,一个机器(容器)挂大家都挂
1.gunicorn¶
Running Gunicorn — Gunicorn 20.1.0 documentation
①简单配置¶
# config.py文件
reload = True
daemon = False
bind = '0.0.0.0:5000'
worker_class = 'sanic.worker.GunicornWorker'
loglevel = 'info'
accesslog = 'access.log'
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
②启动指令¶
gunicorn -k gevent -w 4 --config config.py python程序脚本:app --access-logfile '-'
2.注意的点¶
①安装环境¶
# requirements.txt
gevent
gunicorn
②对应日志¶
# 主程序中的配置
import logging.config
from logger_config import LOGGING_CONFIG
logging.config.dictConfig(LOGGING_CONFIG)
"""
这里是flask服务代码
"""
gunicorn_logger = logging.getLogger('gunicorn.info')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
# logger_config中的常用配置
LOGGING_CONFIG = {
"version": 1,
"formatters": {
"default": {
'format': '%(asctime)s %(filename)s %(lineno)s %(levelname)s %(message)s',
},
"plain": {
"format": "%(message)s",
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "default",
},
"console_plain": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "plain"
},
"file": {
"class": "logging.FileHandler",
"level": "INFO",
"filename": "./log.txt",
"formatter": "default",
}
},
"loggers": {
"debug_logger": {
"handlers": ["console"],
"level": "DEBUG",
"propagate": False,
},
"online_logger": {
"handlers": ["console"],
"level": "INFO",
"propagate": False,
},
"console_plain_logger": {
"handlers": ["console_plain"],
"level": "DEBUG",
"propagate": False,
},
"file_logger": {
"handlers": ["file"],
"level": "INFO",
"propagate": False,
}
},
"root": {"level": "DEBUG", "handlers": ["console"]},
}
二、多个不同服务¶
作用:方便部署和协调服务的编排
优点:相比docker-compose的方式启动多个服务能更好的利用同一个机器的资源,相比shell有很多可靠的启动重启机制使用
缺点:使用独立机器,机器挂服务都挂,
1.supervisord¶
Configuration File — Supervisor 4.2.4 documentation (supervisord.org)
①简单配置¶
# supervisord.conf
; supervisor config file
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
nodaemon=true
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/tmp ; ('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.
[program:progrm_name1]
# 启动命令入口
command=启动命令1
# 命令程序所在目录(要用绝对路径)
directory=/app
#运行命令的用户名
# user=root
autostart=true
autorestart=true
#日志地址
redirect_stderr = true
#stderr_logfile=/var/log/%(program_name)s.log
#stdout_logfile=/var/log/%(program_name)s.log
loglevel=info
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
[program:progrm_name2]
# 启动命令入口
command=启动命令2
# 命令程序所在目录(要用绝对路径)
directory=/app
autostart=true
autorestart=true
#日志地址
redirect_stderr = true
#stderr_logfile=/var/log/%(program_name)s.log
#stdout_logfile=/var/log/%(program_name)s.log
loglevel=info
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
②启动指令¶
supervisord -c supervisord.conf
2.注意的点¶
①安装环境¶
supervisor(==4.2.4)