shell script deployment springboot (Revised)

Keywords: Java Operation & Maintenance Spring Spring Boot intellij-idea

shell script deployment springboot (Revised)

explain

The shell deployment springboot was sorted out before

website: https://blog.csdn.net/shaoming314/article/details/120145215

However, the shell script does not have the option to stop, view and restart the service. It is not very good. This revision supports the options of stopping, restarting and viewing the service running status

The script is as follows

renren.sh

#!/bin/bash
# Packaging with maven


# Program name (location of jar package)
APP_NAME=/opt/test/shell/renren-generator-1.0.0.jar

# Log file path and name (self setting)
LOG_FILE=/opt/test/shell/renren-genertor.log

# jvm parameters for project startup (self setting)
JVM_OPTS="-Dname=$AppName  -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"

# Project startup port number (self setting)
PORT=80

# Judge the existence of project jar package trial
if [ ! -f  $APP_NAME ];
then
    echo  "Item not found jar package"
    exit 1
fi

# Operate the item according to the selection
if [ "$1" = "" ];
then
    echo -e "\033[0;31m Operation name not entered \033[0m  \033[0;34m {start|stop|restart|status} \033[0m"
    exit 1
fi

# Open project
function start()
{
    PID=`ps -ef |grep java|grep $APP_NAME|grep -v grep|awk '{print $2}'`

        if [ x"$PID" != x"" ]; then
            echo "$APP_NAME is running..."
        else
                nohup java $JVM_OPTS  -jar $APP_NAME --server.port=$PORT > $LOG_FILE 2>&1 &
                echo "Start $APP_NAME success..."
                # View project startup log
                tail -f $LOG_FILE
        fi
}

# Stop project
function stop()
{
    echo "Stop $APP_NAME"

        PID=""
        query(){
                PID=`ps -ef |grep java|grep $APP_NAME|grep -v grep|awk '{print $2}'`
        }

        query
        if [ x"$PID" != x"" ]; then
                kill -9 $PID
                echo "$APP_NAME (pid:$PID) exiting..."
                while [ x"$PID" != x"" ]
                do
                        sleep 1
                        query
                done
                echo "$APP_NAME exited."
        else
                echo "$APP_NAME already stopped."
        fi
}

# Restart project
function restart()
{
    stop
    sleep 2
    start
}

# Check the running status of the project to see whether it is running
function status()
{
    PID=`ps -ef |grep java|grep $APP_NAME|grep -v grep|wc -l`
    if [ $PID != 0 ];then
        echo "$APP_NAME is running..."
    else
        echo "$APP_NAME is not running..."
    fi
}


case $1 in
    start)
    start;;
    stop)
    stop;;
    restart)
    restart;;
    status)
    status;;
    *)

esac

Reference script

This script refers to the startup script of ruoyi project

website: https://gitee.com/y_project/RuoYi/blob/master/ry.sh

Basic project

renren-generator

Project git warehouse address: https://gitee.com/renrenio/renren-generator

Project description

  • Renren generator is the code generator of Renren open source project. It can generate entity, xml, dao, service, html, js and sql codes online, reducing development tasks by more than 70%

Using technology stack

  • springboot
  • mysql
  • mybatis-plus
  • . . . . .

Reasons for using this item to test

  1. The project is simple and easy to deploy
  2. The project is a good open source code generation tool
  3. The project is easy to start and easy to test

Environmental preparation

You can select ECs and virtual machine for settings

My running environment is Tencent cloud lightweight cloud server, and the system version is CentOS 7

Install jdk

yum -y install java-1.8.0-openjdk*

Test for successful installation

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 installed successfully

Installing maven

set up script

instal-maven3.6.1.sh

# Download maven installation package
wget https://archive.apache.org/dist/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz
# Unzip the installation package
tar -zxvf apache-maven-3.6.1-bin.tar.gz
# Move maven to the / usr/local / directory
mv apache-maven-3.6.1 /usr/local/maven
echo 'maven Your directory is /usr/local/maven'
#Configure maven's environment variables
echo 'export MAVEN_HOME=/usr/local/maven' >> /etc/profile
echo 'export PATH=$PATH:$MAVEN_HOME/bin' >> /etc/profile

