您所在的位置:首页 >宏观 >
【云原生 • Prometheus】Prometheus 注册中心Eureka服务发现原理-世界快资讯

时间:2023-04-21 16:30:18    来源:腾讯云

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操作

标签:
【云原生 • Prometheus】Prometheus 注册中心Eureka服务发现原理-世界快资讯

【云原生 • Prometheus】Prometheus 注册中心Eureka服务发现原理-世界快资讯

Eureka服务发现协议允许使用EurekaRestAPI检索出Prometheus需要监控的targets,Prometheus会定时周期性的从Eurek

安徽全椒经济开发区变更主导产业获省政府批复

安徽全椒经济开发区变更主导产业获省政府批复

安徽全椒经济开发区变更主导产业获省政府批复

世界百事通!讲好中国故事、传播铁路文化 老藏品、老物件见证中国铁路发展

世界百事通!讲好中国故事、传播铁路文化 老藏品、老物件见证中国铁路发展

纪念宝成铁路通车运营65周年暨2023第二届中国铁路文化收藏集邮展于4月15日至18日在宝鸡机车检修厂举行。赵

如何为燃气烤架制作防风罩

如何为燃气烤架制作防风罩

你需要的东西一张2x2金属板-在五金店切割钳子或钳子铝箔金属卡夹(2-3)烧烤时,挡风板可使火焰保持活力。燃气

不交钱就别享受?Twitter删除未付费用户蓝色认证标签|今亮点

不交钱就别享受?Twitter删除未付费用户蓝色认证标签|今亮点

新浪科技讯北京时间4月21日早间消息,据报道,当地时间周四,Twitter正式删除了非付费会员的蓝色认证标签,

通讯Plus·早报|乐山点亮首个8K+裸眼3D大屏 中国移动成A股市值最大公司 世界短讯

通讯Plus·早报|乐山点亮首个8K+裸眼3D大屏 中国移动成A股市值最大公司 世界短讯

中国移动成A股市值最大公司4月17日,中国移动A股(600941)股价涨幅近5%,盘间一度达到103元 股,总市值最高触

贵澳深度合作南明核心示范区项目在贵阳集中签约

贵澳深度合作南明核心示范区项目在贵阳集中签约

贵澳深度合作南明核心示范区项目在贵阳集中签约

环球简讯:快讯2023-04-20 13:15:43

环球简讯:快讯2023-04-20 13:15:43

4月20日电,长鑫存储寻求以超过145亿美元的估值进行IPO。

环球今头条!《声生不息宝岛季》三公:莫文蔚排面足,4大唱将终于独唱,张韶涵唱哭观众

环球今头条!《声生不息宝岛季》三公:莫文蔚排面足,4大唱将终于独唱,张韶涵唱哭观众

《声生不息宝岛季》三公:莫文蔚排面足,4大唱将终于独唱,张韶涵唱哭观众

焦点热文:全省首座双层跨海大桥正式通车 上层为机动车道,下层为非机动车道

焦点热文:全省首座双层跨海大桥正式通车 上层为机动车道,下层为非机动车道

全省首座双层跨海大桥正式通车上层为机动车道,下层为非机动车道

原材料价格上涨 明泰铝业2022年净利润同比减少13.68% 今日热议

原材料价格上涨 明泰铝业2022年净利润同比减少13.68% 今日热议

4月20日晚间,明泰铝业披露2022年业绩报告,该公司2022年实现营收277 81亿元,同比增长12 87%,刷新历史最

即时:高速切削刀具_关于高速切削刀具简述

即时:高速切削刀具_关于高速切削刀具简述

小伙伴们,你们好,今天小夏来聊聊一篇关于高速切削刀具,关于高速切削刀具简述的文章,网友们对这件事情都

【快播报】欢乐“三月三”

【快播报】欢乐“三月三”

4月20日,孩子们在广西南宁市逸夫小学准备品尝美食。当日,广西南宁市逸夫小学举行2023“壮族三月三”暨...

天天观热点:火热!“五一”假期铁路客票销售创同期历史新高

天天观热点:火热!“五一”假期铁路客票销售创同期历史新高

央视网消息:按照国铁集团车票预售的相关规定,4月19日开始预售5月3日,也就是“五一”假期最后一天的车...

华瓷股份2022年度实现归母净利润约1.71亿元,同比增长24.64% 环球观焦点

华瓷股份2022年度实现归母净利润约1.71亿元,同比增长24.64% 环球观焦点

4月17日,华瓷股份发布2022年年报。报告显示,2022年度,公司实现营业收入约13 8亿元,同比增长14 63%;实

环球今日报丨exercise的用法_语法搭配

环球今日报丨exercise的用法_语法搭配

欢迎观看本篇文章,小勉来为大家解答以上问题。exercise的用法,语法搭配很多人还不知道,现在让我们一起来

物竞天择意思相近_物竞天择的意思

物竞天择意思相近_物竞天择的意思

1、题名]:物竞天择[拼音]:wùjìntiānzé[解释]:物竞:生物的生存竞争;天择:自然选择。2、生物相

地矿五院召开泰山区域危岩体失稳破坏演化机制与减灾技术研讨会

地矿五院召开泰山区域危岩体失稳破坏演化机制与减灾技术研讨会

通讯员耿怀庆为推动泰山区域危岩体研究工作,近日,地矿五院举行泰山区域危岩体失稳破坏演化机制与减灾关键

药企鏖战PD-1:信达生物销售额大幅下滑,康方、君实闯荡东南亚_环球播资讯

药企鏖战PD-1:信达生物销售额大幅下滑,康方、君实闯荡东南亚_环球播资讯

药企鏖战PD-1:信达生物销售额大幅下滑,康方、君实闯荡东南亚

全国春播粮食进度已近两成|世界微动态

全国春播粮食进度已近两成|世界微动态

国新办20日举行新闻发布会,介绍一季度农业农村经济运行情况。农业农村部总农艺师、发展规划司司长曾衍德表

湖北省首条准自由流收费站通过验收

湖北省首条准自由流收费站通过验收

湖北省首条准自由流收费站通过验收---4月20日,湖北省鄂州花湖机场收费站准自由流收费系统顺利通过验收

焦点消息!直击2023上海车展:超150款新车全球首发,新能源车型占比近三分之二

焦点消息!直击2023上海车展:超150款新车全球首发,新能源车型占比近三分之二

直击2023上海车展:超150款新车全球首发,新能源车型占比近三分之二,新车,腾势,概念车,上海市,上海车展,phe

【世界独家】IBM 发布 2023 年第一季度业绩报告:软件和咨询业务引领增长,利润表现强劲

【世界独家】IBM 发布 2023 年第一季度业绩报告:软件和咨询业务引领增长,利润表现强劲

北京2023年4月20日 美通社 近日,IBM(NYSE:IBM)发布2023年第一季度业绩报告。IBM董

法拉利车标_法拉利911 环球微资讯

法拉利车标_法拉利911 环球微资讯

1、法拉利还有911?记错了吧,那是保时捷911吧,。2、好像是不限量的。本文到此分享完毕,希望对大家有所帮

90后大学生组成“学生卖淫团”,20元至30元不等的红包作为中介费

90后大学生组成“学生卖淫团”,20元至30元不等的红包作为中介费

90后大学生组成“学生卖淫团”,20元至30元不等的红包作为中介费,卖淫,林琳,红包,大学生,中介费,90后,性教育

广告

X 关闭

广告

X 关闭