Deploy the local SpringBoot project jar package to the Linux environment in 1 second (super detailed)

Target

一键Deploy the locally packaged SpringBootproject to linuxthe environment to achieve 秒级deployment
Pre-knowledge: master Maven packaging

iteration steps

  1. Use the command to start the SpringBoot project in the windows environment
  2. Use the command to start the project in the Linux environment
  3. Use a script to start the project in the Linux environment
  4. Optimize the script to close the project first every time it restarts
  5. Separation of dependencies, uploading only hundreds of k code packages each time the Linux environment is uploaded
  6. 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:

image-20221212222543292

2. Window starts the jar package

java -jar SpringBootTest-0.0.1-SNAPSHOT.jar

image-20221212222726325

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

image-20221212223047762

2. Test whether it can be started (Java environment is required)

java -jar SpringBootTest-0.0.1-SNAPSHOT.jar 

image-20221212223419188

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 &

image-20221212224437149

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.

image-20221212224900449

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.

image-20221212230932820

In actual development, the size of the jar package will even reach more than one hundred megabytes. For example like this:

image-20221212231306254

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

image-20221212231557682

It can be seen that the lib folder occupies 16.7M, and what is in the lib folder?

image-20221212231701412

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

image-20221212234058558

(2) /BOOT-INF/libUpload all the jar packages under the directory in the jar package to the lib folder of the Linux server

image-20221212234121085

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>

image-20221212234306062

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:

image-20221212234731533

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

image-20221213000012544

At this point, the use of external dependencies to start is complete, just add -Dloader.path=./libit

6. Merge start.shand start2.shScript

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.shscripts start2.sh. Therefore, it will be merged start.shwith 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

image-20221213001042683

jar packages larger than 10M use internal dependencies

image-20221213001052764

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.

image-20221213001348655

2. Configure the server address

image-20221213001619732

image-20221213001829872

You can see that a new configuration has been added

image-20221213001856838

3. Configure the upload address and the executed command

View the path

image-20221213002904012

Click Upload to configure the uploaded file, upload address and executed script

image-20221213002141765

add execution command

image-20221213002232898

image-20221213003705741

4. Click the Upload button

Result: Deploy SpringBoot project in 1 second

image-20221213003608495

Guess you like

Origin blog.csdn.net/weixin_43811294/article/details/128810233