您的位置: 首页> 资讯 > 正文

【prometheus】-04 轻松搞定Prometheus Eureka服务发现|全球快报

2023-03-23 23:19:10 来源:腾讯云

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配置可选项如下:


【资料图】

# The URL to connect to the Eureka server.server: # Sets the `Authorization` header on every request with the# configured username and password.# password and password_file are mutually exclusive.basic_auth:  [ username:  ]  [ password:  ]  [ password_file:  ]# Optional `Authorization` header configuration.authorization:  # Sets the authentication type.  [ type:  | default: Bearer ]  # Sets the credentials. It is mutually exclusive with  # `credentials_file`.  [ credentials:  ]  # Sets the credentials to the credentials read from the configured file.  # It is mutually exclusive with `credentials`.  [ credentials_file:  ]# Optional OAuth 2.0 configuration.# Cannot be used at the same time as basic_auth or authorization.oauth2:  [  ]# 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 ]

协议分析

通过前面分析的Prometheus服务发现原理以及基于文件方式服务发现协议实现的分析,Eureka服务发现大致原理如下图:

通过解析配置中eureka_sd_configs协议的job生成Config,然后NewDiscovery方法创建出对应的Discoverer,最后调用Discoverer.Run()方法启动服务发现targets。

1、基于文件服务发现配置解析

假如我们定义如下job:

- job_name: "eureka"  eureka_sd_configs:    - server: http://localhost:8761/eureka    

会被解析成eureka.SDConfig如下:

eureka.SDConfig定义如下:

type SDConfig struct {    // eureka-server地址 Server           string                  `yaml:"server,omitempty"`    // http请求client配置,如:认证信息 HTTPClientConfig config.HTTPClientConfig `yaml:",inline"`    // 周期刷新间隔,默认30s RefreshInterval  model.Duration          `yaml:"refresh_interval,omitempty"`}

2、Discovery创建

func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) { rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "eureka_sd", config.WithHTTP2Disabled()) if err != nil {  return nil, err } d := &Discovery{  client: &http.Client{Transport: rt},  server: conf.Server, } d.Discovery = refresh.NewDiscovery(  logger,  "eureka",  time.Duration(conf.RefreshInterval),  d.refresh, ) return d, nil}

3、Discovery创建完成,最后会调用Discovery.Run()启动服务发现:

和上一节分析的服务发现之File机制类似,执行Run方法时会执行tgs, err := d.refresh(ctx),然后创建定时周期触发器,不停执行tgs, err := d.refresh(ctx),将返回的targets结果信息通过channel传递出去。

4、上面Run方法核心是调用d.refresh(ctx)逻辑获取targets,基于Eureka发现协议主要实现逻辑就在这里:

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            

