Spring Boot 项目自动发布与Supervisor

Spring Boot 项目自动发布与Supervisor


专题

Spring Boot 项目自动发布

Spring Boot 项目自动发布与Supervisor

简介

前面写了一遍关于Spring Boot项目自动发布的文章这里是Github地址
还是受到不少欢迎的,有不少点赞的朋友,这次再接再厉,跟着上一篇,介绍使用Supervisor管理Spring Boot项目。

Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。

什么意思呢?就是说,原先启动的Spring boot项目,正常情况下可以一直运行,但是如果程序中出现了Bug,程序会自动退出,那么服务就不可用了,可以使用用Supervisor来管理服务,当程序退出后服务可以自动重启。

安装

安装supervisor很简单,我这边使用的是ubuntu,直接用apt-get安装即可,命令是sudo apt-get install supervisor

sudo apt-get install supervisor
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
建议安装:
  supervisor-doc
下列【新】软件包将被安装:
  supervisor
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
需要下载 253 kB 的归档。
解压缩后会消耗 1,401 kB 的额外空间。
获取:1 http://mirrors.aliyun.com/ubuntu xenial-updates/universe amd64 supervisor all 3.2.0-2ubuntu0.2 [253 kB]
已下载 253 kB,耗时 0秒 (508 kB/s)
正在选中未选择的软件包 supervisor。
(正在读取数据库 ... 系统当前共安装有 229783 个文件和目录。)
正准备解包 .../supervisor_3.2.0-2ubuntu0.2_all.deb  ...
正在解包 supervisor (3.2.0-2ubuntu0.2) ...
正在处理用于 man-db (2.7.5-1) 的触发器 ...
正在处理用于 systemd (229-4ubuntu21.2) 的触发器 ...
正在处理用于 ureadahead (0.100.0-19) 的触发器 ...
正在设置 supervisor (3.2.0-2ubuntu0.2) ...

安装成功后可以在 /etc/supervisor/ 目录下找到supervisord.conf配置文件,用vi命令来编辑。

# xiaqiulei @ ubuntu in /etc/supervisor [15:08:39]
$ cd /etc/supervisor/

# xiaqiulei @ ubuntu in /etc/supervisor [15:08:46]
$ cat supervisord.conf
; 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

配置

supervisord.conf这个文件的最后加上以下内容

[program:you program name] # 你的程序名,随便命名
command=python /home/pi/test.py # 你的命令,可以是任何运行在终端的命令
autostart=true # 自动启动
autorestart=true
user=root
log_stderr=true
logfile=/var/log/testpy.log # 日志文件的地址

在前面,是使用start.sh来启动程序,现在也只需要在supervisor中配置执行这个文件即可。

# xiaqiulei @ ubuntu in ~/deploy [15:18:35]
$ ls
application-prod.properties  application.properties  log  logback-spring.xml  restart.sh  springboot-deploy-demo-0.0.1-SNAPSHOT.jar  start.sh  stop.sh

# xiaqiulei @ ubuntu in ~/deploy [15:18:36]
$ ls -al
总用量 15764
drwxrwxr-x  3 xiaqiulei xiaqiulei     4096 4月  24 22:24 .
drwxr-xr-x 34 xiaqiulei xiaqiulei     4096 6月   1 15:18 ..
-rw-r--r--  1 xiaqiulei xiaqiulei       63 6月   1 15:17 application-prod.properties
-rw-r--r--  1 xiaqiulei xiaqiulei       27 6月   1 15:17 application.properties
drwxrwxr-x  2 xiaqiulei xiaqiulei     4096 6月   1 15:16 log
-rw-r--r--  1 xiaqiulei xiaqiulei      881 6月   1 15:17 logback-spring.xml
-rwxr-xr-x  1 xiaqiulei xiaqiulei       32 6月   1 15:17 restart.sh
-rw-r--r--  1 xiaqiulei xiaqiulei 16103697 6月   1 15:17 springboot-deploy-demo-0.0.1-SNAPSHOT.jar
-rwxr--r--  1 xiaqiulei xiaqiulei      970 6月   1 15:17 start.sh
-rwxr-xr-x  1 xiaqiulei xiaqiulei      291 6月   1 15:17 stop.sh

