spring Cloud中,Eureka常見問題總結。
指定Eureka的Environment
1
|
eureka.environment: 指定環(huán)境
|
參考文檔:https://github.com/Netflix/eureka/wiki/Configuring-Eureka
指定Eureka的DataCenter
1
|
eureka.datacenter: 指定數(shù)據(jù)中心
|
參考文檔:https://github.com/Netflix/eureka/wiki/Configuring-Eureka
文中指出,配置-Deureka.datacenter=cloud,這樣eureka將會知道是在AWS云上。
如何解決Eureka注冊服務慢的問題
使用配置項:
1
|
eureka.instance.leaseRenewalIntervalInSeconds
|
參考文檔:
http://cloud./spring-cloud-static/Camden.SR1/#_why_is_it_so_slow_to_register_a_service
原文:
1 2 3
|
Why is it so Slow to Register a Service? Being an instance also involves a periodic heartbeat to the registry (via the client’s serviceUrl) with default duration 30 seconds. A service is not available for discovery by clients until the instance, the server and the client all have the same metadata in their local cache (so it could take 3 heartbeats). You can change the period using eureka.instance.leaseRenewalIntervalInSeconds and this will speed up the process of getting clients connected to other services. In production it’s probably better to stick with the default because there are some computations internally in the server that make assumptions about the lease renewal period.
|
翻譯:
1
|
作為實例還涉及到與注冊中心的周期性心跳,默認持續(xù)時間為30秒(通過serviceUrl)。在實例、服務器、客戶端都在本地緩存中具有相同的元數(shù)據(jù)之前,服務不可用于客戶端發(fā)現(xiàn)(所以可能需要3次心跳)。你可以使用eureka.instance.leaseRenewalIntervalInSeconds 配置,這將加快客戶端連接到其他服務的過程。在生產中,最好堅持使用默認值,因為在服務器內部有一些計算,他們對續(xù)約做出假設。
|
Eureka的自我保護模式
如果在Eureka Server的首頁看到以下這段提示,則說明Eureka已經進入了保護模式。
1
|
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.
|
保護模式主要用于一組客戶端和Eureka Server之間存在網絡分區(qū)場景下的保護。一旦進入保護模式,Eureka Server將會嘗試保護其服務注冊表中的信息,不再刪除服務注冊表中的數(shù)據(jù)(也就是不會注銷任何微服務)。
詳見:https://github.com/Netflix/eureka/wiki/Understanding-Eureka-Peer-to-Peer-Communication
如何解決Eureka
Server不踢出已關停的節(jié)點的問題
在開發(fā)過程中,我們常常希望Eureka Server能夠迅速有效地踢出已關停的節(jié)點,但是新手由于Eureka自我保護模式,以及心跳周期長的原因,常常會遇到Eureka Server不踢出已關停的節(jié)點的問題。解決方法如下:
(1) Eureka Server端:配置關閉自我保護,并按需配置Eureka Server清理無效節(jié)點的時間間隔。
1 2
|
eureka.server.enable-self-preservation # 設為false,關閉自我保護 eureka.server.eviction-interval-timer-in-ms # 清理間隔(單位毫秒,默認是60*1000)
|
(2) Eureka Client端:配置開啟健康檢查,并按需配置續(xù)約更新時間和到期時間。
1 2 3
|
eureka.client.healthcheck.enabled # 開啟健康檢查(需要spring-boot-starter-actuator依賴) eureka.instance.lease-renewal-interval-in-seconds # 續(xù)約更新時間間隔(默認30秒) eureka.instance.lease-expiration-duration-in-seconds # 續(xù)約到期時間(默認90秒)
|
示例:
服務器端配置:
1 2 3 4
|
eureka: server: enable-self-preservation: false eviction-interval-timer-in-ms: 4000
|
客戶端配置:
1 2 3 4 5 6 7
|
eureka: client: healthcheck: enabled: true instance: lease-expiration-duration-in-seconds: 30 lease-renewal-interval-in-seconds: 10
|
注意:
更改Eureka更新頻率將打破服務器的自我保護功能,生產環(huán)境下不建議自定義這些配置。
詳見:https://github.com/spring-cloud/spring-cloud-netflix/issues/373
自定義Eureka的Instance
ID
在Spring Cloud中,服務的Instance ID的默認值是${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}} ,也就是機器主機名:應用名稱:應用端口 。因此在Eureka
Server首頁中看到的服務的信息類似如下:itmuch:microservice-provider-user:8000 。如果想要自定義這部分的信息怎么辦?
示例:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
preferIpAddress: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 將Instance ID設置成IP:端口的形式
Eureka配置最佳實踐參考
https://github.com/spring-cloud/spring-cloud-netflix/issues/203
注意點:eureka.client.healthcheck.enabled=true配置項必須設置在application.yml中
eureka.client.healthcheck.enabled=true 只應該在application.yml中設置。如果設置在bootstrap.yml中將會導致一些不良的副作用,例如在Eureka中注冊的應用名稱是UNKNOWN等。
轉自:http://www./spring-cloud-sum-eureka/
|