Spring Cloud中,Eureka常見問題總結

Spring Cloud中,Eureka常見問題總結。

指定Eureka的Environment


eureka.environment: 指定環境

參考文檔:https://github.com/Netflix/eureka/wiki/Configuring-Eureka

指定Eureka的DataCenter


eureka.datacenter: 指定數據中心

參考文檔:https://github.com/Netflix/eureka/wiki/Configuring-Eureka
文中指出,配置-Deureka.datacenter=cloud,這樣eureka將會知道是在AWS雲上。

如何解決Eureka註冊服務慢的問題

使用配置項:


eureka.instance.leaseRenewalIntervalInSeconds

參考文檔:http://cloud.spring.io/spring-cloud-static/Camden.SR1/#_why_is_it_so_slow_to_register_a_service原文:


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.

翻譯:


作爲實例還涉及到與註冊中心的週期性心跳,默認持續時間爲30秒(通過serviceUrl)。在實例、服務器、客戶端都在本地緩存中具有相同的元數據之前,服務不可用於客戶端發現(所以可能需要3次心跳)。你可以使用eureka.instance.leaseRenewalIntervalInSeconds 配置,這將加快客戶端連接到其他服務的過程。在生產中,最好堅持使用默認值,因爲在服務器內部有一些計算,他們對續約做出假設。

Eureka的自我保護模式

如果在Eureka Server的首頁看到以下這段提示,則說明Eureka已經進入了保護模式。


EMERGENCYEUREKA 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將會嘗試保護其服務註冊表中的信息,不再刪除服務註冊表中的數據(也就是不會註銷任何微服務)。

詳見:https://github.com/Netflix/eureka/wiki/Understanding-Eureka-Peer-to-Peer-Communication

如何解決Eureka Server不踢出已關停的節點的問題

在開發過程中,我們常常希望Eureka Server能夠迅速有效地踢出已關停的節點,但是新手由於Eureka自我保護模式,以及心跳週期長的原因,常常會遇到Eureka Server不踢出已關停的節點的問題。解決方法如下:

(1) Eureka Server端:配置關閉自我保護,並按需配置Eureka Server清理無效節點的時間間隔。


eureka.server.enable-self-preservation # 設爲false,關閉自我保護
eureka.server.eviction-interval-timer-in-ms # 清理間隔(單位毫秒,默認是60*1000)

(2) Eureka Client端:配置開啓健康檢查,並按需配置續約更新時間和到期時間。


eureka.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

客戶端配置:


eureka:
client:
healthcheck:
enabled: true
instance:
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 10

注意:
更改Eureka更新頻率將打破服務器的自我保護功能,生產環境下不建議自定義這些配置。
詳見: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.itmuch.com/spring-cloud-sum-eureka/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章