Springboot服务器自动部署supervisor+gradle+git

Springboot服务器自动部署supervisor+gradle+git

Java SpringBoot 项目在服务器端部署
Git 拉取最新代码
自动 build 打成 jar

然后使用 supervisor 守护进程管理运行

Supervisor配置和使用

安装supervisor

sudo pip install supervisor

生成配置文件到 /etc下

echo_supervisord_conf > /etc/supervisor/supervisord.conf

启动supervisor

supervisord -c /etc/supervisor/supervisord.conf 

配置 supervisord.conf 文件

vim /etc/supervisor/supervisord.conf
//`;` 是代表注释,需要修改的都列出来了
...

[unix_http_server]
file=/var/run/supervisor.sock   ; 原来在临时tmp文件下,但是tmp会被系统删除,所以新建run文件夹,注意改路径

[supervisord]                ;按照默认配置去掉分号即生效
logfile=/var/logs/supervisor/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=/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     

...

[rpcinterface:supervisor]     ;按照默认配置无需改动
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

...

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; 这里还是将tmp改成run文件夹,需自行创建

...

;重要: 需要运行的项目,我这里叫api
[program:api] 
command=java -jar /root/api.jar              ; 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=/                ; 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=true        ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false       
...

;如果要分开配置各个文件,则放开注释,这里暂时不管
;[include]
;files = /etc/supervisor/conf.d/*.ini

常用 supervisorctl 命令


 supervisorctl status  查看

 supervisorctl stop usercenter  停止

 supervisorctl start usercenter  启动

 supervisorctl restart usercenter   重启

 supervisorctl reread  读取有更新(增加)的配置文件,不会启动新添加的程序

 supervisorctl update 重启配置文件修改过的程序


//退出spuervisor模式
>spuervisor  exit  //退出

//更多命令
>spuervisor help
default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail   
avail  fg        pid   remove  shutdown  status  update 
clear  maintail  quit  reread  signal    stop    version

supervisor 服务启动

 supervisord -c /etc/supervisor/supervisord.conf 

当切换tmp文件夹 -> run 文件夹下 , 可能报错:

  • 报错 unix:///var/run/supervisor.sock no such file
//执行即可创建一个文件,修改权限
sudo touch /var/run/supervisor.sock
sudo chmod 777 /var/run/supervisor.sock
  • 报错 Unlinking stale socket /var/run/supervisor.sock
//启动spuervisordctl
spuervisorctl
  • 报错 Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
//已经启动了一个supervisord,可以先杀掉之前的再重启
ps -ef | grep supervisor   //查进程id
kill -9 6090               //杀掉进程为6090的supervisor
supervisord -c /etc/supervisor/supervisord.conf //重启

重新启动后再次查看运行状态

supervisorctl status
  • 我运行的项目为 api

这里写图片描述

Gradle下载和配置

查看官方最新版本, 在服务器安装 gradle

https://gradle.org/releases/

下载gradle

wget https://downloads.gradle.org/distributions/gradle-4.9-bin.zip 

这里写图片描述

解压

 unzip -d /opt/gradle gradle-4.9-bin.zip 

解压完成后,输入命令 vim /etc/profile ,进入配置文件最后增加两行环境变量

export PATH=${PATH}:/opt/gradle/gradle-4.9/bin
export PATH=${PATH}:/usr/local/git/bin

然后:wq保存并刷新

source /etc/profile

检查是否配置正确

gradle --version

这里写图片描述

项目关联Git仓库

把项目放在在服务器 /root 目录下,cd到/root 初始化git

git init

添加远程仓库

git remote add origin https://gitee.com/你的项目名称.git

拉取代码

git pull origin master

将本地和远程关联

git branch --set-upstream-to=origin/master master

使用gradle来构建项目

gradle clean 
gradle build
  • 命令可以执行,但是我们需要让他自动构建,需要创建一个可执行文件 我起名叫 update.sh 创建并打开
vi update.sh
  • 编辑内容如下 :wq 保存退出
#!/bin/bash
echo ========================
echo 自动化部署脚本启动
echo ========================
echo 进入/root/platfrom
cd /root/platfrom
echo 开始pull版本
git pull
cd api
echo clean项目
gradle clean
echo 开始编译文件
gradle build
echo 进入编译完成jar包路径
cd build/libs/
echo 移动jar文件
mv api.jar /root/
echo 重启jar
cd /root
supervisorctl restart api
echo 重启完成
  • 修改update.sh增加可运行权限
sudo chmod +x /update.sh

我的服务器目录结构:

注意根据自己的目录结构来写update路径内容

|– root

|  |– api.jar   (打包生复制过来的jar ,要运行它的)

扫描二维码关注公众号,回复: 2801171 查看本文章

|  |– platefrom   (同步git项目)

|  |– update.sh  (自动部署脚本)

|– boot

|– mnt

|– 省略...

自动部署

  • 打开到 /root 路径下执行命令
./update.sh
  • 这里输入 git 账号密码,然后等待自动 build 成功, 最后 provisorctl 来启动项目。

猜你喜欢

转载自blog.csdn.net/baidu_25797177/article/details/81481518
今日推荐