Prometheus 使用 Consul 自动发现 Spring Boot 服务并拉取数据

本文主要是介绍Prometheus 使用 Consul 自动发现 Spring Boot 服务并拉取数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Prometheus 使用 Consul 自动发现 Spring Boot 服务并拉取数据

使用 Prometheus监控 SpringBoot 应用,当应用很多,且上下线频繁时,需要不断的更改 Prometheus 的配置文件,不能灵活的使用,可以通过为 Prometheus配置注册中心,从注册中心拉取应用数据获取监控数据

启动 Prometheus

  • 添加配置文件 prometheus.yaml
mkdir -p ~/docker/prometheus/config
vi ~/docker/prometheus/config/prometheus.yml
global:scrape_interval: 15sscrape_timeout: 10sevaluation_interval: 15s
alerting:alertmanagers:- static_configs:- targets: []scheme: httptimeout: 10sapi_version: v1
scrape_configs:
- job_name: prometheushonor_timestamps: truescrape_interval: 15sscrape_timeout: 10smetrics_path: /metricsscheme: httpstatic_configs:- targets:- localhost:9090
  • 启动容器
docker run \-d \--name prometheus \-p 9090:9090 \-v ~/docker/prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml \prom/prometheus    

启动 Consul

docker run \-d \--name consul \-p 8500:8500 \consul

启动 Spring Boot 应用

添加监控

  • 添加依赖 build.gradle
    compile 'org.springframework.cloud:spring-cloud-starter-consul-discovery'compile 'org.springframework.boot:spring-boot-starter-actuator'compile 'io.micrometer:micrometer-core:1.5.1'compile 'io.micrometer:micrometer-registry-prometheus:1.5.1' 
  • 修改应用配置 applicaiton.properties
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.prefer-ip-address=true
spring.cloud.consul.discovery.health-check-url=http://host.docker.internal:${server.port}/actuator/health
spring.cloud.consul.discovery.tags=metrics=truemanagement.endpoints.web.exposure.include=*
# prometheus
management.metrics.tags.application=${spring.application.name}

上面配置中指定健康检查是为了 Consul 从容器访问宿主机的应用,指定tag是为了Prometheus 从Consul列表中拉取需要监控的指定应用

使用 Consul 发现服务

修改 Prometheus 配置

  • prometheus.yaml
global:scrape_interval: 15sscrape_timeout: 10sevaluation_interval: 15s
alerting:alertmanagers:- static_configs:- targets: []scheme: httptimeout: 10sapi_version: v1
scrape_configs:- job_name: "prometheus"static_configs:- targets: ["localhost:9090"]- job_name: "consul"consul_sd_configs:- server: "host.docker.internal:8500"relabel_configs:- source_labels: ["__meta_consul_dc"]target_label: "dc"- source_labels: [__meta_consul_service]separator: ;regex: (.*)target_label: applicationreplacement: $1action: replace- source_labels: [__address__]separator: ":"regex: (127.0.0.1):(.*)target_label: __address__replacement: host.docker.internal:${2}action: replace- source_labels: [__metrics_path__]separator: ;regex: /metricstarget_label: __metrics_path__replacement: /actuator/prometheusaction: replace- source_labels: ['__meta_consul_tags']regex: '^.*,metrics=true,.*$'action: keep

其中 :

  • consul_sd_configs指定 Consul 的地址
  • relabel_configs 指定配置标签覆盖规则
  • __meta_consul_service
      - source_labels: [__meta_consul_service]separator: ;regex: (.*)target_label: applicationreplacement: $1action: replace

这个配置是将 __meta_consul_service 重新映射为 application字段,方便 Prometheus 查询

  • __address__
      - source_labels: [__address__]separator: ":"regex: (127.0.0.1):(.*)target_label: __address__replacement: host.docker.internal:${2}action: replace

这个配置是为了将 127.0.0.1的地址改为host.docker.internal,方便 Prometheus 从容器中访问宿主机的应用

  • __metrics_path__
      - source_labels: [__metrics_path__]separator: ;regex: /metricstarget_label: __metrics_path__replacement: /actuator/prometheusaction: replace

这个配置是为了将 Prometheus 默认的拉取数据 /metrics改成 /actuator/prometheus,方便从 Spring拉取,如果有多种不同的应用,数据路径不一样,建议设置多个job,以使用不同的规则

  • __meta_consul_tags
      - source_labels: ['__meta_consul_tags']regex: '^.*,metrics=true,.*$'action: keep

这个配置是为了筛选指定tag的应用,只有有这个tag的应用才会拉取监控数据,这里是 metrics=true,是在 Spring Boot的配置文件中配置的,这样就可以避免拉取不相关的数据的应用(如 Consul自己的数据,替换路径为/actuator/prometheus后无法获取到监控数据)

配置完成后重启 Prometheus

获取应用

  • Consul 中注册的应用

prometheus-consul-consul-service-list.png

  • Prometheus 的 Service Discovery

可以看到,consul这个服务下发现了三个服务,但是只有一个在拉取数据,另外两个被丢弃了,这是因为限制了只拉取 tag 为 metrics=true

prometheus-consul-prometheus-discovery-list.png

  • Prometheus 的 Target

可以看到 Target 也只拉取了这一个应用

prometheus-consul-prometheus-target-list.png

  • 查询监控数据

在 Prometheus 的 Graph中查询应用信息,也只能看到一个

process_uptime_seconds

prometheus-consul-prometheus-query-result1.png

  • 修改另一个 Spring Boot 应用,添加监控tag

再次查询 Prometheus 数据,可以看到另一个应用也开始拉取数据

prometheus-consul-prometheus-query-result2.png


  • 相关项目可以参考 grpc

这篇关于Prometheus 使用 Consul 自动发现 Spring Boot 服务并拉取数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1138620

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

关于DNS域名解析服务

《关于DNS域名解析服务》:本文主要介绍关于DNS域名解析服务,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录DNS系统的作用及类型DNS使用的协议及端口号DNS系统的分布式数据结构DNS的分布式互联网解析库域名体系结构两种查询方式DNS服务器类型统计构建DNS域

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具