通过Prometheus + Grafana对线上应用进行观测、监控、预警…

  • 健康状况【组件状态、存活状态】Health
  • 运行指标【cpu、内存、垃圾回收、吞吐量、响应成功率…】Metrics

1. SpringBoot Actuator

1. 基本使用

1. 场景引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. 暴露指标

management:
  endpoints:
    enabled-by-default: true #暴露所有端点信息
    web:
      exposure:
        include: '*'  #以web方式暴露

3. 访问数据

2. Endpoint

1. 常用端点

2. 定制端点

  • 健康监控:返回存活、死亡
  • 指标监控:次数、率
1. HealthEndpoint
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

}

构建Health
Health build = Health.down()
                .withDetail("msg", "error service")
                .withDetail("code", "500")
                .withException(new RuntimeException())
                .build();
management:
    health:
      enabled: true
      show-details: always #总是显示详细信息。可显示每个模块的状态信息
@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {

    /**
     * 真实的检查方法
     * @param builder
     * @throws Exception
     */
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        //mongodb。  获取连接进行测试
        Map<String,Object> map = new HashMap<>();
        // 检查完成
        if(1 == 2){
//            builder.up(); //健康
            builder.status(Status.UP);
            map.put("count",1);
            map.put("ms",100);
        }else {
//            builder.down();
            builder.status(Status.OUT_OF_SERVICE);
            map.put("err","连接超时");
            map.put("ms",3000);
        }


        builder.withDetail("code",100)
                .withDetails(map);

    }
}
2. MetricsEndpoint
class MyService{
    Counter counter;
    //默认一个构造时,参数会从ioc中拿
    public MyService(MeterRegistry meterRegistry){
         counter = meterRegistry.counter("myservice.method.running.counter");
    }

    public void hello() {
        counter.increment();
    }
}

2. 监控落地

1. 安装 Prometheus + Grafana

2. 导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.10.6</version>
</dependency>
management:
  endpoints:
    web:
      exposure: #暴露所有监控的端点
        include: '*'

SpringBoot3 整合Prometheus + Grafana-LMLPHP

确保可以访问到部署好的服务,http://192.168.254.129:8080/actuator/prometheus

SpringBoot3 整合Prometheus + Grafana-LMLPHP

http://192.168.254.129:8080/actuator

SpringBoot3 整合Prometheus + Grafana-LMLPHP

3. 配置 Prometheus 拉取数据

## 修改 prometheus.yml 配置文件
scrape_configs:
  - job_name: 'spring-boot-actuator-exporter'
    metrics_path: '/actuator/prometheus' #指定抓取的路径
    static_configs:
      - targets: ['192.168.254.129:8080']
        labels:
          nodename: 'app-demo'

SpringBoot3 整合Prometheus + Grafana-LMLPHP

4. 配置 Grafana 监控面板

  • 添加数据源(Prometheus)

SpringBoot3 整合Prometheus + Grafana-LMLPHP

  • 添加面板。可去 grafana dashboard 市场找一个自己喜欢的面板,也可以自己开发面板
    • 市场直接搜索springboot,注意看面板支持的数据源,复制面板ID

SpringBoot3 整合Prometheus + Grafana-LMLPHP

SpringBoot3 整合Prometheus + Grafana-LMLPHP

5. 效果

08-03 09:37