提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
springboot整合actuator
使用步骤
1.引入依赖
代码如下(示例):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.application.yml配置
代码如下(示例):
# 应用名称
spring.application.name=springboot-actuator
# Actuator Web 访问端口
management.server.port=8081
#暴露所有的端点
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#为true时默认开启所有的端点
management.endpoints.enabled-by-default=false
#选择开启部分端点
management.endpoint.health.enabled=true
management.endpoint.info.enabled=true
management.endpoint.beans.enabled=true
management.endpoint.metrics.enabled=true
#自定义配置info
info.appname=boot-admin
info.appversion=1.0.0
# 应用服务 WEB 访问端口
server.port=8090
3.自定义info信息
package com.actuator;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
@Component
public class MyInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("hello", "world")
.withDetail("你好", "李焕英");
}
}
4.自定义health信息
package com.actuator;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class MyAbstractHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
Map<String,Object> map=new HashMap<>();
//此处模拟状态
if(1==1){
builder.up();
map.put("一切正常", "万事大吉");
}else{
builder.down();
map.put("完了白给", "删库跑路");
}
builder.withDetails(map);
}
}
//自定义的health如果down,那么整体的health都会是down的状态