Spring Cloud runs as a jar package on Windows or Linux

Source: Internet
Author: User

sort out some of the ways that spring boot background runs. Before we introduce the background run configuration, let's review some of the ways in which the Spring boot application works

1: Run the Spring boot application main class
2: Run with MAVEN's spring boot plugin mvn spring-boot:run
3: Run with Java-jar after hitting into jar package


When we develop, we usually use the first two, but we often use a third when we deploy. However, when we use Java-jar to run, it is not running in the background. Let's take a look at the windows and Linux/unix two environments and how to configure how the background runs.

Windows

Windows is simple, we can directly use this software: Alwaysup. As shown, simple, violent, and useful.

The configuration is simple, we just need to mvn install the Spring boot app into a jar package, and then write a Java-jar yourapp.jar bat file. Then open Alwaysup, click on the first button of the toolbar, as shown, select the Bat file written above and fill in the service name.

After the creation, you can see our configured services in the list, and by right-clicking on start XXX You can start the app in the background.

Linux/unix
Let's talk about how to configure it on the server. In fact, there are many ways to implement it, and here are two more useful ways to do it:
Nohup and Shell

This method is mainly implemented by using the Nohup command, which is described in detail below:
Nohup command
Purpose: To run the command without hanging off.

                                        语法:nohup Command [ Arg … ][ & ]                                        描述:nohup 命令运行由 Command 参数和任何相关的 Arg 参数指定的命令,忽略所有挂断(SIGHUP)信号。在注销后使用 nohup 命令运行后台中的程序。要运行后台中的 nohup 命令,添加 &到命令的尾部。

So, we just need to use the Nohup java-jar Yourapp.jar & command to make Yourapp.jar run in the background.
However, to facilitate management, we can also write some scripts to launch the application through the shell,
Here are some examples:
To close the app's script: stop.sh
{
#!/bin/bash
pid=$ (ps-ef | grep yourapp.jar | grep-v grep | awk ' {print $} ')
If [-Z "$PID"]
Then
echo application is already stopped
Else
Echo Kill $PID
Kill $PID
Fi
}
Script to launch the app: start.sh
{
#!/bin/bash
Nohup Java-jar Yourapp.jar--server.port=8888 &
}

Integration of the shutdown and startup scripts: Run.sh, because the application will be closed first, and then start the application, which will not cause port conflicts and other problems, suitable for repeated calls in the continuous integration System
{
#!/bin/bash
echo Stop Application
SOURCE stop.sh
echo Start Application
SOURCE start.sh
}

System Services

In the spring boot maven plug-in, it also provides the ability to build a complete executable program, what does it mean? That is, instead of Java-jar, we can run the jar directly to execute the program. This allows us to easily create a system service that runs in the background. The main steps are as follows:
Add the Spring boot plug-in to Pom.xml and note that the executable configuration is set

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
After completing the above configuration, use MVN install to package, build an executable jar package

Create a soft connection to the/etc/init.d/directory
sudo ln-s/var/yourapp/yourapp.jar/etc/init.d/yourapp
After the soft connection is created, we can control the start, stop, restart operation with the following command for the Yourapp.jar application

/etc/init.d/yourapp Start|stop|restart

Spring Cloud runs as a jar package on Windows or Linux

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.