5、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、prometheuslabel只支持[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方式的服务原理如下图:

大概说明:Discoverer启动后定时周期触发从eureka server/eureka/apps接口拉取注册服务元数据,然后通过targetsForApp遍历app下的instance,将每个instance解析成target,并将其它元数据信息转换成target原标签可以用于target抓取前relabeling操作。

关键词:

资讯
业界
企业
骑闻
产品
【prometheus】-04 轻松搞定Prometheus Eureka服务发现|全球快报
Eureka服务发现协议允许使用EurekaRestAPI检索出Prometheus需要监控的targets,Prometheus会定时周期性的从Eurek
2023-03-23
世界消息!天微电子(688511.SH):拟使用不超2亿元闲置自有资金进行现金管理
天微电子(688511 SH):拟使用不超2亿元闲置自有资金进行现金管理,天微电子,自有资金,现金管理
2023-03-23
天天观点:not yet的用法
“notyet”意思是:还没有,还没。短语用法:1、Butnotjustyet但不是现在,但不现在。2、Notdeadyet独立流行。3、StillNotAwa
2023-03-23
【天天速看料】南部战区新闻发言人就美舰擅闯中国西沙领海发表谈话
据南部战区微信公众号消息,南部战区新闻发言人田军里空军大校表示,3月23日,美“米利厄斯”号导弹驱逐舰未经中国政府批准,
2023-03-23
锐趣文化总经理陈敏:电商行业如逆水行舟 不进则退
3月22日,由网经社主办的《2022年度中国直播电商市场数据报告》发布会暨直播电商圆桌会在网经社总部网盛大厦召开。本次大会,快手、谦寻、交个
2023-03-23
2022重庆涪陵区事业单位招聘西藏籍高校毕业生拟聘人员公示
根据《重庆市事业单位公开招聘人员实施办法》(渝人发〔2006〕44号)等规定,按照藏东人社2022年9月7日发布的《20
2023-03-23
  中新网海口1月23日电(符宇群)海南省第六届人民代表大会第五次会议23日在海口举行第二次全体会议。海南省人民检察院检察长张毅在作报告
2022-01-24
  中新网太原1月23日电 (高雨晴 冉涌 张鹏宇)记者23日从国网山西省电力公司获悉,该公司冬奥保电应急发电队伍已到达河北张家口赛区,
2022-01-24
  中新网西宁1月23日电 (记者 李江宁)据青海省地震局23日消息,中国地震台网正式测定,北京时间2022年1月23日10时21分,在青海海西州德
2022-01-24
  中新网贵阳1月23日电 (周燕玲)对外开放,是内陆开放型经济试验区贵州正在召开的两会热点词汇,如何拓展海内外“朋友圈”助力贵州经济
2022-01-24
温馨生活好young 厦门推广文旅产品火爆全网
  中新网厦门1月23日电 (记者 杨伏山)“冬日暖阳厦门好young”福建省内宣传推广线下活动22日精彩收官。主办方称,本次活动火爆全网及福
2022-01-24
宁夏非遗传承人:刻刀里的守正创新更有“年味”
  (新春见闻)宁夏非遗传承人:刻刀里的守正创新更有“年味”  中新网银川1月23日电 题:宁夏非遗传承人:刻刀里的守正创新更有“年味
2022-01-24
沧州:8个重大科技专项项目确定 引领经济社会高质量发展
为充分发挥科技在经济社会高质量发展中的引领和支撑作用,沧州市确定8个项目为2021年全市重大科技专项项目。这8个重大科技专项项目分别为:
2022-03-19
  中新网海口1月23日电(王子谦 符宇群)海南省高级人民法院院长陈凤超23日说,2021年海南法院为自贸港建设提供坚强司法保障,全年有效管
2022-01-24
  新华社武汉1月23日电(记者王贤)随着春节假期临近,从广州、深圳等地返回湖北的旅客较多。为此,23日,武汉站、汉口站、襄阳东站、十堰
2022-01-24
  1月22日0—24时,广东省新增本土确诊病例3例和本土无症状感染者1例,均为珠海报告。23日,珠海市疫情防控新闻发布会上,珠海市政府副秘
2022-01-24
青海海西州德令哈市发生3.7级地震
  据中国地震台网正式测定,1月23日11时58分在青海海西州德令哈市发生3 7级地震,震源深度9千米,震中位于北纬38 40度,东经97 35度。
2022-01-24
  北京2022年冬奥会和冬残奥会颁奖花束已于近期完成交付。与传统的鲜切花不同,这些花束全部采用上海市非物质文化遗产“海派绒线编结技艺
2022-01-24
深圳本地股板块12月20日跌1.68%,特发服务领跌,主力资金净流出8.98亿元
12月20日深圳本地股板块较上一交易日下跌1 68%,特发服务领跌。当日上证指数报收于3073 77,下跌1 07%。深证成指报收于10949 12,下跌1 5
2022-12-21
旭辉控股集团:配股募资逾9亿港元 今年已进行3次股本融资约31亿港元-热资讯
中国网是国务院新闻办公室领导,中国外文出版发行事业局管理的国家重点新闻网站。本网通过10个语种11个文版,24小时对外发布信息,是中国进行
2022-12-20
11月份内蒙古煤炭价格环比下降2.3%
  2022年11月份受疫情防控、原油价格变化,农业生产进程和建筑工程进展等因素影响,煤炭价格环比下降。据内蒙古自治区商务厅生产资料市场监
2022-12-20
每日头条!奥园美谷: 北京市万商天勤律师事务所关于奥园美谷科技股份有限公司2022年第四次临时股东大会的法律意见书
北京 BEIJING         北京市朝阳区建国门外大街 8 号   北京国际财源中心 A 座 32 层 100022    menwai Avenue 
2022-12-19
大豪科技董秘回复:期间利润的归属请见重组报告书
大豪科技(603025)12月19日在投资者关系平台上答复了投资者关心的问题。投资者:重组中止期间,标的公司经营所得利润是否归大豪科技所有?大豪
2022-12-19
中电华大科技(00085.HK)早盘涨超24%,截至发稿,涨21.84%,报1.06港元,成交额413.05万港元_环球快资讯
中电华大科技(00085 HK)早盘涨超24%,截至发稿,涨21 84%,报1 06港元,成交额413 05万港元。截至2022年12月16日收盘,中电华大科技
2022-12-19
  中新网西宁1月23日电 (记者 孙睿)据青海省地震台网测定,2022年1月23日10点21分(北京时间)在青海省海西州德令哈市(北纬38 44度,东经
2022-01-24
江西南昌:市民赏年画迎新年 书法家挥毫送春联
  (新春见闻)江西南昌:市民赏年画迎新年 书法家挥毫送春联  1月23日,“赏年画过大年”新年画作品联展江西南昌站活动在江西省文化馆
2022-01-24
  中新网成都1月23日电 (祝欢)成都市第十七届人民代表大会第六次会议23日在成都举行,成都市中级人民法院院长郭彦与成都市人民检察院检
2022-01-24
列车临时停车3分钟救旅客
  (新春见闻)列车临时停车3分钟救旅客  中新网广州1月23日电 (郭军 黄伟伟)“车长,车长,4号车厢有位旅客腹涨难忍,身体不舒服”…
2022-01-24
女子背负命案潜逃24年 因涉疫人员核查落网
  中新网湖州1月23日电(施紫楠 徐盛煜 赵学良)1998年7月,犯罪嫌疑人杜某因家庭琐事,用菜刀将自己的弟媳砍伤致死。案发后,她从老家河
2022-01-24
广东“00后”雄狮少年锤炼功夫迎新春
  (新春见闻)广东“00后”雄狮少年锤炼功夫迎新春  中新社广州1月23日电 题:广东“00后”雄狮少年锤炼功夫迎新春  作者 孙秋霞 
2022-01-24
05-20 中国旅游日!河北创新发展“旅游+” 打造“京畿福地 乐享河北”品牌
中国旅游日!河北创新发展“旅游+” 打造“京畿福地 乐享河北”品牌
5月19日是第12个中国旅游日。今年中国旅游日主题为感悟中华文化,享受美好旅程。河北内环京津,外环渤海,携太行燕山之威,挟畿辅拱卫之要 [详细]
05-20 西湖游船“国潮宋风”特色夜游上线 体验穿越宋风豪华画舫之旅
西湖游船“国潮宋风”特色夜游上线 体验穿越宋风豪华画舫之旅
西湖之胜,晴湖不如雨湖,雨湖不如夜西湖。想要一边沐着晚风,一边乘坐游船感受西湖夜色的别样美景吗?那就来体验一把穿越宋风的豪华画舫之 [详细]
05-20 杭州:露营成今年最大热点 “城市露营”模式受年轻人追捧
杭州:露营成今年最大热点 “城市露营”模式受年轻人追捧
一顶帐篷、一副桌椅、一张地垫,寻个湖畔草坪,就能体验时下最热的旅行方式——露营。五一假期,杭州灵山景区铜鉴湖九曲梅韵和海棠融春两块 [详细]
03-19 2022岳阳国际旅游节开幕 特色农产品展销等系列活动目不暇接
2022岳阳国际旅游节开幕 特色农产品展销等系列活动目不暇接
今天,天下洞庭岳阳市君山区第九届良心堡油菜花节暨2022岳阳国际旅游节开幕,菊红、粉红、水红、桃红、紫色、白色等七色组成的4万亩花海在 [详细]
03-19 2022年郴州计划重点推进文旅项目101个 总投资354亿元
2022年郴州计划重点推进文旅项目101个 总投资354亿元
3月16日,我市举行全市文旅项目和城市大提质大融城项目集中开工仪式,市委书记吴巨培宣布项目开工。郴州嘉合欢乐世界、仙福路工地清风徐来 [详细]
03-19 宿州泗县深入推进文旅融合发展 擦亮城市品牌
宿州泗县深入推进文旅融合发展 擦亮城市品牌
近年来,泗县以争创安徽省文化旅游名县为目标,深入推进文旅融合发展,努力擦亮水韵泗州 运河名城城市品牌,全县文化旅游业实现高质量发展 [详细]
03-19 淡季不忘引流 京郊民宿市场有望迎来回暖
淡季不忘引流 京郊民宿市场有望迎来回暖
旅游淡季中的京郊民宿有望成为市场中最先复苏的板块。3月17日,北京商报记者调查发现,虽然正值旅游淡季,且受疫情变化的影响,不过各家民 [详细]