【微服务部署】二、Jenkins一键打包部署SpringBoot

2023-10-24  本文已影响0人  全栈程序猿

  本文介绍使用Jenkins一键将SpringBoot应用打包并上传到生产环境服务器运行,这里使用的是直接在服务器上运行FagJar的方式,首先确保服务器环境配置好,安装必须的JDK运行时环境,运行目录,日志存放目录等。

一、服务器环境配置

  关于JDK的选择,自1.8版本收费之后,我们大多数选择openjdk作为生产环境的运行时环境,和大多数软件安装包一样,在CentOS服务器上,我们可以选择yum方式和下载压缩包的方式进行安装。大多数情况下使用yum方式比较简单方便。也有特殊情况,一些安全要求高单位或系统,只允许在局域网内运行,此时就需要选择压缩包的方式进行安装了。

1. yum方式安装openjdk
yum search java | grep -i --color openjdk
[root@localhost ~]# yum search java | grep -i --color openjdk
openjdk-asmtools-javadoc.noarch : Javadoc for openjdk-asmtools
icedtea-web.x86_64 : Additional Java components for OpenJDK - Java browser
java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation
java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-headless.x86_64 : The OpenJDK runtime environment without
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.8.0-openjdk.i686 : OpenJDK Runtime Environment 8
java-1.8.0-openjdk.x86_64 : OpenJDK 8 Runtime Environment
java-1.8.0-openjdk-accessibility.i686 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.8.0-openjdk-demo.i686 : OpenJDK Demos 8
java-1.8.0-openjdk-demo.x86_64 : OpenJDK 8 Demos
java-1.8.0-openjdk-devel.i686 : OpenJDK Development Environment 8
java-1.8.0-openjdk-devel.x86_64 : OpenJDK 8 Development Environment
java-1.8.0-openjdk-headless.i686 : OpenJDK Headless Runtime Environment 8
java-1.8.0-openjdk-headless.x86_64 : OpenJDK 8 Headless Runtime Environment
java-1.8.0-openjdk-javadoc.noarch : OpenJDK 8 API documentation
java-1.8.0-openjdk-javadoc-zip.noarch : OpenJDK 8 API documentation compressed
java-1.8.0-openjdk-src.i686 : OpenJDK Source Bundle 8
java-1.8.0-openjdk-src.x86_64 : OpenJDK 8 Source Bundle
java-11-openjdk.i686 : OpenJDK Runtime Environment 11
java-11-openjdk.x86_64 : OpenJDK 11 Runtime Environment
java-11-openjdk-demo.i686 : OpenJDK Demos 11
java-11-openjdk-demo.x86_64 : OpenJDK 11 Demos
java-11-openjdk-devel.i686 : OpenJDK Development Environment 11
java-11-openjdk-devel.x86_64 : OpenJDK 11 Development Environment
java-11-openjdk-headless.i686 : OpenJDK Headless Runtime Environment 11
java-11-openjdk-headless.x86_64 : OpenJDK 11 Headless Runtime Environment
java-11-openjdk-javadoc.i686 : OpenJDK 11 API documentation
java-11-openjdk-javadoc.x86_64 : OpenJDK 11 API documentation
java-11-openjdk-javadoc-zip.i686 : OpenJDK 11 API documentation compressed in a
java-11-openjdk-javadoc-zip.x86_64 : OpenJDK 11 API documentation compressed in
java-11-openjdk-jmods.i686 : JMods for OpenJDK 11
java-11-openjdk-jmods.x86_64 : JMods for OpenJDK 11
java-11-openjdk-src.i686 : OpenJDK Source Bundle 11
java-11-openjdk-src.x86_64 : OpenJDK 11 Source Bundle
java-11-openjdk-static-libs.i686 : OpenJDK libraries for static linking 11
java-11-openjdk-static-libs.x86_64 : OpenJDK 11 libraries for static linking
openjdk-asmtools.noarch : To develop tools create proper & improper Java
openprops.noarch : An improved java.util.Properties from OpenJDK

sudo yum install java-11-openjdk.x86_64 -y
[root@localhost /]# java -version
openjdk version "11.0.20" 2023-07-18 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.20.0.8-1.el7_9) (build 11.0.20+8-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.20.0.8-1.el7_9) (build 11.0.20+8-LTS, mixed mode, sharing)
[root@localhost /]# which java
/usr/bin/java
[root@localhost /]# dirname $(readlink $(readlink $(which java)))
/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el7_9.x86_64/bin
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el7_9.x86_64
PATH=$JAVA_HOME/bin:$PATH
export PATH JAVA_HOME
[root@localhost /]# source /etc/profile
[root@localhost /]# echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el7_9.x86_64

  执行完以上操作之后,Java运行时环境就安装配置好了,但是一定要注意:PATH在配置的时候,一定要把$JAVA_HOME/bin放在前面,不然系统中安装有多个版本的JDK,那么在使用java命令时,系统会找到其他版本的Java。还有以前很多教程配置中的CLASSPATH和tools.jar在JDK1.5以后就不需要配置了。

