Detailed explanation of SpringBoot application service startup and safe termination

  • 2021-07-22 09:49:57
  • OfStack

SpringBoot application service startup

A simple SpringBoot application can be quickly built with reference to the official example project. The official connection is as follows: http://projects.spring.io/spring-boot/# quick-start
Less gossip, code:


package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

  @RequestMapping("/")
  @ResponseBody
  String home() {
    return "Hello World!";
  }

  public static void main(String[] args) throws Exception {
    SpringApplication.run(SampleController.class, args);
  }
}

The main () function is used as an entry to start the SpringBoot service. Although there are only a few lines of code here, it was already a complete web program at that time.

There are several annotations that need special explanation. 1. I suffered a lot from these annotations when I was developing.

@ EnableAutoConfiguration: This annotation tells Spring Boot to guess how you want to configure Spring based on the added jar dependency. Since spring-boot-starter-web adds Tomcat and Spring MVC, auto-configuration will assume that you are developing an web application and setup Spring accordingly.

@ ComponentScan: Annotation searches for beans, combined with @ Autowired constructor injection.

@ SpringBootApplication: Equivalent to using @ Configuration, @ EnableAutoConfiguration, and @ ComponentScan with the default properties, but this annotation only searches for siblings and subordinates of this package! ! ! ! ! !

SpringBoot Application Security Termination

Because SpringBoot integrates tomcat, when SpringBoot application is started, SpringBoot cannot be operated like tomcat operation 1, but SpringBoot encapsulates the methods of starting and stopping.

SpringBoot, as the product of the best practice of Spring framework for the concept of "convention takes precedence over configuration (Convention Over Configuration)", can help us quickly create independent and product-level applications based on Spring framework. Most Spring Boot applications can run quickly with very little configuration, which is a micro-framework that is quite compatible with micro-services (MicroServices).

There are two main ways: sending shutdown signal through HTTP, or sending service stop. This article only introduces HTTP.

1. Introducing actuator dependency in pom. xml


<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. Open shutdown endpoint in application. shutdown. enabled of SpringBoot is turned off by default.


# Enable shutdown
endpoints.shutdown.enabled=true
# Disable password authentication 
endpoints.shutdown.sensitive=false

3. Send a stop signal to send an post request to the server using curl:

curl-X POST host: port/shutdown

You will get the following return message:


{"message":"Shutting down, bye..."}

4. It can be seen that this method is very convenient, but it is also very insecure. When it is officially used, the necessary security settings must be made for this request, and the identity authentication can be carried out with the help of spring-boot-starter-security:
pom. xml Add security dependency


<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-security</artifactId>
</dependency>

Configuration changes in application. properties

# Turn on security authentication for shutdown
endpoints.shutdown.sensitive=true
# Verify the user name
security.user.name=admin
# Verify password
security.user.password=secret
# Roles
management.security.role=SUPERUSER

Specify path, IP, port

# Specifies the path to shutdown endpoint
endpoints.shutdown.path=/custompath
# You can also specify paths for all endpoints ` management. context-path=/manage `
# Specify the administrative port and IP
management.port=8081
management.address=127.0.0.1


Related articles: