一、supervisor 介绍
Supervisor 翻译过来是监管人,在 Linux 中 Supervisor 是一个进程管理工具,当进程中断的时候 Supervisor 能自动重新启动它。可以运行在各种类 Linux/unix 的机器上,supervisor 就是用 Python 开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台 daemon,并监控进程状态,异常退出时能自动重启。
二、安装 supervisor
1.检查仓库是否有 supervisor 的包,没有则更新 epel 源
yum list | grep supervisor
yum install -y epel-release
2.安装 supervisor
yum install -y supervisor
安装完之后的 supervisor 默认配置文件:/etc/supervisord.conf
supervisor 安装完之后会生成两个执行程序(supervisord、supervisorctl)和一个配置文件(echo_supervisord_conf),分别是 supervisor 的守护进程服务(用于接收进程管理命令)、客户端(用于守护进程通信,发送管理进程的指令)、生成初始配置文件程序。
以下是 supervisord.conf 文件的配置:
; Sample supervisor config file.
[unix_http_server]
file=/var/run/supervisor/supervisor.sock ; (the path to the socket file)
;chmod=0700 ; socket文件的mode,默认是0700,如果是非root用户使用supervisorctl,则设置成高权限,如0777
;chown=nobody:nogroup ; socket文件的owner,格式:uid:gid
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
;[inet_http_server] ; HTTP服务器,提供web管理界面
;port=127.0.0.1:9001 ; Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
;username=user ; 登录管理后台的用户名
;password=123 ; 登录管理后台的密码
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
;umask=022 ; (process file creation umask;default 022)
;user=chrism ; (default is current user, required if root)
;identifier=supervisor ; (supervisord identifier, default is 'supervisor')
;directory=/tmp ; (default is not to cd during start)
;nocleanup=true ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value ; (key value pairs to add to environment)
;strip_ansi=false ; (strip ansi escape codes in logs; def. false)
; 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/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as http_username if set
;password=123 ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.
;[program:theprogramname]
;command=/bin/cat ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;autorestart=true ; retstart at unexpected quit (default: true)
;startsecs=10 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A=1,B=2 ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.
;[eventlistener:theeventlistenername]
;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size=10 ; event buffer queue size (default 10)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=-1 ; the relative start priority (default -1)
;autostart=true ; start at supervisord start (default: true)
;autorestart=unexpected ; restart at unexpected quit (default: unexpected)
;startsecs=10 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A=1,B=2 ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.
;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority=999 ; the relative start priority (default 999)
; 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/supervisord.d/*.conf ;可以指定一个或多个以.ini或.conf结束的配置文件,以空格分隔各个配置条目
注:[include]
files = /etc/supervisord.d/*.conf 这行代表 supervisor 会从配置文件的同级目录supervisord.d(默认为/etc/ supervisord.d/)下读取所有的.ini或.conf文件作为被管理的应用的配置。
3.启动 supervisor 并配置成开机自启
systemctl start supervisord
systemctl enable supervisord
三、安装 rocketmq
1.安装包下载地址
https://rocketmq.apache.org/download/
2.下载所需版本上传解压
unzip rocketmq-all-5.1.1-bin-release.zip
mv rocketmq-all-5.1.1-bin-release /usr/local/rocketmq
vim /etc/profile #将以下配置添加到 profile
export PATH=$PATH:/usr/local/rocketmq/bin
export NAMESRV_ADDR=localhost:9876
source /etc/profile
3.修改 server、broke 启动内存
cd /usr/local/rocketmq/
vim bin/runserver.sh
vim bin/runbroker.sh
4.rocketmq 启停命令
cd /usr/local/rocketmq/
nohup sh bin/mqnamesrv>/usr/local/rocketmq/log/namesrv.log 2>&1 & # 启动 namesrv
nohup sh bin/mqbroker -n localhost:9876 -c conf/broker.conf >/usr/local/rocketmq/log/broker.log 2>&1 & # 启动 broker
sh bin/mqshutdown broker # 停止 broke
sh bin/mqshutdown namesrv # 停止 namesrv
四、配置 supervisor 管理 rocketmq 进程文件
1.启动 namesrv 配置
vim /etc/supervisord.d/mqnamesrv.conf
[program:mqnamesrv]
command=/usr/local/rocketmq/bin/mqnamesrv
directory=/usr/local/rocketmq
autostart=true
priority=1
autorestart=true
startretries=5
stopsignal=QUIT
user=root
stdout_logfile=/usr/local/rocketmq/log/namesrv.log
environment=JAVA_HOME=/opt/jdk1.8.0_291,JAVA_BIN=/opt/jdk1.8.0_291/bin
2.启动 broke 配置
vim /etc/supervisord.d/mqbroker.conf
[program:mqbroker]
command=/usr/local/rocketmq/bin/mqbroker -n localhost:9876 -c /usr/local/rocketmq/conf/broker.conf
directory=/usr/local/rocketmq
autostart=true
autorestart=true
startretries=5
stopsignal=QUIT
user=root
stdout_logfile=/usr/local/rocketmq/log/broker.log
environment=JAVA_HOME="/opt/jdk1.8.0_291",JAVA_BIN="/opt/jdk1.8.0_291/bin"
- supervisor 常用命令
supervisorctl status # 查看当前运行的进程列表
supervisorctl reload # 载入所有配置文件,并按新的配置启动、管理所有进程(会重启原来已运行的程序)
supervisorctl update # 更新新的配置到supervisord(不会重启原来已运行的程序)
supervisorctl reread # 当一个服务由自动启动修改为手动启动时执行一下就ok
supervisorctl start xxx # 启动某个进程
supervisorctl stop xxx # 停止某一个进程(xxx),xxx为[program:theprogramname]里配置的值
supervisorctl restart xxx # 重启某个进程
supervisorctl stop all # 停止全部进程
supervisorctl stop all #启动全部进程
报错:ERROR: Please set the JAVA_HOME variable in your environment, We need java(x64)! !!
这里用 supervisor 启动 rocketmq 会找不到 jdk 路径,然后做了以下修改
cd /usr/local/rocketmq/
vim bin/runserver.sh
vim bin/runbroker.sh
4.启动 namesrv、broker
supervisorctl start all