# Refresh environment variables
source /etc/profile

echo 'Alibaba cloud image addresses are as follows:'
#Configuring alicloud images
echo '<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>Alibaba cloud public warehouse</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
'

#Empty maven's settings file 
echo '' > /usr/local/maven/conf/settings.xml

# Configure maven's settings file, set the image to Alibaba cloud image, and submit depends on the download speed (the configuration annotated in maven's remote configuration file is removed, so the content is relatively small, so you can install it yourself and configure it according to your own environment)
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>Alibaba cloud public warehouse</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
  </mirrors>
  <profiles>
  </profiles>
</settings>
EOF

# Test whether maven is installed successfully
mvn -v

Test whether maven is installed successfully and execute the command 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"

Install docker

install-docker.sh

#!/bin/bash
## Automatically install Docker using yum, and configure Docker using Yum source 2020-05-31
## Problem feedback https://aq2.cn/c/docker
## email: ppabc@qq.com
## robert yu
## redhat 7

##install
## 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 private can add "secure registers": ["harbor. IO", "k8s. GCR. IO", "GCR. IO", "quad. IO"],
## Uninstall docker command 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


##Configure docker source
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


##start-up
systemctl daemon-reload 
systemctl enable docker
systemctl restart docker
docker version

Test whether docker verson is installed

[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: false

Server: 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 installed successfully

Install mysql

To facilitate quick testing, I use docker to install mysql here

docker installation mysql5.7 script

docker-install-mysql57.sh

# Pull mysql5.7 image
docker pull mysql:5.7
# Start the MySQL service with the following command
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.7

echo 'mysql The initialization password is root '
echo 'The command to enter the container is docker exec -it mysql env LANG=C.UTF-8 bash,ordinary docker exec -it mysql bash no way,Chinese garbled code will appear'
echo 'docker exec -it mysql env LANG=C.UTF-8 bash'

Install git

yum -y install git

Test whether Git is successfully installed and execute the command git --version

[root@VM-16-6-centos ~]# git --version
git version 1.8.3.1

Import test data

To facilitate the test, I will create a new library, a new table and a new record

Enter the mysql container

docker exec -it mysql env LANG=C.UTF-8 bash
# Login to 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>

Login to mysql succeeded

# Build database
create database test4 charset utf8mb4;
use test4;
# Build table
CREATE TABLE `user1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(16) DEFAULT NULL,
  PRIMARY KEY (`id`)
);
# Test insert a record
insert into user1 select null , 'hello docker' ;

Download project

The test directory is in / opt/test / directory (personal habits)

# New test directory
mkdir -p /opt/test/
# Enter test directory
cd /opt/test/
# Download project
git clone https://gitee.com/renrenio/renren-generator.git
# Enter project directory
cd renren-generator
# New test directory
mkdir /opt/test/shell
# Project packaging (wait a minute, it will take some time)
mvn clean package

Modify the database connection information of the project

Database connection address, mysql container exposure port number, database user name and password

To facilitate testing, I put the jar package files and scripts in the / opt/test/shell / directory

 mv /opt/test/renren-generator/target/renren-generator-1.0.0.jar  /opt/test/shell/

Execute script

Create a new deployment script in the / opt/test/shell directory

renren.sh

Assign permissions to scripts

chmod u+x renren.sh

  1. Start command

./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

...

  1. Stop command

./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.

  1. View service status command

./renren.sh status

[root@VM-16-6-centos shell]# ./renren.sh status
/opt/test/shell/renren-generator-1.0.0.jar is running...

  1. Service restart command

./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]

Access project - generate code

Visit website

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]

Access project - generate code

Visit website

ip:80

Posted by vchris on Tue, 30 Nov 2021 07:31:31 -0800