Spring boot app start and stop (Spring boot app starts with the start command)

Source: Internet
Author: User
Tags spring boot actuator

Spring Boot, as a product of the spring Framework's best practices for the "Contract over Configuration" concept, allows us to quickly create convention, product-level, spring-based applications Most spring boot applications require very little configuration to run quickly and are a micro-framework that is quite aligned with microservices (microservices).

There are two main ways to close a spring boot: by HTTP sending a shutdown signal, or by service stop the way.

One, HTTP close the shutdown application by sending signal

The main characteristics of this approach depend on Spring Boot Actuator the endpoint following steps:

1, in the pom.xml introduction of actuator dependency

< Dependency >  < groupId >org.springframework.boot</groupId>  < Artifactid >spring-boot-starter-actuator</artifactid></  dependency>

2. Openshutdown endpoint

Spring Boot ActuatorThe shutdown endpoin T is off by default, so it is application.properties turned on in shutdown endpoint :

#启用shutdownendpoints. shutdown.enabled=true# Disabling password Authentication endpoints.shutdown.sensitive=false

Specify path, IP, port

#指定shutdown endpoint Path endpoints.shutdown.path=/custompath# can also uniformly specify the path of all endpoints ' management.context-path=/ Manage ' #指定管理端口和IPmanagement. port=8081management.address=127.0.0.1

3. Send shutdown Signal

shutdownBy default url host:port/shutdown , when a service needs to be stopped, the request is made to post the server, such as:

Curl-x POST Host:port/shutdown

Will get the shape {"message":"Shutting down, bye..."} of the response

4. Security Settings

As you can see, using this method can be very convenient for remote operation, but it is important to note that when used formally, the request must be made with the necessary security settings, such as with the help of spring-boot-starter-security identity authentication:

Pom.xml Adding security dependencies

< Dependency >  < groupId >org.springframework.boot</groupId>  <  Artifactid>spring-boot-starter-security</artifactid>  </dependency>

Turn on security verification
application.propertiesChange the configuration in

#开启shutdown的安全验证endpoints. shutdown.sensitive=true# Verify user name security.user.name=admin# authentication password security.user.password= secret# role Management.security.role=superuser

Note: If the security framework is introduced, all requests will be required to enter the account password in order to access it, as configured above.

Second, deploy as Unix/linux Service

This approach is primarily based on the official creation of the spring-boot-maven-plugin "Fully executable" jar, which contains a shell script that makes it easy to set the app as a Unix/linux system service (INIT.D service). The feature is officially tested on CentOS and Ubuntu, and may need to be customized for OS X and FreeBSD. The steps are as follows:

1, in the pom.xml introduction of plug-ins:

<plugin>    <groupId>Org.springframework.boot</groupId>    <Artifactid>Spring-boot-maven-plugin</Artifactid>    <Configuration>        <executable>true</executable>    </Configuration></plugin>

Note: The red section means that it can be executed.

2. Give executable permission:

chmod u+x App.jar

Note: After this step basically can be run at the command line, first package out the jar package, and then start, such as./app.jar start.

3. Set As System service

Make your app into a jar package, deploy it to a server, assuming the deployment path is/var/app and the package name is App.jar, you should set it as a system service as follows:

sudo ln-s/var/app/app.jar/etc/init.d/app

4. Managing in a system service manner

Next, you can use our familiar service Foo Start|stop|restart to manage the start and stop of your application.

Start|stop

command will get the shape Started|Stopped [PID] of the result feedback

Default PID file path:

/var/Run/appname/appname.pid

Default log file path:

/var/log/appname.log

This is probably a more familiar and more common way of managing.

Tip: The above log and the file that holds the PID are different depending on where the system might appear.

5. Custom Parameters

In this way, we can also use the custom. conf file to change the default configuration, as follows:

1) create a. conf file under the same path as the jar package, and the name should be the same as the name of the. jar, such as appname.conf

2) in which the relevant variables are configured, such as:

java_home=/usr/local/jdk java_opts=-xmx1024m log_folder=/custom/log

6. Security Settings

As an application service, security is a problem that cannot be ignored, and some of the following actions can be referenced as part of the underlying settings:

    • Create a separate user for the service, and preferably bind the user's shell to/usr/sbin/nologin
    • Give minimum scope permissions:chmod 500 app.jar
    • To prevent modification:sudo chattr +i app.jar
    • Do similar work to the. conf file:chmod 400 app.conf,sudo chown root:root app.conf

Reference:

Http://www.cnblogs.com/lobo/p/5657684.html (the above content is transferred from this article)

Installing Spring Boot Applications

Endpoints

Securing sensitive Endpoints

Spring boot app start and stop (Spring boot app starts with the start command)

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.