目录
一、可观测性
导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
通过springboot配置文件配置暴露端点
#通过web暴露所有监控端点
management.endpoints.web.exposure.include=*
通过访问地址得到所有http://localhost:8080/actuator
二、定制健康监控的端点
@Component
public class MyComponent {
public int check(){
return 1;
}
}
@Component
public class MyHealthIndicator extends AbstractHealthIndicator {
@Autowired
private MyComponent myComponent;
/**
* 检查健康
* @param builder
* @throws Exception
*/
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
//自定义检查方法
int check = myComponent.check();
if (check == 1){
//存活
builder.up()
.withDetail("code","200")
.withDetail("msg","健康的运行")
.build();
}else {
builder.down()
.withDetail("code","400")
.withDetail("msg","运行崩溃了")
.build();
}
}
}
配置文件
management.endpoint.health.enabled=true
management.endpoint.health.show-details=always
效果图
三、定制metrics
@Component
public class MyComponent {
Counter counter = null;
public MyComponent(MeterRegistry meterRegistry) {
counter = meterRegistry.counter("My.hello");
}
public int check(){
return 1;
}
public void hello(){
System.out.println("hello");
counter.increment();
}
}
@RestController
public class HelloController {
@Autowired
private MyComponent myComponent;
@GetMapping("/hello")
public void hello(){
myComponent.hello();
}
}
效果图
四、整合Prometheus+Grafana
导入Prometheus需要的依赖
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
将自己的Java程序打包上传到服务器部署
安装liunx的jdk17
后台运行Java服务
修改 Prometheus配置文件
测试访问:
整合Grafana