Eureka服务发现协议允许使用Eureka Rest API
检索出Prometheus需要监控的targets,Prometheus会定时周期性的从Eureka调用Eureka Rest API
,并将每个应用实例创建出一个target。
Eureka服务发现协议支持对如下元标签进行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 #刷新间隔,默认30s
eureka_sd_configs
官网支持主要配置如下:
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
服务发现协议核心逻辑都封装在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拉取元数据: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下每个instance部分转成target targets := targetsForApp(&app) //解析的采集点合入一起 tg.Targets = append(tg.Targets, targets...) } return []*targetgroup.Group{tg}, nil}
refresh
方法主要有两个流程:
1、fetchApps()
:从eureka-server
的/eureka/apps
接口拉取注册服务信息;
2、targetsForApp()
:遍历app
下instance
,将每个instance
解析出一个target
,并添加一堆元标签数据。
如下示例从eureka-server
的/eureka/apps
接口拉取的注册服务信息:
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
信息会被解析成采集点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则默认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_]字符,其它非法字符都会被替换成下划线_ ln := strutil.SanitizeLabelName(m.XMLName.Local) target[model.LabelName(appInstanceMetadataPrefix+ln)] = lv(m.Content) } } targets = append(targets, target) } return targets}
解析比较简单,就不再分析,解析后的标签数据如下图:
标签中有两个特别说明下:
1、__address__
:这个取值instance.hostname
和port
(默认80),所以要注意注册到eureka
上的hostname
准确性,不然可能无法抓取;
2、metadata-map
数据会被转成__meta_eureka_app_instance_metadata_
格式标签,prometheus
进行relabeling
一般操作metadata-map
,可以自定义metric_path
、抓取端口等;
3、prometheus
的label
只支持[a-zA-Z0-9_]
,其它非法字符都会被转换成下划线,具体参加:strutil.SanitizeLabelName(m.XMLName.Local)
;但是eureka
的metadata-map
标签含有下划线时,注册到eureka-server
上变成双下划线,如下配置:
eureka: instance: metadata-map: scrape_enable: true scrape.port: 8080
通过/eureka/apps
获取如下:
基于Eureka
服务发现原理如下图:
基于eureka_sd_configs
服务发现协议配置创建Discoverer
,并通过协程运行Discoverer.Run
方法,Eureka
服务发现核心逻辑封装discovery/eureka.go
的func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error)
方法中。
refresh
方法中主要调用两个方法:
1、fetchApps
:定时周期从Eureka Server
的/eureka/apps
接口拉取注册上来的服务元数据信息;
2、targetsForApp
:解析上步骤拉取的元数据信息,遍历app
下的instance
,将每个instance
解析成target
,并将其它元数据信息转换成target
元标签可以用于relabel_configs
操作
Eureka服务发现协议允许使用EurekaRestAPI检索出Prometheus需要监控...
截至2023年4月21日收盘,粤电力B(200539)报收于2 18元,下跌0 46...
妥善储存橙子和橘子,以确保最佳风味。尽管有保护性的果皮,但橙子...
外交部发言人汪文斌20日表示,当前国际金融风险突出,同美国等发达...
昨日拍摄的金沙街道老旧小区希望苑改造后的新貌。通州高度重视城镇...
4月10日晚,经过近3个小时的飞行,陈涛落地距离北京1800多公里之外...
温度记|多地破30℃!广东漫长夏天热情洋溢地来了!
丰田最出名的混合动力系统叫做THS,最近随着16代皇冠的公开又多出一...
今年以来,南阳市镇平县郭庄乡将“人人持证、技能河南”工作作为助...
扫码查看详情淄博新闻网讯(全媒体记者常旭)群众可通过哪些途径参...
泰安日报社·最泰安讯 4月18日,“外国留学生‘感知泰安’”活动走...
温榆河公园池塘中成片的芦苇成了水鸟繁殖的天堂,成群的水鸟在这里...
IPP评论是国家高端智库华南理工大学公共政策研究院(IPP)官方微信...
今天小编肥嘟来为大家解答以上的问题。兔子可以洗澡吗会死吗,兔子...
原标题:一年春好处最是读书时打造“书香夏都”品牌,培育发展新型...
本报枣庄讯4月16日,长篇小说《东进》新书发布会在枣庄市山亭区举行...
在欧冠1 4决赛中,本菲卡两回合总比分3-5负于国米,无缘四强。赛后...
广生堂10月9日公告,公司创新药控股子公司广生中霖口服小分子广谱抗...
根据数据提供商ORTEX的计算,截至周三,对冲基金对多伦多道明银行的...
4月21日,生意社槽钢基准价为4043 33元 吨,与本月初(4120 00元...
德转盘点了近15个赛季欧冠金靴奖得主,其中C罗6次当选,梅西则5次登...
郑恺前女友程晓玥疑似已分娩,手腕上戴着手环,腹部平坦自称产妇,郑...
4月16日,由太古地产、成都远洋太古里、电子科技大学MBA联合会共同...
你们好,最近小活发现有诸多的小伙伴们对于肉末茄子怎么做好吃又简...
打疯了!半场狂轰81分全场25记三分灭热火!残阵雄鹿露出夺冠獠牙,热...
北京商报讯(记者马换换)深交所官网显示,美智光电科技股份有限公...
“书卷多情似故人,晨昏忧乐每相亲”,开篇即饱含深情地写出于谦对...
中汇集团公布,公司将于2023年4月26日(下周三)举行董事会会议。
新华社北京4月20日电(记者张辛欣、王聿昊)工信部总工程师赵志国20...
为了让“特殊”孩子拥有和健全孩子一样的学习机会,江苏省连云港市...