preface
Sometimes we want to monitor the health of our application in real time, such as displaying metrics in real time, observing traffic from moment to moment, or the access status of our database, etc. That’s when the Actuator needs to operate.
The advantage of using the Actuator is that we can use the production-grade tools directly without having to implement these things ourselves. The robot can automatically expose the information for us, using HTTP or JMX Beans. The most important thing is that we can configure it directly in the Properties file.
Here’s how to do it:
Code implementation
To create a SpringBoot project, I used a version of Springboot2.4.
Step 1: Add dependencies
<! -- Monitor dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Copy the code
Dependence is that simple.
Step 2: Configure
Port =8081
# 1, the default only opens the info and physical health of two endpoints # the following configuration can open all the endpoint: management endpoints. Web. Exposure. Include = *
# 2, open the health monitoring data management. The endpoint. Health. The show - the details = always
# 3, enable httptrace endpoint management. The endpoint. Httptrace. Enabled = true
Copy the code
# 4, every time want to add a physical prefix too troublesome, change the endpoint prefix path management endpoints. Web. The base - path = /
This adds several configurations.
Now we can just access it directly.
Step 3: Direct access
Since we configured our own base-path earlier. So there is no need to add the prefix of the actuator. Visit now:
http://localhost:8081/mappings
It’s a little ugly to look at, but with this information, you can format it, and there are a lot of open source projects that do that automatically.
Mappings (); / / Add mappings (); / / Add mappings ();
EndPoints | describe |
---|---|
auditevents | Exposes audit event information for the current application. |
beans | Displays a complete list of all Spring beans in the application. |
caches | Expose available caches. |
conditions | Displays the conditions evaluated on the configuration and auto-configuration classes and why they match or don’t match. |
configprops | Display all the collation lists @ConfigurationProperties and view the ConfigurationProperties, including the default configuration |
env | Various environment variables that expose Spring properties, followed by /{name} to see the specific values |
flyway | Displays any Flyway database migrations that have been applied. |
health | Display application health information. After 2.0, you need to turn on the k switch in the configuration show-details |
httptrace | Display HTTP trace information. After 2.0, you need to manually open it |
info | The display of arbitrary application information is self-defined in the configuration file |
integrationgraph | Displays the Spring Integration diagram. |
loggers | Displays and modifies logger configuration in the application. |
liquibase | Displays any Liquibase database migrations that have been applied. |
metrics | Displays metrics, such as memory usage and HTTP request counts, followed by /{name} |
mappings | Displays a collated list of all @requestMapping paths. |
scheduledtasks | Displays scheduled tasks in the application. |
sessions | Allows user sessions to be retrieved and deleted from the Session store supported by Spring Session. |
shutdown | Allow the application to close normally. |
threaddump | Perform a thread dump. |
OK. That’s basically it.
Add security authentication for the Actuator
For example, the ports of our Actuator do not want to be seen by irrelevant personnel, so you can configure Spring Security to add login. The method is as follows:
Step 1: Add dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Copy the code
Step 2: Add the configuration to the Properties file
#5. What if the port information is seen by others? Can add a security spring. Security. The user. The name = FDD spring. Security. The user. The password = 123456 spring. Security. User. Roles = ADMINCopy the code
The user face is FDD, the password is 123456, and the role is ADMIN.
Step 3: Revisit the port address above
Revisiting the relevant port address redirects you to the login page. Use the configured user name and password to log in.
OK, this is easy.