Stop Springboot application services correctly and safely

Source: Internet
Author: User
Tags spring boot actuator

Introduction

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).
The QuickStart Chinese content on spring boot on the web has been quite rich, but it is still relatively scarce for how to stop service (shutdown) easily and safely after deployment, and recently found that the official guide to spring Boot has updated the relevant content, so in conjunction with this part of the update For a brief description of how to properly stop the spring boot application based on the officially provided features .

There are two main ways: by HTTP sending a shutdown signal, or by service stop the way

Way one: Through HTTPSend shutdownSignal

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

1. In pom.xmlIntroduced in actuatorDepend on
<dependency>  <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
2. Open shutdown 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#禁用密码验证endpoints.shutdown.sensitive=false
3. Send shutdownSignal

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:

  1. Pom.xml Add security dependency

    < Span class= "Hljs-tag" ><dependency> << Span class= "Hljs-name" >groupid>org.springframework.boot</groupid> <artifactId >spring-boot-starter-security</ Artifactid></DEPENDENCY>   
  2. Turn on security verification
    application.propertieschange the configuration in, and

    #开启shutdown的安全验证endpoints.shutdown.sensitive=true#验证用户名security.user.name=admin#验证密码security.user.password=secret#角色management.security.role=SUPERUSER
  3. Specify path, IP, port

    #指定shutdown endpoint的路径endpoints.shutdown.path=/custompath#也可以统一指定所有endpoints的路径`management.context-path=/manage`#指定管理端口和IPmanagement.port=8081management.address=127.0.0.1
Mode two: 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 pom.xmlPlug-in introduced in:

xml <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin>

2. 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

3. Give executable permissions:

chmod u+x app.jar

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.
sudo service app 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.

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 related variables are configured, such as:

      JAVA_HOME=/usr/local/jdkJAVA_OPTS=-Xmx1024MLOG_FOLDER=/custom/log
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 things to. conf files: chmod 400 app.conf ,sudo chown root:root app.conf
References:
      1. Installing Spring Boot Applications
      2. Endpoints
      3. Securing sensitive Endpoints

Http://www.cnblogs.com/lobo/p/5657684.html

Stop Springboot application services correctly and safely

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.