Article Directory
Preface
In the actual development and online operation of the project, it is necessary to monitor the data and information of the project in real time.
Spring Boot Actuator can help you monitor and manage Spring Boot applications, such as health checks, audits, statistics, and HTTP tracking. All these features can be obtained through JMX or HTTP endpoints.
In " Spring Boot Actuator: Health Check, Audit, Statistics and Monitoring "
, springboot-actuator provides monitoring endpoints for many systems, as shown below:
Function name | description | Is it sensitive |
---|---|---|
autoconfig | Display an auto-configuration report, the report shows all auto-configuration candidates and the reason why they are applied or not applied | true |
beans | Display a complete list of all Spring Beans in an application | true |
configprops | Display a collated list of all @ConfigurationProperties | true |
dump | Perform a thread dump | true |
env | Expose properties from SpringConfigurableEnvironment | true |
health | Display the health information of the application (a simple'status' is displayed when using an unauthenticated connection to access, and all information details are displayed when using an authenticated connection to access) | false |
info | Display arbitrary application information | false |
metrics | Display the current application's'indicator' information | true |
mappings | Display a collated list of all @RequestMapping paths | true |
shutdown | Allow apps to close in a graceful way (not enabled by default) | true |
trace | Display trace information (default is some of the latest HTTP requests) | true |
Version selection
Springboot version: 2.1.14. RELEASE
Admin client version: 2.1.5
Admin server version: 2.1.5
Simple use of actuator
Create a project Springboot-Actuator-client
and introduce dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Add configuration file
server:
port: 80
management:
server:
port: 8889 ##管理端口调整为 8889,独立的端口可以作安全控制
endpoints:
web:
base-path: /xiangjiao ## actuator的访问路径(修改访问路径,2.0之前默认是/,2.0默认是/actuator)
exposure:
include: '*' #开放所有页面节点 默认只开启了health、info两个节点
#exclude: 'env' #公开中的所有端点,除去 env
endpoint:
health:
show-details: always
Start the service, test and observe
http://localhost:
8889/xiangjiao
/env
http://localhost:
8889/xiangjiao
In addition to shutdown
commands, all other commands can be tested!
Use shutdown to gracefully close the project
Since the shutdown
node is closed by default, if you need to use the node to operate, you need to open it in the configuration file.
management:
server:
port: 8889 ##管理端口调整为 8889,独立的端口可以作安全控制
endpoint:
shutdown:
enabled: true ## 默认关闭,如果需要使用shutdown指令,则需要配置开启
Here is another detail:
This request must be used
POST
!
Request a test:
View the project console output:
Combine with Admin to realize graphical display
From the above test return results, it is obvious:
1. The data returns json type
2. All are data information, not intuitive enough!
How can the information obtained by the monitoring project be displayed more intuitively? At this time, you can take the admin
display operation.
For Admin operation, an additional server
service main class needs to be configured, under which multiple client
classes can be configured .
Create Admin-server class
The admin server project user accepts the monitoring information pushed by each admin client.
Dependency introduction:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.5</version>
</dependency>
The configuration file only specifies the port information occupied by the project startup:
server:
port: 8000
#spring:
# boot:
# admin:
# ui:
# title: 监控测试server
# brand: <span>Service Monitoring Center</span>
The startup class annotation is admin server
:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
@SpringBootApplication
@EnableAdminServer //注解表示开启admin-server
public class ActuatorAdminServer {
public static void main(String[] args) {
try {
SpringApplication.run(ActuatorAdminServer.class, args);
} catch (Exception e) {
System.out.println(e);
}
}
}
Project begining.
Modify the Springboot-Actuator-client project
Because the server part is a display window for all monitoring information, the data in it needs to be used to admin client
realize the push operation of the data; it actuator
is only used to read the real-time monitoring information when the project is running, and does not have the push function.
So you need Springboot-Actuator-client
to add a dependency in it spring-boot-admin-starter-client
.
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.5</version>
</dependency>
After adding dependencies, you also need to configure the monitoring log data push address:
server:
port: 80
spring:
boot:
admin:
client:
url: http://localhost:8000 ## 监控信息推送admin server
management:
server:
port: 8889 ##管理端口调整为 8889,独立的端口可以作安全控制
endpoints:
web:
base-path: /xiangjiao ## actuator的访问路径(修改访问路径,2.0之前默认是/,2.0默认是/actuator)
exposure:
include: '*' #开放所有页面节点 默认只开启了health、info两个节点
#exclude: 'env' #公开中的所有端点,除去 env
endpoint:
health:
show-details: always
Compared with its original actuator
configuration, only the push address configuration has been added:
spring:
boot:
admin:
client:
url: http://localhost:8000 ## 监控信息推送admin server
Startup project.
UI interface display effect
Visit link:
http://localhost:8000