Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

WangScaler: A writer with heart.

Declaration: uneducated, if there is a mistake, kindly correct.

The Actuator is not available on other platforms, but is one of the four cores of SpringBoot. Once introduced, some information about SpringBoot can be monitored with a simple configuration.

Introduction of depend on

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
Copy the code

After introducing dependencies, we can monitor our application.

Simple configuration

For the sake of our demonstration, we have all endpoints turned on and exposed on the Web side. If this configuration is unavailable in a formal environment, you can use Spring Security for authentication access.

Management: endpoints: # Expose all endpoints enable-by-default: true Web: exposure: # Expose all endpoints in web mode include: '*' endpoint: health: show-details: alwaysCopy the code

Check the information

Once configured, you can view the information for these endpoints directly.

Add /actuator after the URL to access it.

As you can see, one of the parameters is shutdown, which as the name implies is the interface to remotely close the program. Now let’s see if we can close the program remotely.

Remote closing routine

Let’s just click on the URL and send the request.

You can see that this is a Post request, not directly accessible in the browser, so let’s try using Postman.

Based on the response information, the application was indeed shut down. Let’s see if our application is really down.

That’s right, this is an interface to remotely close applications, isn’t it powerful? This if arbitrarily open out, used by users that is not GG, so remind you again, these ports can not arbitrarily open.

Protection of endpoints

These sensitive endpoints need to be open, which means adding Security policies that can be combined with Spring Security to protect these endpoints.

The following is an example:

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
​
@Configuration(proxyBeanMethods = false)
public class MySecurityConfiguration {
​
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.requestMatcher(EndpointRequest.toAnyEndpoint())
                .authorizeRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
        http.httpBasic();
        returnhttp.build(); }}Copy the code

Using this configuration, all endpoints of the actuator need the ENDPOINT_ADMIN role to access them.

The last

The monitoring endpoints of the robot are many and can be used in various ways. If you are interested, you can visit the official website to learn how to use them correctly. It can not only monitor the information of the memory, but also monitor the state of the database integrated by the service. It is a very good tool to monitor the performance of the service.

Come all come, click “like” and then go!

Follow WangScaler and wish you a promotion, a raise and no bucket!