偶然间看到了pom依赖健康检测,所以也来试一试它的简单使用:
1.pom文件添加依赖
<!-- 健康监测 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Web支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.启动springboot项目
那我们可以访问一下health 和 info:
打开地址:http://localhost:8085/actuator/info 或者 http://localhost:8085/actuator/health
3.application.yml自定义配置(这里配置了暴露所有endpoint,并配置访问路径(如 health):ip:8081/health)
server:
port: 8085
management: # actuator
server:
port: 8081 # actuator access port
endpoints:
web:
base-path: / # root path
exposure:
include: "*" # include all endpoint
4.自定义Endpoint,编写自定义的Endpoint
@Configuration
@Endpoint(id = "demo-endpoint")
public class DemoEndpoint {
@ReadOperation(produces = "application/json")
public Map<String, Object> operation(){
Map<String, Object> map = new HashMap<String, Object>();
map.put("demo", "{demo: endpoint test}");
return map;
}
}
访问:http://localhost:8081/demo-endpoint
注意:
@EndPoint中的id不能使用驼峰法,需要以-分割
Spring Boot会去扫描@Endpoint注解下的@ReadOperation, @WriteOperation, @DeleteOperation注解,分别对应生成Get/Post/Delete的Mapping。
注解中有个produces参数,可以指定media type, 如:application/json等。
如果将include设置为*,则开启全部监控功能,如果只想开启某个功能的监控,则,include中传你要开启的endpoint的名称,例如,我要开启beans和metrics功能include: beans,metrics