- Target
- iteration steps
- 1. Premise: There is a SpringBoot project jar package that can be started in the Windows environment
- 2. Start the jar package in the linux environment
-
- 1. First throw the jar package into the linux environment
- 2. Test whether it can be started (Java environment is required)
- 3. Optimize the startup script, change it to start in the background, and output the log to `springboot.log`
- 4. Improve the script. When starting, if there is a service that has already started, close it first and then start it again
- 3. Separate dependency deployment
- 4. Use with the Alibaba Cloud Toolkit plug-in to upload files with one click and execute scripts
Target
一键
Deploy the locally packagedSpringBoot
project tolinux
the environment to achieve秒级
deployment
Pre-knowledge: master Maven packaging
iteration steps
- Use the command to start the SpringBoot project in the windows environment
- Use the command to start the project in the Linux environment
- Use a script to start the project in the Linux environment
- Optimize the script to close the project first every time it restarts
- Separation of dependencies, uploading only hundreds of k code packages each time the Linux environment is uploaded
- With IDEA plug-in, realize one-click deployment in IDEA
1. Premise: There is a SpringBoot project jar package that can be started in the Windows environment
1. Use Maven's package command to create a jar package with dependencies
For example:
2. Window starts the jar package
java -jar SpringBootTest-0.0.1-SNAPSHOT.jar
After the startup is complete, it means that the jar package can be started normally.
2. Start the jar package in the linux environment
1. First throw the jar package into the linux environment
2. Test whether it can be started (Java environment is required)
java -jar SpringBootTest-0.0.1-SNAPSHOT.jar
Note that it can also be started normally in the Linux environment
But there is a problem with the above startup method. Once the window is closed, the project will be automatically closed.
3. Optimize the startup script, change it to start in the background, and output the log tospringboot.log
nohup java -jar SpringBootTest-0.0.1-SNAPSHOT.jar > springboot.log &
Started successfully, the process number is 9777
There is also a problem with the above script. When starting for the second time, because a service has already been started, the port is occupied and cannot be started.
4. Improve the script. When starting, if there is a service that has already started, close it first and then start it again
create script
vim start.sh
Script content
# 关闭程序
# fileName为jar包的名称
fileName=SpringBootTest-0.0.1-SNAPSHOT.jar
pid=$(ps -ef | grep $fileName| grep -v "grep" | awk '{print $2}')
kill -9 $pid
# 启动项目
nohup java -jar $fileName > springboot.log &
After that, start the project with
sh start.sh
So far, the start of the SpringBoot project in Linux has been explained.
However, there are still some problems in the above deployment method. When there is only web dependency, the size of the jar has reached 17M.
In actual development, the size of the jar package will even reach more than one hundred megabytes. For example like this:
Why is the size of the package so large when there is not much code?
Unzip the SpringBootTest-0.0.1-SNAPSHOT.jar package to view the content
It can be seen that the lib folder occupies 16.7M, and what is in the lib folder?
As you can see, there are various dependencies.
Therefore, a new optimization direction has been triggered. Can you put the dependent package directly on the server and only update your own code each time?
The answer is: yes! In actual development, generally speaking, dependent packages will not move, so start the third step.
3. Separate dependency deployment
1. Upload the dependent jar package to the Linux server
(1) Create a lib folder
mkdir lib
(2) /BOOT-INF/lib
Upload all the jar packages under the directory in the jar package to the lib folder of the Linux server
2. Transform the pom.xml file of the project
Add configuration:
<build>
<plugins>
<!-- 解决jar中没有主清单属性问题 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.springboottest.SpringBootTestApplication</mainClass>
<!-- jar包不携带依赖配置开始-->
<layout>ZIP</layout>
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
<!-- jar包不携带依赖部署配置结束-->
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3. Repackage
The newly generated jar package only has156kb!
4. Upload to the server
At this time, if you use the original script to start, an error will be reported:
5. Use -Dloader.path to specify external dependencies
(1) Add startup filesstart2.sh
cp start.sh start2.sh
(2) Append after java -jar-Dloader.path=./lib
# 关闭程序
fileName=SpringBootTest-0.0.1-SNAPSHOT.jar
pid=$(ps -ef | grep $fileName| grep -v "grep" | awk '{print $2}')
kill -9 $pid
# 启动项目
nohup java -jar -Dloader.path=./lib $fileName > springboot.log &
Successful startup with external dependencies
At this point, the use of external dependencies to start is complete, just add
-Dloader.path=./lib
it
6. Merge start.sh
and start2.sh
Script
There is still a problem in the fifth step. Although this method works well, it adds another script. Inside the company, there are always people who want to build full packages and use the built-in dependencies. Some people want to build packages without dependencies so that they can be quickly deployed and started. At this time, it is necessary to distinguish whether to use scripts or
start.sh
scriptsstart2.sh
. Therefore, it will be mergedstart.sh
with the script. When it is larger than 10M, it will use internal dependencies, and when it is smaller than 10M, it will use external dependencies.start2.sh
The merged start.sh script:
# 关闭程序
fileName=SpringBootTest-0.0.1-SNAPSHOT.jar
pid=$(ps -ef | grep $fileName| grep -v "grep" | awk '{print $2}')
kill -9 $pid
# 获取jar包的大小
filesize=`ls -l $fileName | awk '{ print $5 }'`
# 多少M以上使用外部依赖
maxsize=$((1024 * 1024 * 10)) # 10M
if [ $filesize -gt $maxsize ]
then
echo "文件大小为【$filesize】,使用内部依赖启动"
nohup java -jar -Dloader.path=./lib $fileName > springboot.log &
else
echo "文件大小为【$filesize】,使用外部依赖启动"
nohup java -jar $fileName > springboot.log &
fi
Adapt to two situations
jar packages smaller than 10M use external dependencies
jar packages larger than 10M use internal dependencies
After the third step is optimized, the size of the jar package is greatly reduced to less than 1M, and each upload takes less than 1 second. Can we continue to optimize?
The answer is yes! The current problems are:
1. You need to manually select the file to upload.
2. You need to manually execute the script
Therefore, the fourth step of optimization is to introduce the Alibaba Cloud Toolkit plug-in
4. Use with the Alibaba Cloud Toolkit plug-in to upload files with one click and execute scripts
1. IDEA installs the Alibaba Cloud Toolkit plug-in
1.1 Download the Alibaba Cloud Toolkit plug-in from the plug-in market, and restart IDEA.
2. Configure the server address
You can see that a new configuration has been added
3. Configure the upload address and the executed command
View the path
Click Upload to configure the uploaded file, upload address and executed script
add execution command