栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

Spring Boot Actuator 整合 Prometheus

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot Actuator 整合 Prometheus

简介

Spring Boot 自带监控功能 Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、环境变量、日志信息、线程信息等。这一节结合 Prometheus 、Grafana 来更加直观的展示这些信息。

实验 说明
服务名 地址 端口
Prometheus 172.16.2.101 9090
Grafana 172.16.2.101 3000
Spring Boot Demo 172.16.2.204 8080
创建项目

创建用于测试的 Spring Boot 项目,主要代码如下。

pom.xml

    org.springframework.boot
    spring-boot-starter-actuator


    org.springframework.boot
    spring-boot-starter-web


    io.micrometer
    micrometer-registry-prometheus



    org.springframework.boot
    spring-boot-starter-test
    test

application.yml
management:
  endpoints:
    web:
      exposure:
 include: '*'

  endpoint:
    health:
      show-details: always

  metrics:
    tags:
      application: actuator-demo
  • management.endpoints.web.exposure.include:大多数actuator的端口都不会通过http公开,* 代表公开所有这些端点。对于生产环境,应该仔细选择要公开的端点。
  • management.metrics.tags.application:为应用设置 tag ,方便区分不同的应用。
启动类
@SpringBootApplication
@RestController
public class SpringbootActuatorPrometheusDemoApplication {

    public static void main(String[] args) {
 SpringApplication.run(SpringbootActuatorPrometheusDemoApplication.class, args);
    }

    @RequestMapping(value = "/hello")
    public String  sayHello() {
 for (int i = 1 ; i <= 10 ; i++) {
     Thread t = new Thread(() -> {
  try {
      Thread.sleep(5000);
  } catch (InterruptedException e) {
      e.printStackTrace();
  }
     } , "HelloThread - " + i);
     t.start();
 }
 return "ok";
    }

    

}
配置 Prometheus 和 Grafana

在 prometheus.yml 中添加针对该 Spring Boot 应用 的监控 job

- job_name: 'actuator-demo'
    metrics_path: '/prometheus'
    static_configs:
    - targets: ['172.16.2.204:8080']

运行 Prometheus 和 Grafana:

docker start prometheus grafana

访问 Prometheus UI http://172.16.2.101:9090 ,查看 targets ,可以看到 job 处于 UP 状态,说明配置成功了。

Grafana UI http://172.16.2.101:3000,通过Grafana的 + 图标导入(import) JVM (Micrometer) dashboard:

  • grafana id = 4701
  • 注意选中prometheus数据源

查看JVM (Micormeter) dashboard:

可以看到应用的 JVM 的 堆栈、 线程、 IO 等等信息。

源码

https://github.com/gf-huanchupk/SpringBootLearning/tree/master/springboot-actuator-prometheus

参考

https://micrometer.io/docs/registry/prometheus
https://prometheus.io/docs/prometheus

往期内容
  • Prometheus 入门
  • Prometheus 与 Grafana 集成
  • Prometheus 整合 alertManager
  • Prometheus 集成 Node Exporter
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号