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

相关文章

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

springboot security之前后端分离配置方式

《springbootsecurity之前后端分离配置方式》:本文主要介绍springbootsecurity之前后端分离配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的... 目录前言自定义配置认证失败自定义处理登录相关接口匿名访问前置文章总结前言spring boot secu

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

java中反射(Reflection)机制举例详解

《java中反射(Reflection)机制举例详解》Java中的反射机制是指Java程序在运行期间可以获取到一个对象的全部信息,:本文主要介绍java中反射(Reflection)机制的相关资料... 目录一、什么是反射?二、反射的用途三、获取Class对象四、Class类型的对象使用场景1五、Class

SpringBoot中封装Cors自动配置方式

《SpringBoot中封装Cors自动配置方式》:本文主要介绍SpringBoot中封装Cors自动配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot封装Cors自动配置背景实现步骤1. 创建 GlobalCorsProperties