在项目添加SpringBoot2.x版本 actuator的时候尝试了好几次都没有成功,记录下看文档的结果
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
或者
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
到这里启动项目就可以看到/actuator/health和/actuator/info的接口,这两个endpoint是springboot2.x版本默认开启的,但是其他的endpoint如果要开启需要在额外设置
{
_links:- {
self:- {
href: "http://localhost:8312/actuator",
templated: false
},
health:- {
href: "http://localhost:8312/actuator/health",
templated: false
},
health-component:- {
href: "http://localhost:8312/actuator/health/{component}",
templated: true
},
health-component-instance:- {
href: "http://localhost:8312/actuator/health/{component}/{instance}",
templated: true
},
info:- {
href: "http://localhost:8312/actuator/info",
templated: false
}
}
}
endpoint默认支持
endpoint | JMX | WEB |
---|---|---|
auditevents | Yes | No |
beans | Yes | No |
caches | Yes | No |
conditions | Yes | No |
configprops | Yes | No |
env | Yes | No |
flyway | Yes | No |
health | Yes | Yes |
heapdump | N/A | No |
httptrace | Yes | No| |
info | Yes | Yes |
integrationgraph | Yes | No |
jolokia | N/A | No |
logfile | N/A | No |
loggers | Yes | No |
liquibase | Yes | No |
metrics | Yes | No |
mappings | Yes | No |
prometheus | N/A | No |
scheduledtasks | Yes | No |
sessions | Yes | No |
shutdown | Yes | No |
threaddump | Yes | No |
配置启动和禁用
Property | Default |
---|---|
management.endpoints.jmx.exposure.exclude | |
management.endpoints.jmx.exposure.include | * |
management.endpoints.web.exposure.exclude | |
management.endpoints.web.exposure.include | info, health |
我们可以设置控制要开放的endpoint,例如我们只开放health和info,其他都禁用我们可以设置
management.endpoints.jmx.exposure.include=health,info
*号可以用来表示所有,我们在web端开放所有但是禁用env和beans,可以这么来设置
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
Note: as "*" has a special meaning in YAML, so be sure to add quotes if you want to include (or exclude) all endpoints, as shown in the following example:
management:
endpoints:
web:
exposure:
include: "*"
通过这些设置我们就可以在springboot2.x版本里面去控制自己想要的监控的endpoint.
禁用endpoint
我们可以通过设置management.endpoint..enable=true/false来设置是否启用对应的endpoint,例子是启用了shutdown服务和info,并且仅用了其他所有的endpoint包括health也被禁用了
management.endpoints.enabled-by-default=false
management.endpoint.shutdown.enabled=true
management.endpoint.info.enabled=true