prometheus获取Consul上注册的服务
? ? ?在上一篇《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: 5smetrics_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的更多配置詳解,請參考官方文檔,這里只是替換文本的最基本用法。
總結
以上是生活随笔為你收集整理的prometheus获取Consul上注册的服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker使用Link在容器之间建立连
- 下一篇: CentOS、Ubuntu、Debian