赞
踩
本文介绍如何集成actuator,以及集成针对actuator接口数据提供UI美化封装的监控工具spring-boot-admin
Maven仓库: spring-boot-admin-starter-server
Maven仓库: spring-boot-starter-parent
使用的依赖版本:
spring-boot-starter-parent: 2.3.8.RELEASE,
spring-boot-admin-starter-server: 2.3.1
本文源码github地址:https://github.com/xhga/spring-boot-actuator-xhga
创建springboot项目
略,我配置的端口是9998
引入actuator依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
引入依赖之后启动项目,并访问:http://localhost:9998/actuator
因为actuator默认仅开启health,info端点,所以将看到下面的返回信息.
{ "_links": { "self": { "href": "http://localhost:9998/actuator", "templated": false }, "health": { "href": "http://localhost:9998/actuator/health", "templated": false }, "health-path": { "href": "http://localhost:9998/actuator/health/{*path}", "templated": true }, "info": { "href": "http://localhost:9998/actuator/info", "templated": false } } }
配置监控端点
在第二步的基础上,配置更多的监控端点,在yml配置中添加监控所有端点的配置项:
management:
endpoints:
web:
exposure:
include: "*"
再次访问:http://localhost:9998/actuator
返回信息:
{ "_links": { "self": { "href": "http://localhost:9998/actuator", "templated": false }, "beans": { "href": "http://localhost:9998/actuator/beans", "templated": false }, "caches-cache": { "href": "http://localhost:9998/actuator/caches/{cache}", "templated": true }, "caches": { "href": "http://localhost:9998/actuator/caches", "templated": false }, "health": { "href": "http://localhost:9998/actuator/health", "templated": false }, "health-path": { "href": "http://localhost:9998/actuator/health/{*path}", "templated": true }, "info": { "href": "http://localhost:9998/actuator/info", "templated": false }, "conditions": { "href": "http://localhost:9998/actuator/conditions", "templated": false }, "configprops": { "href": "http://localhost:9998/actuator/configprops", "templated": false }, "env": { "href": "http://localhost:9998/actuator/env", "templated": false }, "env-toMatch": { "href": "http://localhost:9998/actuator/env/{toMatch}", "templated": true }, "loggers": { "href": "http://localhost:9998/actuator/loggers", "templated": false }, "loggers-name": { "href": "http://localhost:9998/actuator/loggers/{name}", "templated": true }, "heapdump": { "href": "http://localhost:9998/actuator/heapdump", "templated": false }, "threaddump": { "href": "http://localhost:9998/actuator/threaddump", "templated": false }, "metrics-requiredMetricName": { "href": "http://localhost:9998/actuator/metrics/{requiredMetricName}", "templated": true }, "metrics": { "href": "http://localhost:9998/actuator/metrics", "templated": false }, "scheduledtasks": { "href": "http://localhost:9998/actuator/scheduledtasks", "templated": false }, "mappings": { "href": "http://localhost:9998/actuator/mappings", "templated": false } }}
获取系统监控信息
各个监控端点的访问方式已返回在对应端点的href中;
举例:获取系统cpu使用率信息
从第3步的返回信息中找到:
"metrics-requiredMetricName": {
"href": "http://localhost:9998/actuator/metrics/{requiredMetricName}",
"templated": true
},
"metrics": {
"href": "http://localhost:9998/actuator/metrics",
"templated": false
},
访问http://localhost:9998/actuator/metrics:
{ "names": [ "http.server.requests", "jvm.buffer.count", "jvm.buffer.memory.used", "jvm.buffer.total.capacity", "jvm.classes.loaded", "jvm.classes.unloaded", "jvm.gc.live.data.size", "jvm.gc.max.data.size", "jvm.gc.memory.allocated", "jvm.gc.memory.promoted", "jvm.gc.pause", "jvm.memory.committed", "jvm.memory.max", "jvm.memory.used", "jvm.threads.daemon", "jvm.threads.live", "jvm.threads.peak", "jvm.threads.states", "logback.events", "process.cpu.usage", "process.start.time", "process.uptime", "system.cpu.count", "system.cpu.usage", "tomcat.sessions.active.current", "tomcat.sessions.active.max", "tomcat.sessions.alive.max", "tomcat.sessions.created", "tomcat.sessions.expired", "tomcat.sessions.rejected" ] }
names中system.cpu.usage表示系统cpu的使用率,
因此将system.cpu.usage代入metrics-requiredMetricName的href中,
得到获取cpu使用率的url并访问:
http://localhost:9998/actuator/metrics/system.cpu.usage
返回信息:
{
"name": "system.cpu.usage",
"description": "The \"recent cpu usage\" for the whole system",
"baseUnit": null,
"measurements": [
{
"statistic": "VALUE",
"value": 0.13861874171047217
}
],
"availableTags": []
}
cpu使用率为: 0.13861874171047217
说明:上述请求url均为get请求
介绍:
Spring Boot Admin 是一个管理和监控Spring Boot 应用程序的开源软件。每个应用都认为是一个客户端,通过HTTP或者使用 Eureka注册到admin server中进行展示,Spring Boot Admin UI部分使用AngularJs将数据展示在前端。
springbootadmin需要客户端及服务端两个项目。
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.1</version>
</dependency>
@SpringBootApplication
@EnableAdminServer
public class ActuatorServerApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorServerApplication.class, args);
}
}
在第一步(一. 新建springboot项目,引入actuator)的项目中集成spring-boot-admin-client
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.1</version>
</dependency>
spring:
boot:
admin:
client:
enabled: true
url: http://localhost:9999
import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringBootAdminConfig {
@Bean
public InMemoryHttpTraceRepository getInMemoryHttpTrace(){
return new InMemoryHttpTraceRepository();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。