当前位置:首页  >  行业资讯  > 正文

今日热搜:【云原生 • Prometheus】Prometheus 注册中心Eureka服务发现原理

今日热搜:【云原生 • Prometheus】Prometheus 注册中心Eureka服务发现原理
2023-04-22 03:24:56 来源:腾讯云

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 metadata

eureka_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.gofunc (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():遍历appinstance,将每个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.hostnameport(默认80),所以要注意注册到eureka上的hostname准确性,不然可能无法抓取;

2、metadata-map数据会被转成__meta_eureka_app_instance_metadata_格式标签,prometheus进行relabeling一般操作metadata-map,可以自定义metric_path、抓取端口等;

3、prometheuslabel只支持[a-zA-Z0-9_],其它非法字符都会被转换成下划线,具体参加:strutil.SanitizeLabelName(m.XMLName.Local);但是eurekametadata-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.gofunc (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操作

标签:

(责任编辑:news01)
今日热搜:【云原生 • Prometheus】Prometheus 注册中心Eureka服务发现原理

今日热搜:【云原生 • Prometheus】Prometheus 注册中心Eureka服务发现原理

Eureka服务发现协议允许使用EurekaRestAPI检索出Prometheus需要监控的targets,Prometheus会定时周期性的从Eurek
04-22 03:24:56
【快播报】澳超前瞻:西悉尼主场战惠灵顿   两队皆需力保附加赛席位

【快播报】澳超前瞻:西悉尼主场战惠灵顿 两队皆需力保附加赛席位

北京时间4月21日17:45,澳超联赛第25轮,西悉尼流浪者主场迎战惠灵顿凤凰。
04-22 03:26:48
全球速看:骑迹无限!首届中国公路自行车职业联赛首站比赛在济源举办

全球速看:骑迹无限!首届中国公路自行车职业联赛首站比赛在济源举办

4月21日,2023首届中国公路自行车职业联赛首站比赛开幕式在济源文化城西广场举行。国家体育总局竞技体育...
04-22 03:02:45
环球视点!4月21日基金净值:富国品质生活混合A最新净值1.8294,跌0.44%

环球视点!4月21日基金净值:富国品质生活混合A最新净值1.8294,跌0.44%

4月21日,富国品质生活混合A最新单位净值为1 8294元,累计净值为1 8294元,较前一交易日下跌0 44%。...
04-22 02:35:06
当前要闻:恶魔城月下夜想曲最强装备攻略_恶魔城月下夜想曲最强装备

当前要闻:恶魔城月下夜想曲最强装备攻略_恶魔城月下夜想曲最强装备

1、正城最好的剑是妖刀村正,可以吸血的,还可以不断增加自身的攻击力。2、逆城最好的剑应该说是无限真...
04-22 02:07:29
全球快资讯:中国民航中性电子运单系统在贵阳机场上线运行

全球快资讯:中国民航中性电子运单系统在贵阳机场上线运行

4月20日,中国民航中性电子运单系统(DigitalAirWaybillPlatform,以下简称“DAP”)在贵阳机场华夏航空...
04-22 01:58:38
环球快讯:中原证券:一季度归母净利润7049.73万元,同比增长282.45%

环球快讯:中原证券:一季度归母净利润7049.73万元,同比增长282.45%

财经网讯4月21日,中原证券公布的2023年第一季度报告显示,公司报告期实现营业收入4 64亿元,同比增长1...
04-22 01:08:19
当前动态:斗破苍穹3寻找异火

当前动态:斗破苍穹3寻找异火

1、斗破伴随我半涯意登陆起点看斗破简介我并没意起初意点我看纳兰嫣退婚我便毫犹豫斗破载我看萧炎三云岚...
04-22 01:07:29
全球头条:国际贸易流仍相对偏紧 郑糖跟随外盘波动

全球头条:国际贸易流仍相对偏紧 郑糖跟随外盘波动

【行情分析】在国际供需矛盾尚未解决前原糖仍维持相对偏强走势。泰国产量增幅不及预期,另外,市场正在...
04-22 00:42:17
【焦点热闻】中建环能:4月21日召开董事会会议

【焦点热闻】中建环能:4月21日召开董事会会议

中建环能(SZ300425,收盘价:4 9元)4月21日晚间发布公告称,公司第五届第七次董事会会议于2023年4月2...
04-22 00:45:22
环球快讯:word文档身份证号码转换电子表格(word文档身份证尺寸)

环球快讯:word文档身份证号码转换电子表格(word文档身份证尺寸)

1、身份证尺寸大小:高度5 4厘米、宽度8 56厘米。2、Word文档页面尺寸采用A4,常用的办公纸张大小。3...
04-22 00:24:44
天天看热讯:国产豪华轿车新选择!红旗H6售19.28-23.98万元

天天看热讯:国产豪华轿车新选择!红旗H6售19.28-23.98万元

国产豪华轿车新选择!红旗H6售19 28-23 98万元
04-21 23:37:14
简讯:招商证券副总裁、董事会秘书吴慧峰辞职

简讯:招商证券副总裁、董事会秘书吴慧峰辞职

中国经济网北京4月21日讯今日,招商证券(600999 SH)发布关于公司副总裁、董事会秘书辞职暨变更信息披露...
04-21 22:58:59
世界播报:希腊神话尼古拉斯_希腊队尼古拉迪斯

世界播报:希腊神话尼古拉斯_希腊队尼古拉迪斯

1、这是希腊队参加2004年欧洲杯球员名单 门将1尼科波利迪斯Nikopolidis71 10 14187c
04-21 22:43:37
热推荐:以金融创新扩大消费刚需,浦发银行多场景助力消费市场回暖

热推荐:以金融创新扩大消费刚需,浦发银行多场景助力消费市场回暖

2023年的春天,万物复苏、百花盛放的美景动人,作为国际消费中心城市之一的上海,消费市场复苏回暖的形...
04-21 22:12:03
环球即时:商务部:一季度我国网络零售市场规模总体稳步增长

环球即时:商务部:一季度我国网络零售市场规模总体稳步增长

商务部网站4月21日消息,商务部电子商务司负责人介绍2023年一季度网络零售市场发展情况。2023年一季度,...
04-21 21:53:46
今日播报!pr更新插件是否需要重新安装_pr更新

今日播报!pr更新插件是否需要重新安装_pr更新

1、不知道你用的什么版本?应该是完全同步更新的!此外,你的“更新”是指的怎么样的更新?2、是用另一...
04-21 21:42:37
【环球时快讯】银之杰4月21日盘中跌幅达5%

【环球时快讯】银之杰4月21日盘中跌幅达5%

以下是银之杰在北京时间4月21日10:22分盘口异动快照:4月21日,银之杰盘中跌幅达5%,截至10点22分,报11...
04-21 21:20:52
【世界报资讯】合资汽车企业进入优势融合新阶段

【世界报资讯】合资汽车企业进入优势融合新阶段

“拥抱汽车行业新时代”是正在举行的第二十届上海国际汽车工业展览会的主题。随着新能源、智能化等技术...
04-21 20:57:02
焦点信息:央行:住户部门消费和投资意愿正在回升

焦点信息:央行:住户部门消费和投资意愿正在回升

央行:住户部门消费和投资意愿正在回升---人民网北京4月21日电(记者杜燕飞)“当前住户存款增加较多,...
04-21 20:31:23
每日快播:伦纳德上场时快船胜率63.5% 缺席时快船胜率36.7%

每日快播:伦纳德上场时快船胜率63.5% 缺席时快船胜率36.7%

今日快船官方宣布,球队前锋科怀-伦纳德因右膝扭伤将缺席首轮第三场比赛。本赛季至今,当伦纳德上场时,...
04-21 20:25:20
快看点丨深圳男篮直接晋级半决赛  具体怎么回事?

快看点丨深圳男篮直接晋级半决赛 具体怎么回事?

直播吧4月19日讯CBA季后赛8进4,浙江2-0直接拿下广州挺进四强!这样一来,CBA半决赛的第一组对阵也直接出炉:浙
04-21 19:56:41
世界头条:太“秃”然!大草坪被挖成这样,2万居民:无法接受

世界头条:太“秃”然!大草坪被挖成这样,2万居民:无法接受

近日,潮新闻记者帮接到了一则真情实感的求助,求助人是良渚文化村绿野花语苑的业主,他想让记者帮来看...
04-21 19:37:09
世界快看:假阴线选股公式源码_假阴线

世界快看:假阴线选股公式源码_假阴线

1、高开低走,收盘价位比昨天收盘价高,K线上就形成阴线,但事实上价格比昨天高,所以称假阴线。2、作用...
04-21 19:33:46
环球微资讯!扁豆馅的月饼什么梗_月饼有多少种都有什么馅

环球微资讯!扁豆馅的月饼什么梗_月饼有多少种都有什么馅

1、月饼的品种已异彩纷呈。2、我国月饼品种繁多,按产地分有:京式月饼、广式月饼、苏式月饼、台式月饼...
04-21 18:29:20
每日速递:满30减20?“俯冲式”降温预警!

每日速递:满30减20?“俯冲式”降温预警!

满30减20?“俯冲式”降温预警!
04-21 18:29:23
天天观察:农发行鼎城区支行:奋力抓“早”抓“专” 提升支农履职质效

天天观察:农发行鼎城区支行:奋力抓“早”抓“专” 提升支农履职质效

华声在线讯(通讯员:赵亮)农发行鼎城区支行把认真宣传贯彻好党的二十大精神作为当前重要政治任务,将学习...
04-21 18:15:03
天天观点:首搭宁德时代4C麒麟电池!理想汽车下场造纯电车,蔚小理正面交锋

天天观点:首搭宁德时代4C麒麟电池!理想汽车下场造纯电车,蔚小理正面交锋

4月18日,2023第二十届上海国际汽车工业展览会正式开幕。本次车展期间,新能源汽车产品“大唱主角”,蔚...
04-21 17:03:48
世界时讯:2023年残疾人两项补贴年度资格认定工作开始 广东户籍残疾人6月20日前申报

世界时讯:2023年残疾人两项补贴年度资格认定工作开始 广东户籍残疾人6月20日前申报

南方日报讯(记者 钱明雅吴晓娴通讯员 莫冠婷)笔者近日从广东省民政厅了解到,2023年残疾人两项补贴...
04-21 17:14:51
实时:北京市老龄协会:消费欺诈是老年人遇到最为频繁的侵权类型

实时:北京市老龄协会:消费欺诈是老年人遇到最为频繁的侵权类型

北京市老龄协会:消费欺诈是老年人遇到最为频繁的侵权类型
04-21 16:52:38

精彩推送