“This is the second day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.
Spring Boot Actuators help monitor and manage Spring Boot applications, such as health checks, auditing, statistics, and HTTP tracking. All of these features are available via JMX or HTTP endPoints.
1. Monitoring apps
β To enable the endpoints of the Actuator, you only need to include the dependencies of the Actuator in the project:
<! -- SpringBoot Monitoring: SpringBoot Actuator -->
<! -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Copy the code
Let’s run the application and try to access HTTP endpoints exposed by default:
/actuator
/actuator/health
/actuator/health/{component}
/actuator/health/{component}/{instance}
/actuator/info
Copy the code
The application uses port 8080 by default. Once the application starts, it can use localhost:8080/ to access the default EXPOSED HTTP endpoints:
Now open http://localhost:8080/actuator/health will display the following:
{"status":"UP"}
Copy the code
2. Actuator Endpoints
The key feature of Spring Boot Actuator is that it provides multiple interfaces (Endpoints) in the application to understand the internal conditions of the operating time.
Actuator Endpoints allow users to monitor and interact with applications. Spring Boot includes a number of built-in endpoints and allows you to customize your own. For example, the Health endpoint provides basic application health information.
2.1 Endpoint List
ID | describe |
---|---|
auditevents |
Expose audit event information for the current application. The need for aAuditEventRepository components γ |
beans |
Displays a complete list of all Spring beans in the application. |
caches |
Expose available caches. |
conditions |
Displays all condition information for automatic configuration, including the reason for the match or mismatch. |
configprops |
Show all@ConfigurationProperties . |
env |
Expose Spring’s propertiesConfigurableEnvironment |
flyway |
Displays all Flyway database migrations that have been applied. You need one or more Flyway Components. |
health |
Displays application health information. |
httptrace |
Displays HTTP trace information (by default, the last 100 HTTP request-responses). The need for aHttpTraceRepository Components. |
info |
Displays arbitrary application information. |
integrationgraph |
According to the Springintegrationgraph Need to depend onspring-integration-core . |
loggers |
Displays and modifies the configuration of logging in the application. |
liquibase |
Displays all Liquibase database migrations that have been applied. You need one or moreLiquibase Components. |
metrics |
Displays metrics information about the current application. |
mappings |
Show all@RequestMapping Path list. |
scheduledtasks |
Displays scheduled tasks in the application. |
sessions |
Allows user sessions to be retrieved and deleted from the Session store supported by Spring Session. Servlet-based Web applications that use Spring Session are required. |
shutdown |
Enables the application to be shut down normally, which is disabled by default. |
startup |
According to theApplicationStartup Collected startup step data. You need to useSpringApplication configureBufferingApplicationStartup . |
threaddump |
Perform a thread dump. |
If the application is a Web application (Spring MVC, Spring WebFlux, or Jersey), the following additional endpoints can also be used:
ID | Description |
---|---|
heapdump |
Returns ahprof Heap dump files. HotSpot JVM is required. |
jolokia |
Expose JMX beans over HTTP (Jolokia needs to be introduced, not for WebFlux). Need to introduce dependenciesjolokia-core . |
logfile |
Returns the contents of the log file, if setlogging.file.name ζ logging.file.path Properties). Support using HTTPRange To retrieve the contents of a partial log file. |
prometheus |
Expose metrics in a format that the Prometheus server can crawl. Need to rely onmicrometer-registry-prometheus . |
2.2 Expose all Endpoints
By default, only a few endpoints are enabled on the Web. To expose all endpoints, you need to configure the following in application.yml:
endpoints:
web:
exposure:
include: The '*'
Copy the code
2.3 Enabling all Monitoring endpoints by default & Enabling/disabling the Actuator Endpoint
management:
endpoints:
enabled-by-default: false # Enable all monitoring endpoints by default :true
Copy the code
In this case, using the Web only /actuator:
The same is true for monitoring using JMX:
Shut down after open all monitoring the endpoint, we can choose respectively exposed endpoint (closed management, at the same time. The endpoints. Web. Exposure. Include = *) :
management:
endpoints:
enabled-by-default: false # Enable all monitoring endpoints by default :true
endpoint:
health:
enabled: true
loggers:
enabled: true
mappings:
enabled: true
metrics:
enabled: true
Copy the code
2.4 The Most common Endpoint
- Health: Monitors application Health
- Metrics: runtime Metrics
- Loggers: logs
Against 2.4.1 Health Endpoint
/ Skeletonoid/Health endpoint provides basic information about the health of applications. Generally, it is used in cloud platforms, which periodically check the health status of applications. Therefore, the health endpoint is required for the platform to return the health status of a series of application components.
Displays detailed health information
management:
endpoint:
health:
show-details: always Open full health check information
Copy the code
The Metrics 2.4.2 the Endpoint
/actuator/metrics/{name} endpoint displays several useful metrics, such as JVM memory usage, system CPU usage, open files, and more.
Access localhost: 8080 / physical/metrics can see monitoring index list as follows:
Want to get a certain measure of detailed information, you need to pass the name of the measure to the URL, such as access localhost: 8080 / physical/metrics/JVM memory. 2:
2.4.3 Loggers Endpoint
/actuator/loggers endpoint displays application logs and can change the log level while running.
For example, in order to obtain the details of the root logger, you can visit http://localhost:8080/actuator/loggers/root:
{
"configuredLevel":"INFO"."effectiveLevel":"INFO"
}
Copy the code
Change the log level at run time
Loggers endpoint also allows you to change the logging level of your application at run time.
For example, in order to change the root logger level to DEBUG, send a POST request to http://localhost:8080/actuator/loggers/root, add the following parameters:
{
"configuredLevel": "DEBUG"
}
Copy the code
This feature is very useful for troubleshooting online problems.
Also, you can reset the log level by passing null to configuredLevel.
2.4.4 Info the Endpoint
βinfo Endpoint displays basic application information. It uses meta-INF /build-info.properties to get build information and gits.properties to get git information. It can also display any other information as long as the environment property contains the Info key.
Go to http://localhost:8080/actuator/info:
2.4.5 the Mappings Endpoint
Visit http://localhost:8080/actuator/mappings return requests mapping information:
2.4.6 Configprops Endpoint
3. Spring Boot Admin
Spring Boot Admin is an open source community project that manages and monitors Spring Boot applications. It provides a visualized UI to display monitoring information of Endpoints in the project.
The Spring Boot Admin has two roles: the Spring Boot Admin Client and the Spring Boot Admin Server. The application registers with the Spring Boot Admin Server as a Spring Boot Admin Client!
3.1 the Admin Server
β Create the admin-server module
β‘ Import the dependent coordinate admin-starter-server
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! -- Server monitoring -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
</dependencies>
Copy the code
Configuring access ports:
# app name
spring.application.name=springboot-admin-server
The server and the client are both on the same computer. The server and the client are on the same computer. The server and client are on the same computer.
server.port=9000
Copy the code
β’ Enable the monitoring function @enableadminServer on the boot class
// Enable server monitoring
@EnableAdminServer
@SpringBootApplication
public class SpringbootAdminServerApplication {
public static void main(String[] args) { SpringApplication.run(SpringbootAdminServerApplication.class, args); }}Copy the code
3.2 the Admin – Client
β Create the admin-client module
β‘ Import coordinates depend on admin-starter-client
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! -- Client access -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
</dependencies>
Copy the code
3 configure related information, such as the server address
# app name
spring.application.name=springboot-admin-client
[root@localhost] [root@localhost] [root@localhost]
If admin-server is not on the host, change localhost to destination IP
spring.boot.admin.client.url=http://localhost:9000
Port =8080 # Port to access the project: default port 8080!
Expose all endPoints
management.endpoints.web.exposure.include=*
Enable complete health check information (not just UP/DOWN information)
management.endpoint.health.show-details=always
Copy the code
β£ Start the server and client services at the same time and access the Spring Boot Admin Server project port (http://localhost:9000)
An instance can be observed running:
We hope this article will help you π§ feel free to leave your thoughts in the comments π, we will discuss and share π₯