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中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

Rust中的注释使用解读

《Rust中的注释使用解读》本文介绍了Rust中的行注释、块注释和文档注释的使用方法,通过示例展示了如何在实际代码中应用这些注释,以提高代码的可读性和可维护性... 目录Rust 中的注释使用指南1. 行注释示例:行注释2. 块注释示例:块注释3. 文档注释示例:文档注释4. 综合示例总结Rust 中的注释