preface

Any system will involve deployment, and the frequency of deployment will be very high during the system test and commissioning. How to improve the deployment efficiency is also a consideration for rapid development framework.

Deployment plan

  • Plan 1 (not very recommended)

  1. Backup of old packages on server
  2. Local package
  3. Upload the local package to the server
  4. Restart the service

Implementation: Either manually or via maven plugins such as wagon- SSH.

  • Plan 2 (Key points of this paper)

  1. Backup of old packages on server
  2. Pull the latest code from the server
  3. Packaging on the server
  4. Restart the service

Implementation: Generally, one-click publishing scripts are written in this way.

  • Option 3 (strongly recommended conditionally)

Using Jenkins, the assembly line is really fragrant.

In fact, no matter what kind of program is used, the final implementation will have the crossover part, just the crossover part is manual, semi-automatic, or automatic problem.

One-click release script design

The environment that

Because Docker involves a bit more content than this article can finish, so for the sake of simplicity, docker will not be used here for the time being. But there will be a series of docker articles.

Operating system:

[root@mldong ~]# cat /etc/redhat-release 
CentOS Linux release 8.1.1911 (Core) 
Copy the code

JDK version

[root@mldong ~]# java -version
java version "1.8.0 comes with _131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
Copy the code

Maven version

[root@mldong ~]# mvn -vApache Maven 3.5.3 (3383 c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00) Maven home: /usr/local/maven/apache-maven-3.5.3
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /usr/local/ Java /jdk1.8.0_131/ JRE Default locale: en_US, platform Encoding: UTF-8 OS name:"linux", version: "4.18.0-147.8.1. El8_1. X86_64", arch: "amd64", family: "unix"
Copy the code

The git version

[root@mldong ~]# git --versionThe git version 2.18.2Copy the code

The installation of the basic environment is not introduced here, if there is time later, will consider a one-click installation of the above environment script.

Common Commands

Git git

Clone the latest code – username + password mode:
git clone https://username:[email protected]/mldong/mldong.git
Copy the code

If username contains @, use %40 instead, for example:

git clone https://524719755%40qq.com:[email protected]/mldong/mldong.git
Copy the code

Of course, this project is open source and does not require an account password

git clone https://gitee.com/mldong/mldong.git
Copy the code
Clone the latest code – public key deployment
git clone [email protected]:mldong/mldong.git
Copy the code

You can check whether the public key exists first

cat ~/.ssh/id_rsa.pub
Copy the code

If the public key does not exist, run the following command on the server to generate it:

ssh-keygen -t rsa -C "[email protected]" 
Copy the code

[email protected] is a personal email address

Press Enter three times to view the public key

cat ~/.ssh/id_rsa.pub
Copy the code

Configuring a Public Key on the Code cloud is omitted (Management -> Deploying Public Key Management -> Adding a Public Key).

Pull the latest code

Suppose the source directory is /java_projects/source/mldong

Go to the source directory first

cd /java_projects/source/mldong
Copy the code

Pull the latest code

git pull
Copy the code
View the current version
git rev-parse HEAD
Copy the code

Maven commands

Clean and pack
mvn clean package -B 
Copy the code
Skipping unit tests
mvn clean package  -B -DskipTests
Copy the code
Specify a configuration file
mvn clean package -B -s /root/.m2/settings-docker.xml -DskipTests 
Copy the code

Java commands

Start the service
java -jar xxx.jar
Copy the code
Start the service and specify the maximum heap and minimum heap
java -server -Xms512m -Xmx512m xxx.jar
Copy the code
Start the service and specify the port and configuration file
java -server -Xms512m -Xmx512m  xxx.jar --server.port=18081 --spring.profiles.active=test 
Copy the code
Start the service and run it in the background
nohup java -server -Xms512m -Xmx512m xxx.jar --server.port=18081 --spring.profiles.active=test > nohup.log
Copy the code
If log already has logs, delete nohup.log
nohup java -server -Xms512m -Xmx512m xxx.jar --server.port=18081 --spring.profiles.active=test > /dev/null 2>log &
Copy the code

Linux commands

Query a service that contains xxx.jar
ps -ef | grep xxx.jar | grep -v grep
Copy the code

Sample:

[root@mldong ~]# ps -ef | grep mldong-admin.jar
root     17504     1  0 15:07 ?        00:00:46 java -server -Xms512m -Xmx512m  -jar /java_projects/mldong-admin/mldong-admin.jar --server.port=18081 --spring.profiles.active=test
root     19118 14014  0 21:00 pts/0    00:00:00 grep --color=auto mldong-admin.jar
Copy the code
Query a service that contains xxx.jar and remove the grep line
ps -ef | grep xxx.jar | grep -v grep
Copy the code

Sample:

