prometheus获取Consul上注册的服务
? ? ?在上一篇《springboot集成prometheus》里,已介紹了springboot和promethues的集成,但是這里有個問題,就是在prometheus.yml里配置需要監(jiān)聽的服務(wù)時,是按服務(wù)名寫死的,如果后面增加了微服務(wù),就得手動修改此配置,并重啟promethues;那么能否動態(tài)的監(jiān)聽微服務(wù)呢。我們知道,在分布式系統(tǒng)架構(gòu)里,有個組件負(fù)責(zé)注冊和發(fā)現(xiàn)所有微服務(wù),那就是注冊中心。常用的注冊中心組件有Spring Cloud Netflix的Eureka,consul,dubbo等,如果promethues能監(jiān)聽服務(wù)注冊中心的微服務(wù),就能實(shí)現(xiàn)動態(tài)監(jiān)聽服務(wù)的功能了。這里以consul為例來整合promethues。
Consul 是什么
Consul 是一個支持多數(shù)據(jù)中心分布式高可用的服務(wù)發(fā)現(xiàn)和配置共享的服務(wù)軟件,由 HashiCorp 公司用 Go 語言開發(fā), 基于 Mozilla Public License 2.0 的協(xié)議進(jìn)行開源. Consul 支持健康檢查,并允許 HTTP 和 DNS 協(xié)議調(diào)用 API 存儲鍵值對.
命令行超級好用的虛擬機(jī)管理軟件 vgrant 也是 HashiCorp 公司開發(fā)的產(chǎn)品.
一致性協(xié)議采用 Raft 算法,用來保證服務(wù)的高可用. 使用 GOSSIP 協(xié)議管理成員和廣播消息, 并且支持 ACL 訪問控制.
?
Consul 的使用場景
- docker 實(shí)例的注冊與配置共享
- coreos 實(shí)例的注冊與配置共享
- vitess 集群
- SaaS 應(yīng)用的配置共享
- 與 confd 服務(wù)集成,動態(tài)生成 nginx 和 haproxy 配置文件
?
將微服務(wù)注冊到consul上
? ? 通過查看promethues配置文件的官方文檔,發(fā)現(xiàn)promethues提供了和多種服務(wù)發(fā)現(xiàn)注冊中心整合的配置選項(xiàng),包括Azure,Consul,DNS,EC2,OpenStack,GCE,Kubernetes等;關(guān)鍵的具體配置如下:
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'是監(jiān)聽prometheus服務(wù)本身;job_name: 'security'是按固定IP:PORT的方式監(jiān)聽微服務(wù) ;job_name: 'overwritten-default'就是一個監(jiān)聽consul的任務(wù),在consul_sd_configs下,server是consul服務(wù)器的訪問地址,services是微服務(wù)名的數(shù)組,如果什么都不填,則默認(rèn)取consul上注冊的所有微服務(wù)。relabel_configs是修改默認(rèn)配置的規(guī)則,這里由于使用了
springboot和promethues整合,暴露的metrics是通過/promethues路徑訪問的,而promethues默認(rèn)的metrics訪問路徑(即metrics_path配置項(xiàng))是/metrics,需要修改。
如下圖:當(dāng)把鼠標(biāo)放在某個label上時,顯示了Before relabeling的配置,可以看到__metrics_path__='/metrics',所以必須通過relabel_configs方式修改為‘/promethues’后,才能讓此微服務(wù)的狀態(tài)為UP,不然會因?yàn)椴环细袷藉e誤而使Endpoint的狀態(tài)為DOWN。
?
關(guān)于relabel_configs的更多配置詳解,請參考官方文檔,這里只是替換文本的最基本用法。
總結(jié)
以上是生活随笔為你收集整理的prometheus获取Consul上注册的服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker使用Link在容器之间建立连
- 下一篇: Flowable V5.x迁移至Flow