当前位置:网站首页>Assembly deployment process
Assembly deployment process
2022-06-23 22:08:00 【Great inventor】
For the moment ,Spring Boot The projects are as follows 2 There are two common ways to deploy
- One is to use docker Container to deploy . take Spring Boot The application is built into a docker image, Then start the image through the container . This way when large-scale applications need to be deployed and extended , It's very convenient , It belongs to the current industrial level deployment plan , But we need to master docker Ecosphere technology .
- The other is to use FatJar Direct deployment launch ( Will a jar And the three parties it depends on jar All in one bag , This bag is FatJar). This is a simple application deployment method for many beginners or small-scale situations .
Assembly The advantages of
It's described above Fatjar There are some flaws in the deployment scheme . Because if we directly build a Spring Boot Of FatJar
If it is deployed by the operation and maintenance personnel , The entire configuration file is hidden in jar
in , It is very difficult to modify the configuration file for different environments . If there is environmental uncertainty , Or when you need to start a script to start a project , This goes directly through jar
It's going to take a lot of work later .
And by assembly take Spring Boot Service oriented packaging , Can solve the problem mentioned above 2 A question
- bring Spring Boot Able to load jar External configuration file .
- Provide a service-oriented startup script , This script is usually shell perhaps windows Under the bat , With Spring Boot After the application service script of , It's easy to start and stop Spring Boot The application of .
Project configuration
Add the plug-in
(1) Edit the pom.xml file , Join in assembly Packaging plug-in .
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<!-- The main use is maven Provided assembly Plug in complete -->
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptors>
<!-- Specific configuration files -->
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- Bound to the maven In terms of operation type -->
<phase>package</phase>
<!-- To run a -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
(2) As you can see from the code above , hold assembly All of the configurations are in main/assembly Under the table of contents ( The files in the specific directory will be created next ).
Use Assembly Packaging and deployment Spring Boot engineering
Write service startup / Stop script
First, in the assembly Create one in the directory bin Folder , And then create a start.sh file , This is linux
The startup script under the environment , The details are as follows .
Tip: The project name at the beginning 、jar The package name doesn't need to be set manually , Here we use parameter variables , After the project is packaged, these parameters are automatically replaced with pom Of profiles in properties Value (assembly The configuration file needs to turn on the property replacement function ), The same is true for the other two configuration files below .
#!/bin/bash
# Project name
SERVER_NAME="${project.artifactId}"# jar name
JAR_NAME="${project.build.finalName}.jar"# Get into bin Catalog
cd `dirname $0`
# bin Directory absolute path
BIN_DIR=`pwd`
# Return to the root directory path of the upper level project
cd ..
# Print the absolute path of the project root
# `pwd` Execute system commands and get results
DEPLOY_DIR=`pwd`
# External configuration file absolute Directory , If it's a catalog, you need to / ending , You can also specify the file directly
# If you specify a directory ,spring All configuration files in the directory will be read
CONF_DIR=$DEPLOY_DIR/config
# SERVER_PORT=`sed '/server.port/!d;s/.*=//' config/application.properties | tr -d '\r'`
# Get the port number of the application
SERVER_PORT=`sed -nr '/port: [0-9]+/ s/.*port: +([0-9]+).*/\1/p' config/application.yml`
PIDS=`ps -f | grep java | grep "$CONF_DIR" |awk '{print $2}'`if [ "$1" = "status" ]; then
if [ -n "$PIDS" ]; then
echo "The $SERVER_NAME is running...!"
echo "PID: $PIDS"
exit 0
else
echo "The $SERVER_NAME is stopped"
exit 0
fi
fi
if [ -n "$PIDS" ]; then
echo "ERROR: The $SERVER_NAME already started!"
echo "PID: $PIDS"
exit 1
fi
if [ -n "$SERVER_PORT" ]; then
SERVER_PORT_COUNT=`netstat -tln | grep $SERVER_PORT | wc -l`
if [ $SERVER_PORT_COUNT -gt 0 ]; then
echo "ERROR: The $SERVER_NAME port $SERVER_PORT already used!"
exit 1
fi
fi
# Project log output absolute path
LOGS_DIR=$DEPLOY_DIR/logs
# If logs Folder does not exist , Create a folder
if [ ! -d $LOGS_DIR ]; then
mkdir $LOGS_DIR
fi
STDOUT_FILE=$LOGS_DIR/catalina.log
# JVM Configuration
JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
JAVA_DEBUG_OPTS=""
if [ "$1" = "debug" ]; then
JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
fi
JAVA_JMX_OPTS=""
if [ "$1" = "jmx" ]; then
JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
fi
JAVA_MEM_OPTS=""
BITS=`java -version 2>&1 | grep -i 64-bit`
if [ -n "$BITS" ]; then
JAVA_MEM_OPTS=" -server -Xmx512m -Xms512m -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
else
JAVA_MEM_OPTS=" -server -Xms512m -Xmx512m -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
fi
# Load external log4j2 Configuration of files
LOG_IMPL_FILE=log4j2.xml
LOGGING_CONFIG=""
if [ -f "$CONF_DIR/$LOG_IMPL_FILE" ]
then
LOGGING_CONFIG="-Dlogging.config=$CONF_DIR/$LOG_IMPL_FILE"
fi
CONFIG_FILES=" -Dlogging.path=$LOGS_DIR $LOGGING_CONFIG -Dspring.config.location=$CONF_DIR/ "
echo -e "Starting the $SERVER_NAME ..."
nohup java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES -jar $DEPLOY_DIR/lib/$JAR_NAME > $STDOUT_FILE 2>&1 &
COUNT=0
while [ $COUNT -lt 1 ]; do
echo -e ".\c"
sleep 1
if [ -n "$SERVER_PORT" ]; then
COUNT=`netstat -an | grep $SERVER_PORT | wc -l`
else
COUNT=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}' | wc -l`fi
if [ $COUNT -gt 0 ]; then
break
fi
done
echo "OK!"
PIDS=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`echo "PID: $PIDS"
echo "STDOUT: $STDOUT_FILE"
And then create a stop.sh file , This is linux Stop script in the environment , The details are as follows .
#!/bin/bash
# Project name
APPLICATION="${project.artifactId}"# Project start jar Package name
APPLICATION_JAR="${project.build.finalName}.jar"# Find... By project name PI, then kill -9 pid
PID=$(ps -ef | grep "${APPLICATION_JAR}" | grep -v grep | awk '{ print $2 }')if [[ -z "$PID" ]]
then
echo ${APPLICATION} is already stoppedelse
echo kill ${PID} kill -9 ${PID} echo ${APPLICATION} stopped successfullyfi
Finally, create a start.bat file , This is Windows The startup script under the environment , The details are as follows .
echo off
set APP_NAME=${project.build.finalName}.jarset LOG_IMPL_FILE=log4j2.xml
set LOGGING_CONFIG=
if exist ../config/%LOG_IMPL_FILE% (
set LOGGING_CONFIG=-Dlogging.config=../config/%LOGGING_CONFIG%
)
set CONFIG= -Dlogging.path=../logs %LOGGING_CONFIG% -Dspring.config.location=../config/
set DEBUG_OPTS=
if ""%1"" == ""debug"" (
set DEBUG_OPTS= -Xloggc:../logs/gc.log -verbose:gc -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs
goto debug
)
set JMX_OPTS=
if ""%1"" == ""jmx"" (
set JMX_OPTS= -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9888 -Dcom.sun.management.jmxremote.ssl=FALSE -Dcom.sun.management.jmxremote.authenticate=FALSE
goto jmx
)
echo "Starting the %APP_NAME%"
java -Xms512m -Xmx512m -server %DEBUG_OPTS% %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
echo "java -Xms512m -Xmx512m -server %DEBUG_OPTS% %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%"
goto end
:debug
echo "debug"
java -Xms512m -Xmx512m -server %DEBUG_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end
:jmx
java -Xms512m -Xmx512m -server %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end
:end
pause
Create a packaging profile
Last , We are assembly Create one under the folder assembly.xml The configuration file , The details are as follows .
<assembly>
<!--
Must write , Otherwise, there will be assembly ID must be present and non-empty error
This name will eventually be appended to the end of the package name , If the name of the project is hangge-test-0.0.1-SNAPSHOT,
Then the final generated package name is hangge-test-0.0.1-SNAPSHOT-bin.tar.gz
-->
<id>bin</id>
<!-- Type of packaging , If there is N individual , Will fight N Types of bags -->
<formats>
<format>tar.gz</format>
<!--<format>zip</format>-->
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<!-- File settings -->
<fileSets>
<!--
0755-> That is, the user has read / Write / Executive authority , Group users and other users have read and write permissions ;
0644-> That is, the user has read and write permission , Group users and other users have read-only rights ;
-->
<!-- take src/main/assembly/bin All files in the directory are output to the packaged bin Directory -->
<fileSet>
<directory>src/main/assembly/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<!-- If it's a script , It must be changed to unix. If it's in windows It's coded , There will be dos Writing questions -->
<lineEnding>unix</lineEnding>
<filtered>true</filtered><!-- Whether to make attribute replacement -->
</fileSet>
<!-- take src/main/assembly/config All files in the directory are output to the packaged config Directory -->
<fileSet>
<directory>src/main/assembly/config</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<!-- take src/main/resources Package the configuration file to config Catalog -->
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>/config</outputDirectory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.yml</include>
</includes>
<filtered>true</filtered><!-- Whether to make attribute replacement -->
</fileSet>
<!-- Start the project jar Pack to lib Directory -->
<fileSet>
<directory>target</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<!-- Package project description documents into docs Directory -->
<fileSet>
<directory>.</directory>
<outputDirectory>docs</outputDirectory>
<includes>
<include>*.md</include>
</includes>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>docs</directory>
<outputDirectory>docs</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>src/main/assembly/docs</directory>
<outputDirectory>docs</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
</fileSets>
</assembly>
Package test
Packaging project
- We use mvn package Order to package the project .
- After packing, it's in target Next, it will generate a file named xxx.tar.gz The compressed file of .
- After decompressing the package, you can see the following directory inside .
Start the service
- After unpacking the above package file , stay bin The directory has the following startup files
- Linux、macOS System : perform start.sh Start the service , perform stop.sh Out of Service .
- Windows System : double-click start.bat Start the service
- After the service starts , The corresponding log file will be generated to logs Under the table of contents (logs The directory is created automatically )
Modify the configuration
- modify config Profile under folder , The configuration file here is application.properties.
- Here we change the service port to 9090.
server.port=9090
- Restart the service , You can see that the port did change , Indicates that the external configuration file was loaded successfully .
Package projects and dependencies separately
thus , The use described above assembly When you package a project , The project code and all the dependent files of the project will form an executable jar package .
If the project has many dependent packages , Then the file will be very large . If you want to upload the whole version every time you release it jar package , It's a waste of bandwidth and time .
Here's how to package the external dependencies of a project separately from your own code package , So when the project is modified , You just need to upload the modified package .
Modify the configuration
- First we edit assembly.xml The configuration file . Add third-party dependency settings based on the previous one , Implementation will third party's jar Package added to the compressed package lib Catalog .
<assembly>
<!--
Must write , Otherwise, there will be assembly ID must be present and non-empty error
This name will eventually be appended to the end of the package name , If the name of the project is hangge-test-0.0.1-SNAPSHOT,
Then the final generated package name is hangge-test-0.0.1-SNAPSHOT-bin.tar.gz
-->
<id>bin</id>
<!-- Type of packaging , If there is N individual , Will fight N Types of bags -->
<formats>
<format>tar.gz</format>
<!--<format>zip</format>-->
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<!-- Third party dependency settings -->
<dependencySets>
<dependencySet>
<!-- Don't use project artifact, The third party jar Don't unzip , Pack in zip Of documents lib Catalog -->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<!-- File settings -->
<fileSets>
<!--
0755-> That is, the user has read / Write / Executive authority , Group users and other users have read and write permissions ;
0644-> That is, the user has read and write permission , Group users and other users have read-only rights ;
-->
<!-- take src/main/assembly/bin All files in the directory are output to the packaged bin Directory -->
<fileSet>
<directory>src/main/assembly/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<!-- If it's a script , It must be changed to unix. If it's in windows It's coded , There will be dos Writing questions -->
<lineEnding>unix</lineEnding>
<filtered>true</filtered><!-- Whether to make attribute replacement -->
</fileSet>
<!-- take src/main/assembly/config All files in the directory are output to the packaged config Directory -->
<fileSet>
<directory>src/main/assembly/config</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<!-- take src/main/resources Package the configuration file to config Catalog -->
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>/config</outputDirectory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.yml</include>
</includes>
<filtered>true</filtered><!-- Whether to make attribute replacement -->
</fileSet>
<!-- Start the project jar Pack to lib Directory -->
<fileSet>
<directory>target</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<!-- Package project description documents into docs Directory -->
<fileSet>
<directory>.</directory>
<outputDirectory>docs</outputDirectory>
<includes>
<include>*.md</include>
</includes>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>docs</directory>
<outputDirectory>docs</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>src/main/assembly/docs</directory>
<outputDirectory>docs</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
</fileSets>
</assembly>
- Then edit the project's pom.xml file , It was previously used spring-boot-maven-plugin To pack , This plug-in inserts all the dependencies of the project into the project jar Inside the package . We'll replace it with maven-jar-plugin, And relevant Settings .
<build>
<plugins>
<!-- Specify the startup class , Make dependency external jar package -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<!-- Generated jar in , Do not include pom.xml and pom.properties These two documents -->
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<!-- Whether or not to put a third party jar Add to the class build path -->
<addClasspath>true</addClasspath>
<!-- External dependence jar The final location of the package -->
<!-- Because we're going to have a third party jar And this project jar Put it in the same directory , Here it is ./ -->
<classpathPrefix>./</classpathPrefix>
<!-- Project start class -->
<mainClass>com.example.hanggetest.HanggeTestApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<!-- The main use is maven Provided assembly Plug in complete -->
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptors>
<!-- Specific configuration files -->
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- Bound to the maven In terms of operation type -->
<phase>package</phase>
<!-- To run a -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
边栏推荐
- How to deal with high memory in API gateway how to maintain API gateway
- Intel openvino tool suite advanced course & experiment operation record and learning summary
- Make it simple. This wave of global topology is quite acceptable!
- The transaction code mp83 at the initial level of SAP retail displays a prediction parameter file
- Knowda: all in one knowledge mixture model for data augmentation in feed shot NLP
- How does the API gateway intercept requests? How does the security of the API gateway reflect?
- Error running PyUIC: Cannot start process, the working directory ‘-m PyQt5. uic. pyuic register. ui -o
- A batch layout WAF script for extraordinary dishes
- [js] generate random array
- Ten thousand words! Understand the inheritedwidget local refresh mechanism
猜你喜欢

MySQL de duplication query only keeps one latest record

Polar cycle graph and polar fan graph of high order histogram

Ten thousand words! Understand the inheritedwidget local refresh mechanism

Simple code and design concept of "back to top"

万字长文!一文搞懂InheritedWidget 局部刷新机制

Data visualization: summer without watermelon is not summer
Performance optimization of database 5- database, table and data migration

使用 Provider 改造屎一样的代码,代码量降低了2/3!

微信小程序中发送网络请求

从CVPR 2022看域泛化(Domain Generalization)最新研究进展
随机推荐
[同源策略 - 跨域问题]
Lighthouse open source application practice: snipe it
The most common usage scenarios for redis
After CVM is configured with IPv6, it cannot be accessed as IPv6 or cannot access IPv6 sites
Selenium批量查询运动员技术等级
Data visualization: summer without watermelon is not summer
Selenium batch query athletes' technical grades
Minimize outlook startup + shutdown
Cloud database smooth disassembly scheme
Intel openvino tool suite advanced course & experiment operation record and learning summary
How to improve the high concurrency of the server
Minimisé lorsque Outlook est allumé + éteint
How to wrap QR code data
How to provide value for banks through customer value Bi analysis
The "Star" industry in the small town is escorted by wechat cloud hosting
MySQL de duplication query only keeps one latest record
How to calculate individual income tax? You know what?
Tencent cloud commercial password compliance solution appears at the 2021 high-end Seminar on commercial password application innovation
After easydss is configured with domain name / public IP, it will always prompt for troubleshooting problems that do not exist in the service
Kubernetes cluster lossless upgrade practice