[root@mldong ~]# ps -ef | grep mldong-admin.jar
root     17504     1  0 15:07 ?        00:00:46 java -server -Xms512m -Xmx512m  -jar /java_projects/mldong-admin/mldong-admin.jar --server.port=18081 --spring.profiles.active=test
Copy the code
Query a service that contains xxx.jar and get its PID
ps -ef | grep xxx.jar | grep -v grep | awk '{print $2}'
Copy the code

Sample:

[root@mldong ~]# ps -ef | grep mldong-admin.jar | grep -v grep | awk '{print $2}'
17504
Copy the code
Kill a server process
kill -9 pid
Copy the code

As above:

kill -9 17504
Copy the code

Start coding

Directory description

├ ─ ─ / java_projects management end interface ├ ─ ─ mldong - admin ├ ─ ─ the config └ ─ ─ application - test. Yml ├ ─ ─ logs log directory ├ ─ ─ mldong - admin. Jar jar package ├ ─ ─ buildAndRestart. Sh rebuilding project and restart the service script ├ ─ ─ restart. Sh restart service script └ ─ ─ nohub. Out of the current service run log └ ─ ─source├ ─ ─ mldong source root ├ ─ ─ mldong - admin └ ─ ─ pom. The XML ├ ─ ─ mldong - common └ ─ ─ pom. The XML ├ ─ ─ mldong - generator └ ─ ─ pom. The XML ├ ─ ─ Mldong - mapper └ ─ ─ pom. XMLCopy the code

Core File Description

  • /java_projects/mldong-admin/restart.sh

Restart service script

#! /bin/bash
jar_name=mldong-admin.jar
jar_path=/java_projects/mldong-admin
jar_pid=$(ps -ef | grep $jar_path/$jar_name | grep -v grep | awk '{print $2}')
echo "$jar_name PID $jar_pid"
if [ -n "$jar_pid" ]
then
	#!kill-9 Forcibly terminateEcho "kill -9 Forced termination "$jar_pid kill -9 $jar_pid fi nohup java-server-xms512m -xmx512m -jar $jar_path/$jar_name --server.port=18081 --spring.profiles.active=test > $jar_path/nohub.out & echo "**********************$jar_name*************************"Copy the code
  • /java_projects/mldong-admin/buildAndRestart.sh

Rebuild the project and restart the service script

#! /bin/bash
#Source code in the root directory
source_dir=/java_projects/source
#Parent project directory
parent_dir=$source_dir/mldong
#Name of the project that needs to be packaged
project_name=mldong-admin
#Project Deployment Directory
project_dir=/java_projects/$project_name
#Backup directory
backup_dir=/backup/$project_name
#Git repository address (SSH mode, need to configure deployment public key)[email protected]:mldong/mldong.git if [ -f "$project_dir/$project_name.jar" ]; Jar $backup_dir/ 'date +"%Y%m%d%H% m% S"'. Jar fi if [ -d "$source_dir" ]; ${source_dir}" cp -p $source_dir fi if [-d "$parent_dir"]; ${source_dir} fi if [-d "$parent_dir"];  Git pull" CD $parent_dir git pull else echo" Git clone" Git clone $git_URL $parent_dir fi git_version=$(git rev-parse HEAD) echo "git rev :${git_version}"#Go to the project source directory
cd $parent_dir
#packaging
mvn clean package -B
#Here you need to determine whether the packaging was successfulif [ $? -ne 0 ]; Then echo "failed to pack" else  #Copy the new package to the deployment directory
  cp $parent_dir/$project_name/target/$project_name.jar $project_dir/$project_name.jar
  #Enter the Deployment project
  cd $project_dir
  #Restart the service
  bash restart.sh
fi
Copy the code

summary

This script only completes the simple one-click publishing function, and does not properly handle backup and restoration, exception handling, etc., but the general rapid publishing scenario is basically satisfied.

Project source code address

  • The back-end

Gitee.com/mldong/mldo…

  • The front end

Gitee.com/mldong/mldo…

Related articles

Create a suitable for their own rapid development framework – the pilot

Build a suitable for their own rapid development framework – back-end scaffolding

Build a fast development framework for yourself – integrated Mapper

Build a fast development framework for yourself – integration with Swaggerui and KNIfe4J

Build a suitable for their own rapid development framework – universal class packaging unified result return, unified exception handling

Create a quick development framework for yourself – business error code specifications and practices

Build a quick development framework for yourself – framework layering and CURD sample

Create a suitable for their own rapid development framework – Mapper logical deletion and enumeration type specification

Create a suitable framework for rapid development – Hibernate Validator data verification

Create a suitable for their own rapid development framework – code generator principle and implementation

Create a suitable for their own rapid development framework – universal query design and implementation

Build a suitable rapid development framework – rBAC-based permission management

Build a quick development framework for yourself – login and permission blocking

Build a suitable for their own rapid development framework – HTTP request log global processing

Create a suitable for their own agile development framework – upload module design and implementation