實(shí)例和客戶端的元數(shù)據(jù)
Eureka Metadata for Instances and Clients
0姑蓝、Eureka的元數(shù)據(jù)字面理解
- 原文: It is worth spending a bit of time understanding how the Eureka metadata works, so you can use it in a way that makes sense in your platform. There is standard metadata for information such as hostname, IP address, port numbers, the status page, and health check. These are published in the service registry and used by clients to contact the services in a straightforward way. Additional metadata can be added to the instance registration in the eureka.instance.metadataMap, and this metadata is accessible in the remote clients. In general, additional metadata does not change the behavior of the client, unless the client is made aware of the meaning of the metadata. There are a couple of special cases, described later in this document, where Spring Cloud already assigns meaning to the metadata map.
- 譯文: 花時(shí)間了解Eureka元數(shù)據(jù)的工作方式是一件值得的事,這樣您就可以在您的平臺(tái)上使用它了凿滤。標(biāo)準(zhǔn)元數(shù)據(jù)的信息包括:例如主機(jī)名瘩扼、IP地址拣挪、端口號(hào)逼庞、狀態(tài)頁和健康檢查克蚂。這些服務(wù)發(fā)布在服務(wù)注冊(cè)中心闺鲸,并由客戶端使用以簡(jiǎn)單的方式與服務(wù)聯(lián)系“0龋可以將其他元數(shù)據(jù)添加到==eureka.instance.metadataMap==中的實(shí)例注冊(cè)中摸恍,并且遠(yuǎn)程客戶端可以訪問該元數(shù)據(jù)。通常赤屋,附加的元數(shù)據(jù)不會(huì)改變客戶端的行為立镶,除非使客戶端知道元數(shù)據(jù)的含義。本文后面將描述幾個(gè)特殊情況类早,其中Spring Cloud已經(jīng)為元數(shù)據(jù)映射分配了含義媚媒。
1、配置Eureka的元數(shù)據(jù)
eureka:
instance:
metadata-map:
zone: ABC # eureka可以理解的元數(shù)據(jù)涩僻,因?yàn)閑ureka中有這個(gè)zone的名字
mmzs: BBC # 不會(huì)影響客戶端行為缭召,因?yàn)閑ureka中沒有mmzs這個(gè)名字,eureka不能理解這個(gè)元數(shù)據(jù)
Using the EurekaClient
不要在@PostConstruct方法或@Scheduled方法中使用EurekaClient(或任何可能尚未啟動(dòng)ApplicationContext的地方)逆日。
為什么注冊(cè)服務(wù)這么慢嵌巷?
官網(wǎng)文檔:http://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html#_why_is_it_so_slow_to_register_a_service
作為一個(gè)實(shí)例,還會(huì)涉及到一個(gè)默認(rèn)的持續(xù)時(shí)間為30秒的注冊(cè)表(通過客戶端的Service URL)的周期性心跳室抽。
直到實(shí)例晴竞、服務(wù)端和客戶端在本地緩存中都具有相同的元數(shù)據(jù)(因此它可能會(huì)花費(fèi)3個(gè)心跳周期),客戶端才可發(fā)現(xiàn)服務(wù)狠半。您可以通過設(shè)置==eureka.instance.leaseRenewalIntervalInSeconds==來改變周期時(shí)間噩死;將其設(shè)置為小于30秒,加快客戶端連接到其他服務(wù)端的過程神年。==在生產(chǎn)中已维,最好是堅(jiān)持使用默認(rèn),因?yàn)樵诜?wù)器內(nèi)部有一些計(jì)算已日,它們會(huì)對(duì)租賃續(xù)訂期做出一些假設(shè)垛耳。==
eureka服務(wù)實(shí)例的應(yīng)用名稱是?
優(yōu)先級(jí):2>0>1
0飘千、默認(rèn)是讀取配置
spring:
application:
# 建議大家都是用小寫
name: microservice-provider-user
1堂鲜、如該配置沒有,那么就是unknown
2护奈、還可以通過下面的配置:
# 這種配置和swagger沒有沖突缔莲,而第0種配置可能會(huì)有沖突
eureka
instance
appname: USER-SERVICE-MMZS
Eureka開啟自我保護(hù)的提示
當(dāng)出現(xiàn)以下語句時(shí):
<font color="red">EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE. </font>
表明Eureka已經(jīng)開啟自我保護(hù)。
如何解決Eureka Server不踢出已關(guān)停的節(jié)點(diǎn)的問題霉旗?
- server端:
# 設(shè)為false痴奏,關(guān)閉自我保護(hù)主要
eureka.server.enable-self-preservation
# 清理間隔(單位毫秒蛀骇,默認(rèn)是60*1000)
eureka.server.eviction-interval-timer-in-ms
- client端:
# 開啟健康檢查(需要spring-boot-starter-actuator依賴)
eureka.client.healthcheck.enabled = true
# 租期更新時(shí)間間隔(默認(rèn)30秒)
eureka.instance.lease-renewal-interval-in-seconds =10
# 租期到期時(shí)間(默認(rèn)90秒)
eureka.instance.lease-expiration-duration-in-seconds =30
- 示例:
- 服務(wù)器端配置:
eureka:
server:
enableSelfPreservation: false
evictionIntervalTimerInMs: 4000
- 客戶端配置:
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
leaseExpirationDurationInSeconds: 30
==注:== 在生產(chǎn)中,更改Eureka更新頻率將打破服務(wù)器的自我保護(hù)功能读拆,所以最好堅(jiān)持使用默認(rèn)值擅憔,因?yàn)樵诜?wù)器內(nèi)部有一些計(jì)算,他們對(duì)續(xù)約做出假設(shè)檐晕。
參考文檔:https://github.com/spring-cloud/spring-cloud-netflix/issues/373
Eureka配置instanceId顯示IP
- 直接配置:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
==注==: 如果只是配置了eureka.instance.prefer-ip-address=true暑诸,而不配置eureka.instance.instance-id,那還是顯示localhost辟灰,但ip地址是可以訪問得了个榕。
- 手工指定IP(推薦)
添加以下配置:
# 指定此實(shí)例的ip
eureka.instance.ip-address = 127.0.0.1
# 注冊(cè)時(shí)使用ip而不是主機(jī)名
eureka.instance.prefer-ip-address = true
- Spring Cloud Netflix Eureka: 多網(wǎng)卡環(huán)境下Eureka服務(wù)注冊(cè)IP選擇問題
鏈接:https://blog.csdn.net/neosmith/article/details/53126924
Eureka Environment和Eureka DataCenter的配置:
- Eureka Environment的配置:
eureka.environment: 字符串
參考文檔:
https://github.com/Netflix/eureka/wiki/Configuring-Eureka
- Eureka DataCenter的配置
eureka.datacenter: cloud
參考文檔:https://github.com/Netflix/eureka/wiki/Configuring-Eureka
這邊說:配置-Deureka.datacenter=cloud,這樣eureka將會(huì)知道是在AWS云上
- 示例:
eureka:
datacenter: cloud
environment: product
Eureka配置最佳實(shí)踐總結(jié)
參考文檔:
https://github.com/spring-cloud/spring-cloud-netflix/issues/203