prometheus获取Consul上注册的服务

来源:互联网 发布:java hadoop 读取文件 编辑:程序博客网 时间:2024/05/29 04:37

    在上一篇《springboot集成prometheus》里,已介绍了springboot和promethues的集成,但是这里有个问题,就是在prometheus.yml里配置需要监听的服务时,是按服务名写死的,如果后面增加了微服务,就得手动修改此配置,并重启promethues;那么能否动态的监听微服务呢。我们知道,在分布式系统架构里,有个组件负责注册和发现所有微服务,那就是注册中心。常用的注册中心组件有Spring Cloud Netflix的Eureka,consul,dubbo等,如果promethues能监听服务注册中心的微服务,就能实现动态监听服务的功能了。这里以consul为例来整合promethues。


Consul 是什么


Consul 是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla Public License 2.0 的协议进行开源. Consul 支持健康检查,并允许 HTTP 和 DNS 协议调用 API 存储键值对.

命令行超级好用的虚拟机管理软件 vgrant 也是 HashiCorp 公司开发的产品.
一致性协议采用 Raft 算法,用来保证服务的高可用. 使用 GOSSIP 协议管理成员和广播消息, 并且支持 ACL 访问控制.


Consul 的使用场景

  • docker 实例的注册与配置共享
  • coreos 实例的注册与配置共享
  • vitess 集群
  • SaaS 应用的配置共享
  • 与 confd 服务集成,动态生成 nginx 和 haproxy 配置文件

将微服务注册到consul上




    通过查看promethues配置文件的官方文档,发现promethues提供了和多种服务发现注册中心整合的配置选项,包括Azure,Consul,DNS,EC2,OpenStack,GCE,Kubernetes等;关键的具体配置如下:

scrape_configs:  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.  - job_name: 'prometheus'    # Override the global default and scrape targets from this job every 5 seconds.    scrape_interval: 5s    # metrics_path defaults to '/metrics'    # scheme defaults to 'http'.    static_configs:      - targets: ['localhost:9090']  - job_name: 'security'    # Override the global default and scrape targets from this job every 5 seconds.    scrape_interval: 5s    metrics_path: '/prometheus'    # scheme defaults to 'http'.    static_configs:      - targets: ['10.94.20.33:80']  - job_name: 'overwritten-default'    consul_sd_configs:    - server:   '10.110.200.29:8500'      services: ['lookup', 'security', 'workflow']    relabel_configs:    - source_labels: ['__metrics_path__']      regex:         '/metrics'      target_label:  __metrics_path__      replacement:   '/prometheus'

这里简单说明下上面的配置意义:
scrape_configs下,定义了3个job_name,其中  - job_name: 'prometheus'是监听prometheus服务本身;job_name: 'security'是按固定IP:PORT的方式监听微服务 ;job_name: 'overwritten-default'就是一个监听consul的任务,在consul_sd_configs下,server是consul服务器的访问地址,services是微服务名的数组,如果什么都不填,则默认取consul上注册的所有微服务。relabel_configs是修改默认配置的规则,这里由于使用了
springboot和promethues整合,暴露的metrics是通过/promethues路径访问的,而promethues默认的metrics访问路径(即metrics_path配置项)是/metrics,需要修改。
如下图:当把鼠标放在某个label上时,显示了Before relabeling的配置,可以看到__metrics_path__='/metrics',所以必须通过relabel_configs方式修改为‘/promethues’后,才能让此微服务的状态为UP,不然会因为不符合格式错误而使Endpoint的状态为DOWN





关于relabel_configs的更多配置详解,请参考官方文档,这里只是替换文本的最基本用法。
阅读全文
0 0
原创粉丝点击