Eureka服務(wù)發(fā)現(xiàn)協(xié)議允許使用Eureka Rest API
檢索出Prometheus需要監(jiān)控的targets,Prometheus會(huì)定時(shí)周期性的從Eureka調(diào)用Eureka Rest API
,并將每個(gè)應(yīng)用實(shí)例創(chuàng)建出一個(gè)target。
Eureka服務(wù)發(fā)現(xiàn)協(xié)議支持對(duì)如下元標(biāo)簽進(jìn)行relabeling
:
(資料圖片僅供參考)
__meta_eureka_app_name
: the name of the app__meta_eureka_app_instance_id
: the ID of the app instance__meta_eureka_app_instance_hostname
: the hostname of the instance__meta_eureka_app_instance_homepage_url
: the homepage url of the app instance__meta_eureka_app_instance_statuspage_url
: the status page url of the app instance__meta_eureka_app_instance_healthcheck_url
: the health check url of the app instance__meta_eureka_app_instance_ip_addr
: the IP address of the app instance__meta_eureka_app_instance_vip_address
: the VIP address of the app instance__meta_eureka_app_instance_secure_vip_address
: the secure VIP address of the app instance__meta_eureka_app_instance_status
: the status of the app instance__meta_eureka_app_instance_port
: the port of the app instance__meta_eureka_app_instance_port_enabled
: the port enabled of the app instance__meta_eureka_app_instance_secure_port
: the secure port address of the app instance__meta_eureka_app_instance_secure_port_enabled
: the secure port of the app instance__meta_eureka_app_instance_country_id
: the country ID of the app instance__meta_eureka_app_instance_metadata_
: app instance metadata__meta_eureka_app_instance_datacenterinfo_name
: the datacenter name of the app instance__meta_eureka_app_instance_datacenterinfo_
: the datacenter metadataeureka_sd_configs
常見配置如下:
- job_name: "eureka" eureka_sd_configs: - server: http://localhost:8761/eureka #eureka server地址 refresh_interval: 1m #刷新間隔,默認(rèn)30s
eureka_sd_configs
官網(wǎng)支持主要配置如下:
server: basic_auth: [ username: ] [ password: ] [ password_file: ]# Configures the scrape request"s TLS settings.tls_config: [ ]# Optional proxy URL.[ proxy_url: ]# Configure whether HTTP requests follow HTTP 3xx redirects.[ follow_redirects: | default = true ]# Refresh interval to re-read the app instance list.[ refresh_interval: | default = 30s ]
基于Eureka
服務(wù)發(fā)現(xiàn)協(xié)議核心邏輯都封裝在discovery/eureka.go
的func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error)
方法中:
func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) { // 通過Eureka REST API接口從eureka拉取元數(shù)據(jù):http://ip:port/eureka/apps apps, err := fetchApps(ctx, d.server, d.client) if err != nil { return nil, err } tg := &targetgroup.Group{ Source: "eureka", } for _, app := range apps.Applications {//遍歷app // targetsForApp()方法將app下每個(gè)instance部分轉(zhuǎn)成target targets := targetsForApp(&app) //解析的采集點(diǎn)合入一起 tg.Targets = append(tg.Targets, targets...) } return []*targetgroup.Group{tg}, nil}
refresh
方法主要有兩個(gè)流程:
1、fetchApps()
:從eureka-server
的/eureka/apps
接口拉取注冊(cè)服務(wù)信息;
2、targetsForApp()
:遍歷app
下instance
,將每個(gè)instance
解析出一個(gè)target
,并添加一堆元標(biāo)簽數(shù)據(jù)。
如下示例從eureka-server
的/eureka/apps
接口拉取的注冊(cè)服務(wù)信息:
1 UP_1_ SERVICE-PROVIDER-01 localhost:service-provider-01:8001 192.168.3.121 SERVICE-PROVIDER-01 192.168.3.121 UP UNKNOWN 8001 443 1 MyOwn 30 90 1629385562130 1629385682050 0 1629385562132 8001 true 8080 http://192.168.3.121:8001/ http://192.168.3.121:8001/actuator/info http://192.168.3.121:8001/actuator/health service-provider-01 service-provider-01 false 1629385562132 1629385562039 ADDED
instance
信息會(huì)被解析成采集點(diǎn)target
:
func targetsForApp(app *Application) []model.LabelSet { targets := make([]model.LabelSet, 0, len(app.Instances)) // Gather info about the app"s "instances". Each instance is considered a task. for _, t := range app.Instances { var targetAddress string // __address__取值方式:instance.hostname和port,沒有port則默認(rèn)port=80 if t.Port != nil { targetAddress = net.JoinHostPort(t.HostName, strconv.Itoa(t.Port.Port)) } else { targetAddress = net.JoinHostPort(t.HostName, "80") } target := model.LabelSet{ model.AddressLabel: lv(targetAddress), model.InstanceLabel: lv(t.InstanceID), appNameLabel: lv(app.Name), appInstanceHostNameLabel: lv(t.HostName), appInstanceHomePageURLLabel: lv(t.HomePageURL), appInstanceStatusPageURLLabel: lv(t.StatusPageURL), appInstanceHealthCheckURLLabel: lv(t.HealthCheckURL), appInstanceIPAddrLabel: lv(t.IPAddr), appInstanceVipAddressLabel: lv(t.VipAddress), appInstanceSecureVipAddressLabel: lv(t.SecureVipAddress), appInstanceStatusLabel: lv(t.Status), appInstanceCountryIDLabel: lv(strconv.Itoa(t.CountryID)), appInstanceIDLabel: lv(t.InstanceID), } if t.Port != nil { target[appInstancePortLabel] = lv(strconv.Itoa(t.Port.Port)) target[appInstancePortEnabledLabel] = lv(strconv.FormatBool(t.Port.Enabled)) } if t.SecurePort != nil { target[appInstanceSecurePortLabel] = lv(strconv.Itoa(t.SecurePort.Port)) target[appInstanceSecurePortEnabledLabel] = lv(strconv.FormatBool(t.SecurePort.Enabled)) } if t.DataCenterInfo != nil { target[appInstanceDataCenterInfoNameLabel] = lv(t.DataCenterInfo.Name) if t.DataCenterInfo.Metadata != nil { for _, m := range t.DataCenterInfo.Metadata.Items { ln := strutil.SanitizeLabelName(m.XMLName.Local) target[model.LabelName(appInstanceDataCenterInfoMetadataPrefix+ln)] = lv(m.Content) } } } if t.Metadata != nil { for _, m := range t.Metadata.Items { // prometheus label只支持[^a-zA-Z0-9_]字符,其它非法字符都會(huì)被替換成下劃線_ ln := strutil.SanitizeLabelName(m.XMLName.Local) target[model.LabelName(appInstanceMetadataPrefix+ln)] = lv(m.Content) } } targets = append(targets, target) } return targets}
解析比較簡(jiǎn)單,就不再分析,解析后的標(biāo)簽數(shù)據(jù)如下圖:
標(biāo)簽中有兩個(gè)特別說明下:
1、__address__
:這個(gè)取值instance.hostname
和port
(默認(rèn)80),所以要注意注冊(cè)到eureka
上的hostname
準(zhǔn)確性,不然可能無法抓取;
2、metadata-map
數(shù)據(jù)會(huì)被轉(zhuǎn)成__meta_eureka_app_instance_metadata_
格式標(biāo)簽,prometheus
進(jìn)行relabeling
一般操作metadata-map
,可以自定義metric_path
、抓取端口等;
3、prometheus
的label
只支持[a-zA-Z0-9_]
,其它非法字符都會(huì)被轉(zhuǎn)換成下劃線,具體參加:strutil.SanitizeLabelName(m.XMLName.Local)
;但是eureka
的metadata-map
標(biāo)簽含有下劃線時(shí),注冊(cè)到eureka-server
上變成雙下劃線,如下配置:
eureka: instance: metadata-map: scrape_enable: true scrape.port: 8080
通過/eureka/apps
獲取如下:
基于Eureka
服務(wù)發(fā)現(xiàn)原理如下圖:
基于eureka_sd_configs
服務(wù)發(fā)現(xiàn)協(xié)議配置創(chuàng)建Discoverer
,并通過協(xié)程運(yùn)行Discoverer.Run
方法,Eureka
服務(wù)發(fā)現(xiàn)核心邏輯封裝discovery/eureka.go
的func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error)
方法中。
refresh
方法中主要調(diào)用兩個(gè)方法:
1、fetchApps
:定時(shí)周期從Eureka Server
的/eureka/apps
接口拉取注冊(cè)上來的服務(wù)元數(shù)據(jù)信息;
2、targetsForApp
:解析上步驟拉取的元數(shù)據(jù)信息,遍歷app
下的instance
,將每個(gè)instance
解析成target
,并將其它元數(shù)據(jù)信息轉(zhuǎn)換成target
元標(biāo)簽可以用于relabel_configs
操作
標(biāo)簽:
中新網(wǎng)鄭州11月24日電 (王登峰)24日,記者從河南省應(yīng)急管理廳獲悉,為保障受災(zāi)困難群眾溫暖過冬,...
(抗擊新冠肺炎)浙江溫州發(fā)現(xiàn)5名密接者 所采樣本核酸檢測(cè)均為陰性 中新網(wǎng)溫州11月24日電(記者 ...
中新網(wǎng)西寧11月24日電 (記者 孫睿)青海機(jī)場(chǎng)公司24日發(fā)布最新通告,即日起,出港旅客進(jìn)入西寧曹家...
3月16日,盛和資源(600392)副總經(jīng)理毛韶春、黃厚兵,財(cái)務(wù)總監(jiān)夏蘭田,董秘郭曉雷,通過上交所集中競(jìng)價(jià)交...
2022年3月15日,這是繼1983年以來的第40個(gè)國際消費(fèi)者權(quán)益日。中消協(xié)組織圍繞共促消費(fèi)公平消費(fèi)維權(quán)年主題...
首批金控牌照的歸屬出爐,兩家公司拿到許可證。3月17日,央行發(fā)布公告稱,已批準(zhǔn)中國中信金融控股有限公...
時(shí)隔半月之久,西寧市城北區(qū)逐步推動(dòng)復(fù)工復(fù)產(chǎn),往日的生機(jī)活力被漸漸尋回,牛肉面紅油飄香、包子鋪炊煙...
音樂是我生活的一部分,是我的夢(mèng)想,也是我的事業(yè)。英國音樂人亞當(dāng)(Adam)告訴記者,在中國的十幾年里,...
Copyright © 2015-2022 太平洋舞蹈網(wǎng)版權(quán)所有 備案號(hào):豫ICP備2022016495號(hào)-17 聯(lián)系郵箱:93 96 74 66 9@qq.com