世界快看点丨【云原生 • Prometheus】Prometheus 注册中心Eureka服务发现原理
Prometheus 注册中心Eureka服务发现原理
概述
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协议实现
基于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
操作
关键词:
责任编辑:meirong
-
世界快看点丨【云原生 • Prometheus】Prometheus 注册中心Eureka服务发现原理
-
焦点热门:如何录制视频讲解配音_如何录制视频
-
微头条丨雷霆神奇上尉官网在哪下载 最新官方下载安装地址
-
【天天聚看点】孔德胜
-
世界热议:元始天尊的徒弟排名顺序_元始天尊的徒弟排名
-
世界新资讯:春游吃零食的片段作文怎么写_片段作文怎么写
-
当前信息:兰州高中学校招生计划(最新)
-
今日讯!厦门丧葬金和抚恤金线上申请须知
-
最新:4月21日上海泰邦天然橡胶报价平稳
-
每日聚焦:深圳公租房能住一辈子吗
-
环球新资讯:科创信息:公司业务已覆盖二十多省份和地区服务超过数千家政府机关
-
环球快资讯:浅谈培养幼儿合作能力
-
环球新消息丨3元低价股票一览表 3元低价股票一览
-
天天速递!读书越多越幸福:当当网联合易观发布《中国年轻人阅读洞察2023》
-
全球滚动:股票行情快报:泉为科技(300716)4月21日主力资金净卖出67.61万元
-
全球简讯:国内燃煤电厂最大电化学储能项目投运
-
焦点播报:立春前可以洗澡吗?
-
天天日报丨今日黄大仙_一句话赢大钱
-
环球时讯:濮阳惠成(300481):4月21日北向资金减持19.17万股
-
要闻速递:南天信息(000948):4月21日北向资金增持56.28万股
-
百事通!发现市州最美图书馆·宜宾丨一个有气质一个有颜值,新老“书房”两相宜
-
每日消息!斯派莎克SV604安全阀_关于斯派莎克SV604安全阀简介
-
环球观天下!抖店无货源选品软件哪种好用?附详细说明
-
世界通讯!助企纾困解难题 暖心服务促发展 信阳市新县城市管理局组织开展“暖心助企”系列活动
-
全球球精选!9.2,没有8090能拒绝今天的告别
-
全球微动态丨股票行情快报:有棵树(300209)4月21日主力资金净卖出635.56万元
-
全球新动态:财政部公开选聘第四届企业会计准则咨询委员会委员
-
环球精选!小刀电动车展现超强动力!航天动力系实力挑战魔鬼弯道!
-
视点!地热清洗合同范本(精选31篇)
-
当前快播:procreate绘画如何填充一块颜色?procreate绘画如何复制粘贴?
-
时讯:【贵州最美图书馆③】凯里学院图书馆:绽放黔东南少数民族文化之美
-
环球今热点:矮壮素的使用方法和用量大豆_矮壮素的使用方法
-
天天微头条丨奥飞娱乐(002292):4月21日14时20分触及跌停板
-
【天天快播报】中信建投期货4月21日能源日报:欧美经济数据引发衰退担忧,关注跳空缺口附近的技术性支撑
-
【全球时快讯】金秀贤的料 金秀贤吧百度贴吧