Spring Boot Actuator 整合 Prometheus

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
简介: Spring Boot 自带监控功能 Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、环境变量、日志信息、线程信息等。这一节结合 Prometheus 、Grafana 来更加直观的展示这些信息。

简介

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

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

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";
    }

    /**
    @Bean
    MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
        return registry -> registry.config().commonTags("application", "springboot-actuator-prometheus-demo");
    }
    */

}

配置 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 状态,说明配置成功了。

1

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

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

查看JVM (Micormeter) dashboard:

2

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

源码

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

参考

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

往期内容

相关文章
|
21天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
38 0
|
3月前
|
消息中间件 Java 开发工具
Spring Cloud【Finchley】实战-06使用/actuator/bus-refresh端点手动刷新配置 + 使用Spring Cloud Bus自动更新配置
Spring Cloud【Finchley】实战-06使用/actuator/bus-refresh端点手动刷新配置 + 使用Spring Cloud Bus自动更新配置
62 0
|
1天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
4 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
3天前
|
XML Java C++
【Spring系列】Sping VS Sping Boot区别与联系
【4月更文挑战第2天】Spring系列第一课:Spring Boot 能力介绍及简单实践
28 0
【Spring系列】Sping VS Sping Boot区别与联系
|
1月前
|
消息中间件 运维 监控
|
2月前
|
XML 监控 druid
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
|
3月前
|
Prometheus 监控 Cloud Native
微服务框架(十九)Spring Boot 可视化监控 Prometheus + Grafana
  此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。   本文为Spring Boot 通过 micrometer 的监控门面,实现Prometheus + G...
|
3月前
|
Prometheus 监控 Cloud Native
SpringBoot3 整合Prometheus + Grafana
SpringBoot3 整合Prometheus + Grafana
SpringBoot3 整合Prometheus + Grafana
|
3月前
|
Prometheus 监控 Cloud Native
Spring Boot应用如何快速接入Prometheus监控
Spring Boot应用如何快速接入Prometheus监控
53 0
|
3月前
|
Java
springboot项目打包瘦身
springboot项目打包瘦身