Linux下如何用Supervisor管理Springboot進(jìn)程?
Supervisor簡(jiǎn)介
Supervisor是一個(gè)C/S架構(gòu)進(jìn)程管理工具,通過它可以監(jiān)控和控制其他的進(jìn)程,同時(shí)它自身提供了一個(gè)WebUI,可以在WebUI進(jìn)行start、stop、restart操作。由Supervisor管理的進(jìn)程,都是它的子進(jìn)程。
Supervisor的服務(wù)器端稱為supervisord,主要負(fù)責(zé)在啟動(dòng)自身時(shí)啟動(dòng)管理的子進(jìn)程,響應(yīng)客戶端的命令,重啟崩潰或退出的子進(jìn)程,記錄子進(jìn)程stdout和stderr輸出,生成和處理子進(jìn)程生命周期中的事件??梢栽谝粋€(gè)配置文件中配置相關(guān)參數(shù),包括Supervisord自身的狀態(tài),其管理的各個(gè)子進(jìn)程的相關(guān)屬性。
Supervisor的客戶端稱為supervisorctl,它提供了一個(gè)類shell的接口(即命令行)來使用supervisord服務(wù)端提供的功能。通過supervisorctl,用戶可以連接到supervisord服務(wù)器進(jìn)程,獲得服務(wù)器進(jìn)程控制的子進(jìn)程的狀態(tài),啟動(dòng)和停止子進(jìn)程,獲得正在運(yùn)行的進(jìn)程列表??蛻舳送ㄟ^Unix域套接字或者TCP套接字與服務(wù)端進(jìn)行通信,服務(wù)器端具有身份憑證認(rèn)證機(jī)制,可以有效提升安全性。
當(dāng)客戶端和服務(wù)器位于同一臺(tái)機(jī)器上時(shí),客戶端與服務(wù)器共用同一個(gè)配置文件,通過不同標(biāo)簽來區(qū)分兩者的配置。
部署
安裝Supervisor
Supervisor由Python開發(fā),所以除了yum方式外也可以通過pip進(jìn)行安裝。
yum安裝
yum install -y epel-release && yum install -y supervisor
pip安裝
pip install supervisor
安裝jdk
由于是Springboot服務(wù)所以還需要安裝jdk。
yum install java-1.8.0-openjdk
Supervisor配置文件
在etc目錄下生成Supervisor配置文件。
echo_supervisord_conf > /etc/supervisord.conf
完整配置文件如下,也可以根據(jù)以下內(nèi)容直接創(chuàng)建配置文件。
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
; - Shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
; - Quotes around values are not supported, except in the case of
; the environment= options as shown below.
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
; - Command will be truncated if it looks like a config file comment, e.g.
; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".
;
; Warning:
; Paths throughout this example file use /tmp because it is available on most
; systems. You will likely need to change these to locations more appropriate
; for your system. Some systems periodically delete older files in /tmp.
; Notably, if the socket file defined in the [unix_http_server] section below
; is deleted, supervisorctl will be unable to connect to supervisord.
[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
; Security Warning:
; The inet HTTP server is not enabled by default. The inet HTTP server is
; enabled by uncommenting the [inet_http_server] section below. The inet
; HTTP server is intended for use within a trusted environment only. It
; should only be bound to localhost or only accessible from within an
; isolated, trusted network. The inet HTTP server does not support any
; form of encryption. The inet HTTP server does not use authentication
; by default (see the username= and password= options to add authentication).
; Never expose the inet HTTP server to the public internet.
;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
silent=false ; no logs to stdout 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=supervisord ; setuid to this UNIX account at startup; recommended 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 rpcinterface:supervisor 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:x] sections.
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
; The supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section.
[supervisorctl]
serverurl=unix:///tmp/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 in [*_http_server] if set
;password=123 ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The sample program section below 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)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;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 (0 means none, default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stdout_syslog=false ; send stdout to syslog with process name (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 (0 means none, default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;stderr_syslog=false ; send stderr to syslog with process name (default false)
;environment=A="1",B="2" ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
; The sample eventlistener section below shows all possible eventlistener
; subsection values. Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.
;[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)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
;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 (0 means none, default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stdout_syslog=false ; send stdout to syslog with process name (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 (0 means none, default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;stderr_syslog=false ; send stderr to syslog with process name (default false)
;environment=A="1",B="2" ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils)
; The sample group section below 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 = relative/directory/*.ini
生成的配置文件模板由多段配置組成,使用英文分號(hào)“;”標(biāo)識(shí)注釋。
[unix_http_server]
這個(gè)配置段,配置http服務(wù)監(jiān)聽的 socket,它的作用是為了讓supervisorctl 能連接supervisord http server獲取進(jìn)程狀態(tài)信息,包括管理子進(jìn)程。
[unix_http_server] file=/tmp/supervisor.sock ; the path to the socket file ;chmod=0700 ; socket file mode (default 0700) ;chown=nobody:nogroup ; socket file uid:gid owner ;username=user ; default is no username (open server) ;password=123 ; default is no password (open server)
[inet_http_server]
Supervisor提供了一個(gè)WebUI的管理界面,如果需要打開的話,就在這個(gè)配置段設(shè)置,port指定監(jiān)聽ip和端口,username 和password指定訪問WebUI需要的認(rèn)證信息。
[supervisord]
Supervisor服務(wù)的配置項(xiàng)。
[supervisord] logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB logfile_backups=10 ; # of main logfile backups; 0 means none, default 10 loglevel=info ; log level; default info; others: debug,warn,trace pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid nodaemon=false ; start in foreground if true; default false silent=false ; no logs to stdout 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=supervisord ; setuid to this UNIX account at startup; recommended 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
[supervisorctl]
Supervisor的客戶端程序supervisorctl的相關(guān)配置,注意serverurl對(duì)應(yīng)的sock就是[unix_http_server]中file的配置項(xiàng),如果上面配置了用戶名密碼,此處也需要配置。
[supervisorctl] serverurl=unix:///tmp/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 in [*_http_server] if set ;password=123 ; should be same as in [*_http_server] if set ;prompt=mysupervisor ; cmd line prompt (default "supervisor") ;history_file=~/.sc_history ; use readline history if available
[include]
指定包含的配置文件的路徑,通常被監(jiān)控的程序配置文件,一個(gè)程序一個(gè),更加清晰方便管理??梢詫⑹纠械?ini后綴改為.conf。
;[include] ;files = relative/directory/*.ini
[program:theprogramname]
具體需要監(jiān)控的程序配置項(xiàng),program冒號(hào)后面的內(nèi)容就是程序名稱。
;[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) ;startsecs=1 ; # of secs prog must stay up to be running (def. 1) ;startretries=3 ; max # of serial start failures when starting (default 3) ;autorestart=unexpected ; when to restart if exited after running (def: unexpected) ;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0) ;stopsignal=QUIT ; signal used to kill process (default TERM) ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) ;stopasgroup=false ; send stop signal to the UNIX process group (default false) ;killasgroup=false ; SIGKILL the UNIX process group (def false) ;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 (0 means none, default 10) ;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) ;stdout_events_enabled=false ; emit events on stdout writes (default false) ;stdout_syslog=false ; send stdout to syslog with process name (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 (0 means none, default 10) ;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) ;stderr_events_enabled=false ; emit events on stderr writes (default false) ;stderr_syslog=false ; send stderr to syslog with process name (default false) ;environment=A="1",B="2" ; process environment additions (def no adds) ;serverurl=AUTO ; override serverurl computation (childutils)
準(zhǔn)備springboot程序
準(zhǔn)備兩個(gè)Springboot程序demo01.jar、demo02.jar用于測(cè)試。程序內(nèi)容沒什么好說的,主要是提供一個(gè)接口來確認(rèn)程序是否正常運(yùn)行,并將兩個(gè)jar包上傳至/usr/app目錄下。。
package com.zzp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class Demo01Application {
@GetMapping("/hello")
public String hello(){
return "hello,demo01";
}
public static void main(String[] args) {
SpringApplication.run(Demo01Application.class, args);
}
}
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class Demo02Application {
@GetMapping("/hello")
public String hello(){
return "hello demo02";
}
public static void main(String[] args) {
SpringApplication.run(Demo02Application.class, args);
}
}
Springboot Supervisor程序配置
在/etc/supervisor目錄下準(zhǔn)備兩個(gè)Springboot對(duì)應(yīng)的Supervisor程序配置文件demo01.conf、demo02.conf,具體的配置文件含義可以參考注釋。
[program:demo01] directory = /usr/app ; 程序的啟動(dòng)目錄 command = java -jar /usr/app/demo01.jar ; 啟動(dòng)命令,可以看出與手動(dòng)在命令行啟動(dòng)的命令是一樣的 autostart = true ; 在 supervisord 啟動(dòng)的時(shí)候也自動(dòng)啟動(dòng) startsecs = 30 ; 啟動(dòng) 30 秒后沒有異常退出,就當(dāng)作已經(jīng)正常啟動(dòng)了 autorestart = true ; 程序異常退出后自動(dòng)重啟 startretries = 3 ; 啟動(dòng)失敗自動(dòng)重試次數(shù),默認(rèn)是 3 redirect_stderr = true ; 把 stderr 重定向到 stdout,默認(rèn) false stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默認(rèn) 50MB stdout_logfile_backups = 20 ; stdout 日志文件備份數(shù) ; stdout 日志文件,需要注意當(dāng)指定目錄不存在時(shí)無法正常啟動(dòng),所以需要手動(dòng)創(chuàng)建目錄(supervisord 會(huì)自動(dòng)創(chuàng)建日志文件) stdout_logfile = /usr/app/demo01.log ;應(yīng)用日志目錄
[program:demo02] directory = /usr/app ; 程序的啟動(dòng)目錄 command = java -jar /usr/app/demo02.jar ; 啟動(dòng)命令,可以看出與手動(dòng)在命令行啟動(dòng)的命令是一樣的 autostart = true ; 在 supervisord 啟動(dòng)的時(shí)候也自動(dòng)啟動(dòng) startsecs = 30 ; 啟動(dòng) 30 秒后沒有異常退出,就當(dāng)作已經(jīng)正常啟動(dòng)了 autorestart = true ; 程序異常退出后自動(dòng)重啟 startretries = 3 ; 啟動(dòng)失敗自動(dòng)重試次數(shù),默認(rèn)是 3 redirect_stderr = true ; 把 stderr 重定向到 stdout,默認(rèn) false stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默認(rèn) 50MB stdout_logfile_backups = 20 ; stdout 日志文件備份數(shù) ; stdout 日志文件,需要注意當(dāng)指定目錄不存在時(shí)無法正常啟動(dòng),所以需要手動(dòng)創(chuàng)建目錄(supervisord 會(huì)自動(dòng)創(chuàng)建日志文件) stdout_logfile = /usr/app/demo02.log ;應(yīng)用日志目錄
修改Supervisor配置文件
修改/etc/supervisord.conf文件中的[include]這段配置,將注釋放開并把上面兩個(gè)程序配置文件加入進(jìn)去。
[include] files = /etc/supervisor/*.conf
啟動(dòng)supervisord
通過“-c”指定配置文件啟動(dòng)supervisord,否則會(huì)默認(rèn)到當(dāng)前目錄下去尋找。
supervisord -c /etc/supervisord.conf
驗(yàn)證程序
supervisord服務(wù)啟動(dòng)成功后,我們就可以通過supervisorctl命令進(jìn)行交互了。
# 查看管理進(jìn)程狀態(tài) supervisorctl status # 停止指定進(jìn)程,如果是all則是操作全部管理的進(jìn)程 supervisorctl stop [進(jìn)程名稱] # 啟動(dòng)指定進(jìn)程,如果是all則是操作全部管理的進(jìn)程 supervisorctl start [進(jìn)程名稱] # 重啟指定進(jìn)程,如果是all則是操作全部管理的進(jìn)程 supervisorctl restart [進(jìn)程名稱] # 配置文件修改后使用該命令加載新的配置 supervisorctl update # 重新啟動(dòng)配置中的所有程序 supervisorctl reload
輸入supervisorctl status如果看到以下內(nèi)容則表示兩個(gè)springboot服務(wù)啟動(dòng)成功。
[root@supervisor etc]# supervisorctl status demo01 RUNNING pid 3853, uptime 0:01:50 demo02 RUNNING pid 3852, uptime 0:01:50
同樣通過ps -ef也能看到對(duì)應(yīng)進(jìn)程信息。
[root@supervisor etc]# ps -ef| grep java root 3852 3851 0 16:36 ? 00:00:07 java -jar /usr/app/demo02.jar root 3912 3851 1 16:40 ? 00:00:07 java -jar /usr/app/demo01.jar root 3945 1643 0 16:49 pts/0 00:00:00 grep --color=auto java
訪問程序中的接口驗(yàn)證程序正常啟動(dòng)。
[root@supervisor ~]# curl http://127.0.0.1:8001/hello hello,demo01 [root@supervisor ~]# curl http://127.0.0.1:8002/hello hello,demo02
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
linux下使用cmake編譯安裝mysql的詳細(xì)教程
這篇文章主要介紹了linux下使用cmake編譯安裝mysql的詳細(xì)教程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
虛擬機(jī)ubuntu16.04無法連網(wǎng)的解決方法
這篇文章主要為大家詳細(xì)介紹了虛擬機(jī)ubuntu16.04無法連網(wǎng)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
linux磁盤管理軟RAID的實(shí)現(xiàn)原理分析和方法分享
這篇文章主要介紹了linux磁盤管理中做軟RAID的實(shí)現(xiàn)原理分析和方法分享,需要的朋友跟著學(xué)習(xí)下。2017-12-12
ubuntu系統(tǒng)theano和keras的安裝方法
這篇文章主要介紹了ubuntu系統(tǒng)theano和keras的安裝方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
在Linux中限制root用戶SSH遠(yuǎn)程登錄的流程詳解
在Linux系統(tǒng)中,root用戶擁有最高權(quán)限,因此為了增強(qiáng)系統(tǒng)安全性,我們通常會(huì)限制root用戶通過SSH遠(yuǎn)程登錄,本文將介紹如何在Linux系統(tǒng)中限制root用戶的SSH遠(yuǎn)程登錄,需要的朋友可以參考下2024-03-03

