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
操作。
标签:
精彩推荐
幸福源自关爱,和谐来自于希望,生命因关爱而美丽,人间因感恩而和谐。6月1日上午,由郑州慈善总会主办,祝
工业汽轮机是指直接用汽轮机作为原动机来驱动一些大型的机械设备,如大型风机、给水泵压缩机等功率比较大的
作者:芳草屋来源:帆书原樊登读书(ID:readingclub_btfx)生活实苦,但一点一点地往前熬,久了便没有那么
向着温暖的地方,一路前行,沿途所遇见的,都是美好的时光,所赠予的礼物。云南,是很多人喜欢的地方,温暖
《曹刿论战》出自《左传》,讲述了春秋时期,草根战略家曹刿在长勺之战中的精彩表现。在曹刿的精妙规划
6月2日消息,据中证报,网传一则比亚迪西安工厂起火视频,视频画面显示现场浓烟滚滚。从比亚迪获悉,西
日前,“东法猎赖”夏季集中执行行动暂告一段落、顺利迎来收官。行动期间,内蒙古鄂尔多斯市东胜区人民...
能源危机引发连锁效应!欧洲突然从无煤可用到库存过剩,去年,为了挽救欧洲经济免于电力枯竭,人们囤积了大量
2023年高考在即,上海市教育考试院官方微信“上海国子监发布”发布重要考试提示! 请考生做好个...
中国网财经6月2日讯据中国物流与采购联合会网站消息,中国物流与采购联合会发布的2023年5月份中国物流业景
同花顺数据中心显示,江天化学6月1日获融资买入72 88万元,占当日买入金额的10 24%,当前融资余额4652 92万
6月1日,居民在宣恩县贡水河畔的民族风情走廊休闲。近年来,位于湖北省西部山区的恩施土家族苗族自治州宣恩
烟台黄渤海新区零工市场推出“惠工早餐摊”中工网讯(工人日报-中工网记者杨明清张嫱通讯员宋洁)“早上...
1、m毫M以前用的物质的量浓度单位。2、现在是mol LmM=10^-3mol L。相信通过1mm这篇文章能帮到你,
现货黄金日内涨幅达到1 00%,报1982 10美元 盎司;COMEX黄金期货主力最新报1999 70美元 盎司,日图涨0 89%;
商务部发展报告显示,2020年中国零食行业总产值已超3万亿元,预计20至24年复合增长率将超过8%,到2024年零
1、买遮阳伞时要注意伞里是否有个卷标写明这把伞是抗UV的,也就是这把伞真的可以防紫外线(一般的伞其实是
5月31日,娄底市市场监管局党组书记、局长付凯军带队,对娄底一小周边商店“五毛食品”开展专项检查,正...
1、《十月》暂不接受电子投稿,只接受纸质投稿。2、《十月》刊载散文、中篇短篇小说,诗歌,《十月·长...
想必现在有很多小伙伴对于去相亲第一次见面不爱说话方面的知识都比较想要了解,那么今天小好小编就为大家收
资讯News
10-09
08-15
07-07
11-03
11-03
11-03
11-03
11-03
11-03
11-03
11-03
11-03
11-03
聚焦Policy
当好农民工的“护薪人” 近日,罗某等7名农民工在收到被拖欠的工资后,纷纷打电话向江西省南昌市...
“通讯录里所有人都知道我欠钱了” □ 本报记者 韩丹东 □ 本报见习记者 张守坤 ...
大连宝马车撞人案肇事司机被判死刑 本报讯 记者韩宇 10月29日,辽宁省大连市中级人民法院一审...
医院财务迷上网络赌博输光5000万元公款 □ 本报记者 马维博 □ 本报通讯员 汪宇堂 曹...
辊环车削 雕琢毫厘(工匠绝活) 【绝活看点】 23年来,雷虎始终扎根一线,改进钢材轧制工艺...
交警严查超标电动自行车挪用“白牌” 截至昨晚6时,处罚电动自行车违法行为共计6585笔;下一步将...
明起寒潮来袭 北方气温普降10℃以上 中央气象台预计,本周日北京平原地区最低气温降至-4℃左右...
多种蔬菜价格降幅达五成 包括菠菜、蒿子秆等 预计本月中旬蔬菜恢复供需平衡 本报讯(记者...
北京周日最低气温或达-4℃ 本报讯(记者 赵婷婷)北京青年报记者昨天从中央气象台获悉,新一股...
昌平一家四口确诊新冠肺炎 天通北苑第二社区升级为中风险地区 朝阳两涉疫校区及16所学校停课 ...