【prometheus】-04 轻松搞定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
配置可选项如下:
(资料图)
# 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()
:遍历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
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、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方式的服务原理如下图:
大概说明:Discoverer
启动后定时周期触发从eureka server
的/eureka/apps
接口拉取注册服务元数据,然后通过targetsForApp
遍历app
下的instance
,将每个instance
解析成target
,并将其它元数据信息转换成target
原标签可以用于target
抓取前relabeling
操作。
标签:
相关文章
【prometheus】-04 轻松搞定Prometheus Eureka服务发现-全球热头条
Eureka服务发现协议允许使用EurekaRestAPI检索出Prometheus需要监控的targets,Prometheus会定时周期性的从Eurek
一般劳务报酬所得税率(一般劳务报酬)
1、一、劳务报酬所得的应税项目劳务报酬所得是指个人从事设计、装潢、安装、制图、化验、测试、医疗、法律、会计、咨询、讲学、新闻、广播、翻
芈姝历史原型有几个孩子_芈姝历史原型
1、芈姝的原形是魏妤,魏国人。2、是赢驷的王后,关系一直非常好,她的儿子赢荡也深受赢驷喜爱,在军中威望较高。3、顺理成章
【全球新要闻】看英仙座流星雨最佳时间
1、看英仙座流星雨的最佳时间是8月12日21时至13日0时。天黑后不久,英仙座从东北方向升起。随着辐射点逐渐升高,流星雨的流量逐渐增大。由于月
美国397米地标要倒了?纽约93层摩天大楼剧烈晃动 还有巨响:官方回应
据美国国家广播公司3月21日报道,高397米的纽约新地标建筑范德堡一号大楼于21日出现剧烈晃动,93层楼内的办公人员纷纷紧急撤离,目击者称听到
每日精选:特斯拉ModelY电池在橡树溪峡谷自燃是什么原因造成的
示,电动汽车电池起火的情况很少见,但一旦发生就会引起情绪反应。电动汽车的批评者还利用这些事件作为反对大规模采用电动汽车的
一个动作致人死亡,被判9个月!这些事故触目惊心→-今日热议
近日,江苏南通如东法院开庭审理了一起因开车门致人伤亡的交通肇事案件被告人吴某犯交通肇事罪被判处有期徒刑九个月下车开车门时
扣好人生“第一粒扣子”!宝山这个镇积极创建清廉单元
党员干部要讲原则、守规矩,把好尺度、取舍有道,方有明媚春光相伴。宝山区罗泾镇围绕打造清廉宝山罗泾样板,加快推进清廉建设,组织各基层单
因物业纠纷,业主持刀行凶!重庆警方通报
3月23日,重庆市公安局北碚区分局官方微博@平安北碚通报:3月22日12时许,北碚区云盛路一小区业主罗某因物业纠纷,在小区物管办公室持刀行凶,
每日速看!女超联赛暂告段落但是各支球队并未因此而停歇
女超联赛暂告段落,但是各支球队并未因此而停歇。长春大众卓越女足俱乐部今日宣布,尼日利亚女足国家队国脚杰瑞·乔伊正式加盟球
全球快资讯丨当瑜伽教练一个月工资多少
第一、初级瑜伽教练的月工资在4000--8000不等,这要看当地地区的消费水平。不过还是很客观的啊。第二、中级教练,差不多在6000至10000元不等。如果
国网英大:3月22日融资买入473.87万元,融资融券余额5.01亿元 天天热消息
3月22日,国网英大(600517)融资买入473 87万元,融资偿还292 78万元,融资净买入181 09万元,融资余额4 96亿元。
【天天速看料】刘维伟:我们球员有争夺季后赛的心理压力 导致今天进攻端放不开
刘维伟:我们球员有争夺季后赛的心理压力导致今天进攻端放不开,刘维伟,放不开,cba,关键时刻
环球新资讯:国药股份(600511):营业收入454.99亿元,与上期同比减少2.09%
国药股份(600511)(600511):营业收入454 99亿元,与上期同比减少2 09%3月22日,国药股份年报显示,本期营业收入454 99亿元
【全球独家】联盟猫咪是什么英雄皮肤,英雄联盟提莫的熊猫皮肤有没有特效
1,英雄联盟提莫的熊猫皮肤有没有特效有的2,英雄联盟不详之刃的猫女皮肤买了是永久的么所有皮肤都是永久的3,英雄联盟的猫咪叫什么名字魔法猫
佛山博爱湖约4.79万㎡商住地终止挂牌 原定起拍价8.12亿元_世界讯息
观点网讯:3月22日消息,原定于明日(3月23日)出让的佛山博爱湖约4 79万㎡商住地终止挂牌,地块原定起拍价81263万,起拍楼面价约7350元 ㎡。出让文件
坐飞机能带多大的充电宝_飞机上能充电吗-全球要闻
1、目前很多飞机都提供了USB充电,可以通过USB插口给手机充电。2、有些飞机没有插头就不能充电。3、飞机有这个充电头就
开特斯拉回村被乡亲群嘲“大冤种” 本想给妻子惊喜却成争吵导火索 全球热点评
李晓宁是一位居住在河北邢台农村的居民,结婚纪念日即将来临之际,他秘密订购了一辆白色的特斯拉Model3作为礼物给妻子。本
中邮证券:给予晨光生物买入评级
中邮证券有限责任公司王琦近期对晨光生物进行研究并发布了研究报告《品类扩张扩展企业边界,全球布局提升价值链》,本报告对晨光生物给出买入
全球热议:港股异动 | 中国铝业(02600)绩后跌超4% 主营产品利润下跌 归母净利同比降27%至42亿元
港股异动|中国铝业(02600)绩后跌超4%主营产品利润下跌归母净利同比降27%至42亿元,减利,港股,中国铝业,上市公司,利润下跌,业绩快报
4月1日起 广深港高铁增开62列跨境高铁列车
为进一步满足内地与香港间旅客出行需求,4月1日起,铁路部门将逐步增开广深港高铁香港西九龙站与广东省内跨境高铁列车40列、与广东省外的长途
观速讯丨qc小组成果认证表_qc小组成果报告范文
1、建筑qc小组成果范文_建筑qc小组成果范文悬赏分:5-提问时间2008-7-2320:04问题为何被关闭
莫言诺贝尔获奖作品是什么_莫言诺贝尔获奖作品简述
欢迎观看本篇文章,小勉来为大家解答以上问题。莫言诺贝尔获奖作品是什么,莫言诺贝尔获奖作品简述很多人还不知道,现在让我们一
每日动态!dnf普雷伊西斯机制_普雷伊西丝地下城在
1、在玩地下城与勇士的时候,很多玩家在满级之后发现找不到普雷,怎么都不知道其他玩家说的普雷到底在哪,下面就来告诉大家地下
大哥大续集2粤语版_大哥大续集_全球热头条
1、万梓良的情况类似于刘松仁,他也是“亚视”培养的艺员,毕业于“亚视”艺员培训班,走红却在“无线”,成就他的是一部《流氓