# xiaqiulei @ ubuntu in ~/deploy [15:18:39]
$ ./start.sh
INFO: /home/xiaqiulei/deploy/springboot-deploy-demo-0.0.1-SNAPSHOT.jar is running! pid=114791
http://127.0.0.1:8088/heartbeat
http code: 000
http code: 000
http code: 000
http code: 000
http code: 200
server start success...

需要先修改下start.sh文件,然后在修改下supervisor配置。因为原先的start.sh启动方式会将java程序作为一个后台进程。
主要语句是去除 2>&1 &,原先的校验功能也需要去除。如果此功能是必要的,可单独写成一个文件。

#!/bin/sh

# start.sh

#get pwd
DIR_HOME="${BASH_SOURCE-$0}"
DIR_HOME="$(dirname "$DIR_HOME")"
PRGDIR="$(cd "${DIR_HOME}"; pwd)"


jarfile=$PRGDIR/springboot-deploy-demo-0.0.1-SNAPSHOT.jar


#get runing pid
pid=$(ps -ef | grep java | grep $jarfile | awk '{print $2}')

#create log dir
mkdir -p $PRGDIR/log/

nohup java -jar $jarfile -Dfile.encoding=UTF-8 --spring.config.location=$PRGDIR/ >$PRGDIR/log/start.log
pid=$(ps -ef | grep java | grep $jarfile | awk '{print $2}')
echo "INFO: $jarfile is running! pid=$pid"

单独的校验文件。

#!/usr/bin/env bash

# validate.sh

url="http://127.0.0.1:8088/heartbeat";
echo $url
while [ true ]
do
    sleep 1
    HTTP_CODE=`curl -G -m 10 -o /dev/null -s -w %{http_code} $url`
    echo "http code: ${HTTP_CODE}"
    if [ ${HTTP_CODE} -eq 200 ]
    then
        echo "server start success..."
        exit 0
    fi
done

完成脚本的修改操作后,就可以修改supervisor的配置。

[program:spring_boot_demo]
user = xiaqiulei
directory = /home/xiaqiulei/deploy
command = bash -c ./start.sh
autostart = true
autorestart =  true

然后重新加载配置,最后开启服务即可。

$ sudo supervisorctl status

$ sudo supervisorctl reload
Restarted supervisord

$ sudo supervisorctl status
spring_boot_demo                 STOPPED   Jun 01 04:25 PM

$ sudo supervisorctl start spring_boot_demo
spring_boot_demo: started

$ sudo supervisorctl status
spring_boot_demo                 RUNNING   pid 119403, uptime 0:00:10

可以测试下,当程序退出的时候,服务会自动重启,如下所示,kill掉当前的进程,然后在看下状态,服务还在,并且pid是和原先不一样的。

$ sudo supervisorctl status
spring_boot_demo                 RUNNING   pid 119607, uptime 0:03:16

$ sudo supervisorctl

$ kill -9 119607

$ sudo supervisorctl status
spring_boot_demo                 RUNNING   pid 119807, uptime 0:00:02

可以使用校验文件,检查下服务。

$ ./validate.sh
http://127.0.0.1:8088/heartbeat
http code: 200
server start success...

设置supervisor开机自启

编辑/etc/rc.local文件 ,让 supervisor 开机启动,这样就可以使脚本在开机的时候随supervisor启动运行。
在这个配置文件的exit 0前面一行加上 service supervisor start保存。

源码地址

源码地址 https://github.com/LiushuiXiaoxia/springboot-deploy-demo

Spring Boot 项目自动发布与Supervisor

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,511评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,495评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,595评论 0 225
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,558评论 0 190
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,715评论 3 270
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,672评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,112评论 2 291
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,837评论 0 181
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,417评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,928评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,316评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,773评论 2 234
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,253评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,827评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,440评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,523评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,583评论 2 249

推荐阅读更多精彩内容