本文主要是介绍shell脚本部署springboot(修订版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
shell脚本部署springboot(修订版)
说明
之前整理了 shell 部署 springboot
网址:https://blog.csdn.net/shaoming314/article/details/120145215
但是这个shell 脚本 没有 停止 , 查看 , 重启服务的选项,不是很好,这个修订版支持,支持停止,重启,查看服务运行状态等选项
脚本内容如下
renren.sh
#!/bin/bash
# 使用maven 进行打包# 程序名称(jar 包的位置)
APP_NAME=/opt/test/shell/renren-generator-1.0.0.jar# 日志文件路径及名称 ( 自行设置 )
LOG_FILE=/opt/test/shell/renren-genertor.log# 项目启动的 jvm 参数 ( 自行设置 )
JVM_OPTS="-Dname=$APP_NAME -Duser.timezone=Asia/Shanghai -Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"# 项目启动端口号( 自行设置 )
PORT=80# 判断项目jar包试凑存在
if [ ! -f $APP_NAME ];
thenecho "没有找到项目jar包"exit 1
fi# 根据选择对项目进行相应的操作
if [ "$1" = "" ];
thenecho -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status} \033[0m"exit 1
fi# 开启项目
function start()
{PID=`ps -ef |grep java|grep $APP_NAME|grep -v grep|awk '{print $2}'`if [ x"$PID" != x"" ]; thenecho "$APP_NAME is running..."elsenohup java $JVM_OPTS -jar $APP_NAME --server.port=$PORT > $LOG_FILE 2>&1 &echo "Start $APP_NAME success..."# 查看项目启动日志tail -f $LOG_FILEfi
}# 停止项目
function stop()
{echo "Stop $APP_NAME"PID=""query(){PID=`ps -ef |grep java|grep $APP_NAME|grep -v grep|awk '{print $2}'`}queryif [ x"$PID" != x"" ]; thenkill -9 $PIDecho "$APP_NAME (pid:$PID) exiting..."while [ x"$PID" != x"" ]dosleep 1querydoneecho "$APP_NAME exited."elseecho "$APP_NAME already stopped."fi
}# 重启项目
function restart()
{stopsleep 2start
}# 查看项目运行状态,看是否运行
function status()
{PID=`ps -ef |grep java|grep $APP_NAME|grep -v grep|wc -l`if [ $PID != 0 ];thenecho "$APP_NAME is running..."elseecho "$APP_NAME is not running..."fi
}case $1 instart)start;;stop)stop;;restart)restart;;status)status;;*)esac
参考脚本
本脚本参考 ruoyi 项目的启动脚本
网址:https://gitee.com/y_project/RuoYi/blob/master/ry.sh
基础项目
renren-generator
项目 git 仓库地址: https://gitee.com/renrenio/renren-generator
项目说明
- renren-generator是人人开源项目的代码生成器,可在线生成entity、xml、dao、service、html、js、sql代码,减少70%以上的开发任务
使用技术栈
springboot
mysql
mybatis-plus
- 。。。。。
使用该项目测试的原因
- 该项目简单容易部署
- 该项目是一个很好的开源代码生成工具
- 该项目启动简单,容易测试
环境准备
可以选择云服务器 , 可以选择虚拟机进行设置
我的运行环境是 腾讯云 轻量级云服务器 系统版本为 centos7
安装jdk
yum -y install java-1.8.0-openjdk*
测试是否安装成功
java -version
[root@VM-16-6-centos ~]# java -version
openjdk version “1.8.0_312”
OpenJDK Runtime Environment (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)
jdk1.8 安装成功
安装maven
安装脚本
instal-maven3.6.1.sh
# 下载maven安装包
wget https://archive.apache.org/dist/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz
# 解压安装包
tar -zxvf apache-maven-3.6.1-bin.tar.gz
# 移动maven到/usr/local/目录
mv apache-maven-3.6.1 /usr/local/maven
echo 'maven的目录在 /usr/local/maven'
#配置maven的环境变量
echo 'export MAVEN_HOME=/usr/local/maven' >> /etc/profile
echo 'export PATH=$PATH:$MAVEN_HOME/bin' >> /etc/profile# 刷新环境变量
source /etc/profileecho '阿里云镜像地址如下:'
#配置阿里云镜像
echo '<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
'#清空maven的settings文件
echo '' > /usr/local/maven/conf/settings.xml# 配置maven的settings文件,设置镜像为阿里云镜像,提交依赖下载速度(去除了maven远配置文件中注释的配置,所以内容比较少,可以自行安装好根据自己环境进行配置)
cat >/usr/local/maven/conf/settings.xml<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"><pluginGroups></pluginGroups><proxies></proxies><servers></servers><mirrors>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror></mirrors><profiles></profiles>
</settings>
EOF# 测试maven是否安装成功
mvn -v
测试 maven 是否安装成功 执行命令mvn -v
[root@VM-16-6-centos ~]# mvn -v
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_312, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: “linux”, version: “3.10.0-1160.11.1.el7.x86_64”, arch: “amd64”, family: “unix”
安装docker
install-docker.sh
#!/bin/bash
## 自动yum安装Docker,Docker配置yum源 2020-05-31
## 有问题反馈 https://aq2.cn/c/docker
## email: ppabc@qq.com
## robert yu
## redhat 7##安装
## yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
## curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
## curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
## daemon.json k8s私有可以增加 "insecure-registries":["harbor.io", "k8s.gcr.io", "gcr.io", "quay.io"],
## 卸载docker命令 yum remove -y docker-ce docker-common-*yum-config-manager --add-repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum install -y yum-utils device-mapper-persistent-data lvm2 nfs-utils conntrack-tools
yum install -y docker-ce-18.09.8 docker-ce-cli-18.09.8##配置docker源
mkdir -p /etc/docker
if [[ -f /etc/docker/daemon.json ]] ; then/usr/bin/cp /etc/docker/daemon.json /etc/docker/daemon.json.bak.`date "+%Y%m%d%H%M%S"`
fi
echo -e '
{"storage-driver": "overlay2","storage-opts": [ "overlay2.override_kernel_check=true" ],"registry-mirrors": ["https://4ux5p520.mirror.aliyuncs.com"],"exec-opts": ["native.cgroupdriver=systemd"],"data-root": "/data/docker", "log-driver": "json-file","log-opts": {"max-size": "100m"}
}
' > /etc/docker/daemon.json##启动
systemctl daemon-reload
systemctl enable docker
systemctl restart docker
docker version
测试docker是否安装 docker verson
[root@VM-16-6-centos ~]# docker version
Client:
Version: 18.09.8
API version: 1.39
Go version: go1.10.8
Git commit: 0dd43dd87f
Built: Wed Jul 17 17:40:31 2019
OS/Arch: linux/amd64
Experimental: falseServer: Docker Engine - Community
Engine:
Version: 18.09.8
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 0dd43dd
Built: Wed Jul 17 17:10:42 2019
OS/Arch: linux/amd64
Experimental: false
docker 安装成功
安装mysql
方便快速测试,我这里使用docker安装mysql
docker 安装 mysql5.7 脚本
docker-install-mysql57.sh
# 拉取mysql5.7镜像
docker pull mysql:5.7
# 使用如下命令启动MySQL服务
docker run -p 3307:3306 --name mysql \
-v /data/docker/mysql/log:/var/log/mysql \
-v /data/docker/mysql/data:/var/lib/mysql \
-v /data/docker/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7echo 'mysql初始化密码是 root '
echo '进入容器的命令是docker exec -it mysql env LANG=C.UTF-8 bash,普通docker exec -it mysql bash不行,会出现中文乱码'
echo 'docker exec -it mysql env LANG=C.UTF-8 bash'
安装git
yum -y install git
测试 git 是否安装成功 , 执行命令 git --version
[root@VM-16-6-centos ~]# git --version
git version 1.8.3.1
导入测试数据
方便测试,我就新建一个库,新建一个表,新建一条记录
进入 mysql 容器内部
docker exec -it mysql env LANG=C.UTF-8 bash
# 登录mysql
mysql -uroot -proot
[root@VM-16-6-centos ~]# docker exec -it mysql env LANG=C.UTF-8 bash
root@6747afd411ef:/# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.36 MySQL Community Server (GPL)Copyright © 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
登录 mysql 成功
# 建数据库
create database test4 charset utf8mb4;
use test4;
# 建表
CREATE TABLE `user1` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(16) DEFAULT NULL,PRIMARY KEY (`id`)
);
# 测试插入一条记录
insert into user1 select null , 'hello docker' ;
下载项目
测试目录在 /opt/test/ 目录(个人习惯)
# 新建测试目录
mkdir -p /opt/test/
# 进入测试目录
cd /opt/test/
# 下载项目
git clone https://gitee.com/renrenio/renren-generator.git
# 进入项目目录
cd renren-generator
# 新建测试目录
mkdir /opt/test/shell
# 项目打包(等一会,需要一段时间)
mvn clean package
修改项目的数据库连接信息
数据库连接地址,mysql容器暴露端口号,数据库用户名和密码
方便测试,我把 jar 包文件和 脚本 放到了 /opt/test/shell/ 目录下
mv /opt/test/renren-generator/target/renren-generator-1.0.0.jar /opt/test/shell/
执行脚本
在 /opt/test/shell 目录下新建 部署脚本
renren.sh
给脚本赋权限
chmod u+x renren.sh
- 启动命令
./renren.sh start
[root@VM-16-6-centos shell]# ./renren.sh start
Start /opt/test/shell/renren-generator-1.0.0.jar success…. ____ _ __ _ _
/\ / ’ __ _ () __ __ _ \ \ \
( ( )__ | '_ | '| | ’ / ` | \ \ \
\/ )| |)| | | | | || (| | ) ) ) )
’ || .__|| ||| |_, | / / / /
=========||==============|/=////
:: Spring Boot :: (v2.2.6.RELEASE)2021-11-30 22:53:14.004 INFO 13207 — [ main] io.renren.RenrenApplication : Starting RenrenApplication v1.0.0 on VM-16-6-centos with PID 13207 (/opt/test/shell/renren-generator-1.0.0.jar started by root in /opt/test/shell)
2021-11-30 22:53:14.008 INFO 13207 — [ main] io.renren.RenrenApplication : No active profile set, falling back to default profiles: default
2021-11-30T22:53:14.508+0800: [GC (Allocation Failure) [PSYoungGen: 245760K->6146K(253952K)] 245760K->6162K(516096K), 0.0193876 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
2021-11-30 22:53:15.251 WARN 13207 — [ main] o.m.s.mapper.ClassPathMapperScanner : Skipping MapperFactoryBean with name ‘oracleGeneratorDao’ and 'io.renren.da…
- 停止命令
./renren.sh stop
[root@VM-16-6-centos shell]# ./renren.sh stop
Stop /opt/test/shell/renren-generator-1.0.0.jar
/opt/test/shell/renren-generator-1.0.0.jar (pid:14288) exiting…
/opt/test/shell/renren-generator-1.0.0.jar exited.
- 查看服务状态命令
./renren.sh status
[root@VM-16-6-centos shell]# ./renren.sh status
/opt/test/shell/renren-generator-1.0.0.jar is running…
- 服务重启命令
./renren.sh restart
[root@VM-16-6-centos shell]# ./renren.sh restart
Stop /opt/test/shell/renren-generator-1.0.0.jar
/opt/test/shell/renren-generator-1.0.0.jar (pid:13505) exiting…
/opt/test/shell/renren-generator-1.0.0.jar exited.
Start /opt/test/shell/renren-generator-1.0.0.jar success…. ____ _ __ _ _
/\ / ’ __ _ () __ __ _ \ \ \
( ( )__ | '_ | '| | ’ / ` | \ \ \
\/ )| |)| | | | | || (| | ) ) ) )
’ || .__|| ||| |_, | / / / /
=========||==============|/=////
:: Spring Boot :: (v2.2.6.RELEASE)2021-11-30 22:58:27.528 INFO 14288 — [ main] io.renren.RenrenApplication : Starting RenrenApplication v1.0.0 on VM-16-6-centos with PID 14288 (/opt/test/shell/renren-generator-1.0.0.jar started by root in /opt/test/shell)
2021-11-30 22:58:27.545 INFO 14288 — [ main] io.renren.RenrenApplication : No active profile set, falling back to default profiles: default
2021-11-30T22:58:28.119+0800: [GC (Allocation Failure) [PSYoungGen: 245760K->6210K(253952K)] 245760K->6218K(516096K), 0.0220240 secs] [Times: user=0.04 sys=0.01, real=0.03 secs]
访问项目-生成代码
访问网址
ip:80
Application v1.0.0 on VM-16-6-centos with PID 14288 (/opt/test/shell/renren-generator-1.0.0.jar started by root in /opt/test/shell)
2021-11-30 22:58:27.545 INFO 14288 — [ main] io.renren.RenrenApplication : No active profile set, falling back to default profiles: default
2021-11-30T22:58:28.119+0800: [GC (Allocation Failure) [PSYoungGen: 245760K->6210K(253952K)] 245760K->6218K(516096K), 0.0220240 secs] [Times: user=0.04 sys=0.01, real=0.03 secs]
访问项目-生成代码
访问网址
ip:80
这篇关于shell脚本部署springboot(修订版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!