Use linux to deploy the Spring Boot program.

Source: Internet
Author: User

Use linux to deploy the Spring Boot program.

SpingBoot's learning has finally come to an end, but it leaves us with a question: how should programs be deployed on linux servers? Will the program be shut down if ssh is disconnected after the program is started using the command? To solve the first problem, you only need to ensure that jdk is correctly installed. The tricky problem is later. The key to solving the problem is to ensure that the program runs as a daemon (backend. This article will share how to correctly deploy programs in linux.

To solve this problem, I found the following three solutions based on my online documents:

1. Make the jar package into a system service (not recommended)

The disadvantage of this method is obvious, that is, the service cannot be started easily, because different linux Release versions are more or less different.

2. Use the nohup command with kill-9 xxx

This method is easy to accept and a compromise, but it is a little troublesome to stop the program by using commands to find the process and then using kill-9.

3. Use screen, tmux, and other methods (recommended)

This method is more scientific because it provides a unified interface for managing multiple sessions and corresponding functions, and is easier to operate. The disadvantage is that it requires a little learning cost.

Common tmux commands:

$ Tmux new-s session-name create session
$ Tmux a-t session-name: access a specified session
$ Tmux detach/$ Ctrl-B d disconnect the current session
$ Tmux kill-session-t session-name close session

$ Tmux ls show all sessions

Common screen commands:

$ Screen-S window-name creates a session
$ Screen-r session number or name: access a specified session/reconnect session
$ Screen-ls show all sessions
Ctrl + a B detach the current session
Ctrl + a k

The following are their differences. Although tmux is advanced, it depends on your usage habits.

Tmux is similar to screen, but better than screen. The simple answer is that although the screen function is the same, tmux is better designed. Although screen is available, it is unstable.

Below are some things where tmux surpasses screen:

  • The screen project has been largely terminated, and there are a lot of problems in the code
  • Tmux is an active project and its code library is updated frequently.
  • Tmux uses a real client/server model, while screen only simulates the behavior of this model.
  • Tmux supports both emacs and vim shortcut keys.
  • Tmux supports automatic window rename
  • Tmux can be highly scripted
  • More advanced window separation functions of tmux

SpringBoot is easier to package with maven. Check the configuration of pom. xml during packaging to ensure the following configuration is available:

<Build> <plugins> <plugin> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> <configuration> <! -- Fork: If this option is not configured, devtools does not work, that is, the application does not restart --> <fork> true </fork> <! -- Optional configuration --> <executable> true </executable> </configuration> </plugin> </plugins> lt;/build>

You can use maven install for packaging. If the package is successful, find the corresponding jar, rename it, and upload it to the linux server for authorization.

In this way, the application starts. Even if the command line is closed or the application is disconnected from ssh, the normal operation of the program will not be affected.

Disconnect

After the session ends, the program is shut down, as shown below:

The use of screen is similar to that of tmux. After practice, you can deploy a program to run it in the background. screen and tmux are both effective.

PS: The following describes how to deploy spring-boot in linux.

First, clean maven in the project, then compress maven install into a jar package, and then delete the previous version of the Process in linux.

View the java Process command ps-ef | grep java

Kill-9 process no.

Run the spring-boot project nohup java-jar sinocube. jar and use nohup to end

Nohup outputs the log file back. The file name can be customized. The default value is nohup. nohup. out.

Cat nohup. out view the log nohup. out is the file name
Tailf nohup. out view logs in real time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.