2. 部署及备份目录准备
mkdir -p /opt/tmp /opt/bak /opt/script /data/container/server
chmod -R 777 /opt/tmp /opt/bak /opt/script /data/container/server
3. 部署脚本编写说明
echo "param validate"
if [ $# -lt 1 ]; then  
  echo "you must use like this : ./publish_gitegg_server.sh [projectname] [server port] [server home dir]"  
  exit  
fi 
if [ "$1" != "" ]; then
   project_name="$1"
fi
if [ "$2" != "" ]; then
   server_port="$2"
fi
if [ "$3" != "" ]; then
   server_path="$3"
fi
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el7_9.x86_64
#check server process
server_pid=`/usr/sbin/lsof -n -P -t -i :$server_port`
echo "current :" $server_pid
#shutdown server
kill -9 $server_pid
echo "server shutting down"
while [ -n "$server_pid" ]
do
 sleep 5
 server_pid=`/usr/sbin/lsof -n -P -t -i :$server_port`
 echo "scan server pid :" $server_pid
done
echo "server shutt down"
#publish project
echo "scan no server pid,$project_name publishing"
rm -rf "$server_path"/$project_name*.jar
cp /opt/tmp/$project_name*.jar "$server_path"/$project_name.jar
#bak project
BAK_DIR=/opt/bak/$project_name/`date +%Y%m%d`
mkdir -p "$BAK_DIR"
cp "$server_path"/$project_name.jar "$BAK_DIR"/"$project_name"_`date +%H%M%S`.jar
#remove tmp
rm -rf /opt/tmp/$project_name*.jar
cd "$server_path"
source /etc/profile
nohup java -jar -Xms4096m -Xmx4096m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m "$project_name".jar --server.port="$server_port" > log.file 2>&1 &
echo "Server is starting,please try to access $container_name conslone url"
project_name=gitegg-server
server_port=8080
server_path="/data/container/server"
#param validate
if [ $# -lt 1 ]; then  
  echo "you must use like this : ./publish_qxq_server.sh [projectname] [server port] [server home dir]"  
  exit  
fi
if [ "$1" != "" ]; then
   project_name="$1"
fi
if [ "$2" != "" ]; then
   server_port="$2"
fi
if [ "$3" != "" ]; then
   server_path="$3"
fi
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el7_9.x86_64
#check server process
server_pid=`/usr/sbin/lsof -n -P -t -i :$server_port`
echo "current :" $server_pid
#shutdown server
kill -9 $server_pid
echo "server shutting down"
while [ -n "$server_pid" ]
do
 sleep 5
 server_pid=`/usr/sbin/lsof -n -P -t -i :$server_port`
 echo "scan server pid :" $server_pid
done
echo "server shutt down"
#publish project
echo "scan no server pid,$project_name publishing"
rm -rf "$server_path"/$project_name*.jar
cp /opt/tmp/$project_name*.jar "$server_path"/$project_name.jar
#bak project
BAK_DIR=/opt/bak/$project_name/`date +%Y%m%d`
mkdir -p "$BAK_DIR"
cp "$server_path"/$project_name.jar "$BAK_DIR"/"$project_name"_`date +%H%M%S`.jar
#remove tmp
rm -rf /opt/tmp/$project_name*.jar
#start server
cd "$server_path"
source /etc/profile
nohup java -jar -Xms4096m -Xmx4096m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m "$project_name".jar --server.port="$server_port" > log.file 2>&1 &
echo "Server is starting,please try to access $project_name conslone url"
chmod 755 publish_server.sh

二、新建Jenkins配置打包任务,部署SpringBoot应用

1. Dashboard > 新建任务,输入任务名称,选择“构建一个maven项目”,点击确定。
Dashboard
新建任务
2. 项目配置,进入到任务配置页
源码管理 访问凭证 Post Steps1
/opt/script/publish_server.sh gitegg-application 8182 /data/container/server
Post Steps2
3. 运行构建任务
立即构建 查看构建日志 日志
4、构建成功后,下方会给出构建成功提示,此时登录远程服务器查看文件是否部署成功即可。
部署成功
GitEgg-Cloud是一款基于SpringCloud整合搭建的企业级微服务应用开发框架,开源项目地址:

Gitee: https://gitee.com/wmz1930/GitEgg

GitHub: https://github.com/wmz1930/GitEgg

上一篇下一篇

猜你喜欢

热点阅读