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

相关文章

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录