博客
关于我
Prometheus(2):SpringBoot 2.X集成Prometheus
阅读量:795 次
发布时间:2023-03-04

本文共 4575 字,大约阅读时间需要 15 分钟。

Spring Boot 接入 Prometheus

引入 Prometheus 和 Spring Boot 统计 metrics

本文将介绍如何将 Spring Boot 应用与 Prometheus 进行集成,通过配置和代码实现 metrics 的采集与展示,最后在 Grafana 上进行可视化。

1.1 pom.xml 配置

首先,我们需要在项目的 pom.xml 中引入必要的依赖包。

io.github.mweirauch
micrometer-jvm-extras
0.1.4
org.springframework.boot
spring-boot-starter-actuator
io.micrometer
micrometer-registry-prometheus
1.2.0
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-configuration-processor
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine

1.2 配置 application.properties

在应用的配置文件中,添加以下内容以配置 metrics 的标签和其他相关设置。

spring.application.name=PrometheusApplicationspring.application.tags.management.metrics.tags.application=${spring.application.name}management.endpoints.web.exposure.include=*management.endpoints.web.exposure.exclude=env,beansmanagement.endpoint.shutdown.enabled=truemanagement.metrics.export.simple.enabled=false

1.3 设置定时任务

为了实现 metrics 的动态更新,我们需要在 Spring Boot 应用中添加定时任务。

@SpringBootApplication@EnableSchedulingpublic class PrometheusApplication {    public static void main(String[] args) {        SpringApplication.run(PrometheusApplication.class, args);    }}

1.4 实现 MeterBinder 接口

创建一个组件类,实现 MeterBinder 接口,注册到 MeterRegistry 中。

package com.imooc.springboot.prometheus.metrics;import io.micrometer.core.instrument.Counter;import io.micrometer.core.instrument.Gauge;import io.micrometer.core.instrument.MeterRegistry;import io.micrometer.core.instrument.binder.MeterBinder;import org.springframework.stereotype.Component;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;@Componentpublic class MetricsBinder implements MeterBinder {    private Map
map = new ConcurrentHashMap<>(); public Counter counter1; public Counter counter2; @Override public void bindTo(MeterRegistry meterRegistry) { counter1 = Counter.builder("PrometheusApplication.demo.counter") .tags(new String[]{"name", "counter1"}) .description("This is the first counter") .register(meterRegistry); counter2 = Counter.builder("PrometheusApplication.demo.counter") .tags(new String[]{"name", "counter2"}) .description("This is the second counter") .register(meterRegistry); Gauge.builder("PrometheusApplication.demo.gauge", map, x -> x.get("x")) .tags("name", "gauge1") .description("This is a gauge") .register(meterRegistry); }}

1.5 实现定时任务更新 metrics

通过定时任务不断更新 metrics 的值。

package com.imooc.springboot.prometheus.metrics;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class ScheduledTask {    @Autowired    private MetricsBinder metricsBinder;    private int count1;    private int count2;    @Scheduled(fixedRate = 1000)    public void increment1() {        count1++;        metricsBinder.counter1.increment();        metricsBinder.map.put("x", Double.valueOf(count1));        System.out.println("increment 1 count: " + count1);    }    @Scheduled(fixedRate = 10000)    public void increment2() {        count2++;        metricsBinder.counter2.increment();        metricsBinder.map.put("x", Double.valueOf(count2));        System.out.println("increment 2 count: " + count2);    }}

Prometheus 配置与 Grafana 可视化

2.1 Prometheus 配置

在 Prometheus 目录下进行配置,确保应用程序已正确暴露 metrics 端点。

2.2 使用 Grafana 模板

在 Grafana 上导入模板编号 4701,或通过下载模板文件进行导入。

2.3 配置数据源

在 Grafana 中添加 Prometheus 数据源,确保指标能够正确获取。

通过以上配置和代码实现,你可以在 Prometheus 中看到应用程序的 metrics 数据,并在 Grafana 上进行可视化分析。

转载地址:http://yrxfk.baihongyu.com/

你可能感兴趣的文章
pringBoot Controller接收参数的几种常用方式
查看>>
printf()函数
查看>>
PyTorch中的自定义权重初始化
查看>>
printf格式字符串和输出列表个数及类型不匹配案例
查看>>
printf的格式控制字符串
查看>>
PrintStream概述
查看>>
Prismix:Prisma 架构混合器,为复杂项目而生
查看>>
pritunl服务安装及配置
查看>>
Private Destructor
查看>>
private和protected能同时修饰成员变量吗_天天用注解你了解注解是怎么实现的吗?...
查看>>
privoxy Invalid header received from client.
查看>>
Privoxy代码下载
查看>>
Probabilistic-Programming-and-Bayesian-Methods-for-Hackers
查看>>
pytorch中的求和函数sum(axis=x)解释
查看>>
Problem F: 质心算法
查看>>
Problem N HDU 2612 Find a way (两次BFS求最值)
查看>>
Process /usr/libexec/gdu-notification-daemon was killed by signal 6 (SIGABRT)
查看>>
process.env.VUE_APP_BASE_API 获取不到
查看>>
Process.run() 和 Process.start() 之间的区别
查看>>
Processes
查看>>