The SpringBoot project is deployed to Alibaba Cloud ESC and runs in the background

ready

  1. SpringBoot project
    Insert picture description here

Among them, the use of Maven project management tools

  1. Linux

My Linux distribution is CentOS 7, which is an Alibaba Cloud server
student "online practice" plan . Students can receive a 6-month server for free-in fact, students are not expensive to buy a server, it is recommended to get a play

  1. Remote connection to Linux tools
    Xftp, Xshell is very easy to use

Linux operation (1) Basic operation-remote login and user management


Package the project

We know that SpringBoot has built-in Tomcat, just need to package it into a jar file and run it on Linux

If you are using Maven to manage the project, you can directly
package it through Maven, click on the right side Maven Projects , double-click the package will automatically package
(Windows can also use the cmd command to package under the Maven folder)
Insert picture description here

This problem may occur:

Running com.myblog.blog.BlogApplicationTests
Tests run: 3, Failures: 0, Errors: 3, Skipped: 0, Time elapsed: 0.002 sec <<< FAILURE! - in com.myblog.blog.BlogApplicationTests
initializationError(com.myblog.blog.BlogApplicationTests)  Time elapsed: 0.001 sec  <<< ERROR!
java.lang.Exception: The class com.myblog.blog.BlogApplicationTests is not public.

Insert picture description here

Cause packaging failure

Insert picture description here

This is the maven-surefire-plugin component missing in Maven

Add under build-> plugins tag

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

This is the successful packaging
Insert picture description here

Go to the target folder under your project to find the jar file, such as my address isD:\JavaProject\blog\SpringBootBlog\target\blog-0.0.1-SNAPSHOT.jar

Insert picture description here

Transfer this jar package to Linux


Linux running

Put the jar package in the directory you want to run

I put it under / opt / blog
Insert picture description here

You need to install jdk under Linux (need to run jar project)

Note: Whether your database is put into Linux,
after all, SpringBoot will verify the database connection during the running phase

Note: Alibaba Cloud needs to set up a security group.
There is a security group setting on the left side of the console
Insert picture description here

Click to configure rules

Insert picture description here
Insert picture description here
Click to quickly create a rule , you need to change three places:
8080/8080 security group port number (3306 is the port number of the Mysql server)
1 Priority: the lower the priority, the higher the
0.0.0.0/0 authorized object: allow all IP access
Insert picture description here

ok, the priority is set

Run the jar project:java -jar 项目名

Insert picture description here

The springboot icon comes out (I changed the springboot icon to NO BUG)

The operation may report errors:

o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.

Port 8080 is occupied

First check the port number:netstat -tunlp |grep 8080

There is a process occupying my port 8080

Insert picture description here

Can kill this process: kill -9 2814
-9 is forced to kill this process, 2814 is the process number that occupies my port

8080 is free after killing
Insert picture description here

Run
Insert picture description here
ok again , the operation is successful, access the project through the public IP (if it is not accessible, it may be a firewall problem iptables, firewall)

Insert picture description here
Because it is a command line interface, our interface is the SpringBoot console
Insert picture description here

ctrl + c to exit, return
Insert picture description here

But the problem is that when we quit, SpringBoot quits,
Insert picture description here
and the effect we want is that the cloud server continues to run the server in the background


Continuously running in the background

Can write a shell step to complete

Create one in the current directorystartup.sh
Insert picture description here

vim startup.sh

The command is two lines. The first line is not a comment when it runs. It means that this script uses / bin / sh to explain the execution. The interpreter explains
the address of the second line to write its own jar file. -0.0.1-SNAPSHOT.jar

#!/bin/sh
nohup java -jar /opt/blog/blog-0.0.1-SNAPSHOT.jar &                                              

After the script is written, there is no running permission at the beginning, and there are setting permissions chmod +x startup.shto give executable permissions

Running shell script./startup.sh

Run in the background, the console information will be placed in nohup.out
Insert picture description here

You can also use the project in SpringBoot by closing Xshell


Conclusion

The project will eventually be deployed to Linux.
Also, welcome to visit my personal blog

Published 121 original articles · won 31 · views 7869

Guess you like

Origin blog.csdn.net/key_768/article/details/105413813