一、需求

实现SpringBoot和Prometheus的一个简单整合。

二、实现步骤

1、引入jar包

<dependencies>
<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>
<scope>runtime</scope>
</dependency>
</dependencies>

2、application.prometheus文件配置

1、启用prometheus端点。
2、给每个指标指定一个通用的标签,application见下方的截图。
3、指定管理的端口和路径。

spring.application.name=actuator-prometheus
server.port=10001 # 管理端点的跟路径,默认就是/actuator
management.endpoints.web.base-path=/actuator
# 管理端点的端口
management.server.port=10002
# 暴露出 prometheus 端口
management.endpoints.web.exposure.include=prometheus
# 启用 prometheus 端口,默认就是true
management.metrics.export.prometheus.enabled=true # 增加每个指标的全局的tag,及给每个指标一个 application的 tag,值是 spring.application.name的值
management.metrics.tags.application=${spring.application.name}

3、查看指标数据

可以看到里面存在一个 process_files_open_files 指标,待会儿集成到prometheus中,我们在prometheus中查询一下这个指标,看是否存在。

4、接入到 prometheus 中

1、修改 prometheus.yml 配置文件

scrape_configs:
- job_name: 'spring-boot-actuator-exporter'
# 指定抓取的路径
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:10002']
labels:
nodename: 'spring-boot-actuator'

2、在 prometheus 中查看指标数据

三、个性化 MeterRegistry

1、增加全局标签

当我们和 prometheus 集成后,默认的 MeterRegistry是 PrometheusMeterRegistry
当我们要 个性化 MeterRegistry 时,可以实现MeterRegistryCustomizer接口。
eg:
1、我们要为所有的指标增加一个 role=admin 的标签。

@Configuration
public class CustomMeterRegistryCustomizer implements MeterRegistryCustomizer<MeterRegistry> { @Override
public void customize(MeterRegistry registry) {
registry.config().commonTags("role", "master");
}
}

2、对标签、指标名称等进行过滤。

比如:替换标签的值、判断是否需要该指标等等,此处实现只需要jvm开头的指标,其余的指标一律排除。,需要实现MeterFilter接口。

@Configuration
public class CustomMeterFilter implements MeterFilter {
/**
* 只需要和jvm相关的指标数据,其余的数据一律拒绝
*/
@Override
public MeterFilterReply accept(Meter.Id id) {
if (id.getName().startsWith("jvm")) {
return MeterFilterReply.ACCEPT;
}
return MeterFilterReply.DENY;
}
}

3、监控业务数据

1、如果我们的监控依赖其它的bean,推荐使用MeterBinder注册

@Component
public class OrderAmountMeterBinder implements MeterBinder { @Autowired
private OrderService orderService; @Override
public void bindTo(@NonNull MeterRegistry registry) {
Gauge.builder("order_amount", orderService, service -> service.retrieveOrderAmount().doubleValue())
// 这个 fen 会接到 order_amount的后面及在 prometheus 中的指标名称为 order_amount_fen
.baseUnit("fen")
.description("获取订单的金额")
.tag("system", "order")
.strongReference(false)
.register(registry);
}
}

2、不依赖其它bean

@Service
public class OrderService {
@Autowired
private MeterRegistry meterRegistry; @PostConstruct
public void init() {
Executors.newSingleThreadScheduledExecutor()
.scheduleAtFixedRate(this::createOrder, 0, 1, TimeUnit.SECONDS); } /**
* 创建订单
*/
public int createOrder() {
Counter.builder("order_counter")
.tag("system", "order")
.baseUnit("total")
.description("订单的总数量")
.register(meterRegistry)
.increment(); return ORDER_CNT.get();
}
}

四、代码路径

https://gitee.com/huan1993/spring-cloud-parent/tree/master/prometheus/actuator-prometheus

五、参考链接

1、https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-metrics-getting-started[https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-metrics-getting-started]
2、代码路径-io.micrometer.prometheus.PrometheusNamingConvention#name
3、代码路径-org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter#record

SpringBoot整合Prometheus的更多相关文章

  1. 【Springboot】实例讲解Springboot整合OpenTracing分布式链路追踪系统(Jaeger和Zipkin)

    1 分布式追踪系统 随着大量公司把单体应用重构为微服务,对于运维人员的责任就更加重大了.架构更复杂.应用更多,要从中快速诊断出问题.找到性能瓶颈,并不是一件容易的事.因此,也随着诞生了一系列面向Dev ...

  2. SpringBoot2.x整合Prometheus+Grafana【附源码+视频】

    图文并茂,新手入门教程,建议收藏 SpringBoot2.x整合Prometheus+Grafana[附源码+视频] 附源码+视频 目录 工程简介 简介 Prometheus grafana Spri ...

  3. spring-boot整合mybatis(1)

    sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...

  4. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  5. springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...

  6. springboot整合mybaits注解开发

    springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...

  7. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  8. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

  9. SpringBoot整合Kafka和Storm

    前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...

随机推荐

  1. 快速模式第三包:quick_inR1_outI2()

    快速模式第三包:quick_inR1_outI2() 文章目录 快速模式第三包:quick_inR1_outI2() 1. 序言 2. quick_inR1_outI2()的处理流程 3. 快速模式第 ...

  2. JS020. Array map()函数查到需要的元素时跳出遍历循环,不再执行到数组边界

    Array.prototype.map() map( )  方法创建一个 新数组 *,其结果是该数组中的每个元素是调用一次提供的 函数后的返回值 *.[ MDN / RUNOOB ] * map 添加 ...

  3. 博主有偿带徒 《编程语言设计和实现》《MUD游戏开发》《软件破解和加密》《游戏辅助外挂》《JAVA开发》

    <考研专题>操作系统原理 理论解答:8K 实战 1.5W CPU设计 理论解答:1W 实战 2.5W <编程语言设计和实现>初窥门径<5K>:编译原理.编译设计小试 ...

  4. Windows中nginx多次启动的问题

    在Windows上做开发环境中的nginx服务器.为了使nginx在后台运行,使用如下命令来启停nginx: cd <nginx安装目录> # 开启nginx并在后台运行 start ng ...

  5. Sentry Web 前端监控 - 最佳实践(官方教程)

    系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...

  6. spark集群的构建,python环境

    个人笔记,问题较多 符号说明 [] 表示其中内容可以没有 su [root] 获取root权限 vi /etc/sudoers 1.点击I或Insert获得插入权限 2.在root ALL=(ALL) ...

  7. java基础语法以及进制的转换

    关键字 关键字: 被Java语言赋予特定含义的单词 关键字特点 组成关键字的字母全部小写 关键字注意事项 goto和const作为保留字存在,目前并不使用 类似IDEA这样的集成工具,针对关键字有特殊 ...

  8. ysoserial CommonsColletions3分析(1)

    CC3的利用链在JDK8u71版本以后是无法使用的,具体还是由于AnnotationInvocationHandler的readobject进行了改写. 而CC3目前有两条主流的利用链,利用Trans ...

  9. Django学习day04随堂笔记

    每日测验 """ 今日考题 1.列举你知道的orm数据的增删改查方法 2.表关系如何判定,django orm中如何建立表关系,有什么特点和注意事项 3.请画出完整的dj ...

  10. jquery监听动态添加的input的change事件

    使用下面方法在监听普通的input的change事件正常 $('#pp').on('change', 'input.videos_poster_input', function () { consol ...