Spring Cloud中,Eureka常見問題總結
Spring Cloud中,Eureka常見問題總結。
1 eureka.environment: 指定環境參考文檔:
1 eureka.datacenter: 指定數據中心參考文檔: 使用配置項:
1eureka.instance.leaseRenewalIntervalInSeconds |
參考文檔:
翻譯:
作為實例還涉及到與註冊中心的周期性心跳,默認持續時間為30秒(通過serviceUrl)。在實例、服務器、客戶端都在本地緩存中具有相同的元數據之前,服務不可用於客戶端發現(所以可能需要3次心跳)。你可以使用eureka.instance.leaseRenewalIntervalInSeconds 配置,這將加快客戶端連接到其他服務的過程。在生產中,最好堅持使用默認值,因為在服務器內部有一些計算,他們對續約做出假設。 |
如果在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之間存在網絡分區場景下的保護。一旦進入保護模式,Eureka Server將會嘗試保護其服務註冊表中的信息,不再刪除服務註冊表中的數據(也就是不會註銷任何微服務)。
詳見: 在開發過程中,我們常常希望Eureka Server能夠迅速有效地踢出已關停的節點,但是新手由於Eureka自我保護模式,以及心跳周期長的原因,常常會遇到Eureka Server不踢出已關停的節點的問題。解決方法如下:
(1) Eureka Server端:配置關閉自我保護,並按需配置Eureka Server清理無效節點的時間間隔。
1 2eureka.server.enable-self-preservation # 設為false,關閉自我保護 eureka.server.eviction-interval-timer-in-ms # 清理間隔(單位毫秒,默認是60*1000) |
(2) Eureka Client端:配置開啟健康檢查,並按需配置續約更新時間和到期時間。
1 2 3eureka.client.healthcheck.enabled # 開啟健康檢查(需要spring-boot-starter-actuator依賴) eureka.instance.lease-renewal-interval-in-seconds # 續約更新時間間隔(默認30秒) eureka.instance.lease-expiration-duration-in-seconds # 續約到期時間(默認90秒) |
示例:
服務器端配置:
eureka: server: enable-self-preservation: false eviction-interval-timer-in-ms: 4000 |
客戶端配置:
1 2 3 4 5 6 7eureka: client: healthcheck: enabled: true instance: lease-expiration-duration-in-seconds: 30 lease-renewal-interval-in-seconds: 10 |
註意:
更改Eureka更新頻率將打破服務器的自我保護功能,生產環境下不建議自定義這些配置。
詳見: 在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.client.healthcheck.enabled=true
只應該在application.yml中設置。如果設置在bootstrap.yml中將會導致一些不良的副作用,例如在Eureka中註冊的應用名稱是UNKNOWN等。
轉自:http://www.itmuch.com/spring-cloud-sum-eureka/
Spring Cloud中,Eureka常見問題總結