日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

深入理解Eureka自我保护机制

發布時間:2023/12/20 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深入理解Eureka自我保护机制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

? ? ? ?? Eureka自我保護機制

?自我保護開啟條件

自我保護閥值的計算

自我保護閥值的計算公式

自我保護邏輯

自我保護開關

開發環境配置


?

Eureka注冊中心中各個Eureka-Server節點都是平等的,沒有ZK中角色的概念,即使N-1個節點掛掉也不會影響其他節點的正常運行。Eureka-Server可以很好的應對因網絡故障導致部分節點失聯的情況,而不會像ZK那樣如果有一半不可用的情況會導致整個集群不可用而變成癱瘓。

Eureka自我保護機制是為了防止誤殺服務導致系統癱瘓而提供的一個機制。

何為誤殺?

理論上每個服務都有故障的可能,那么自然Eureka注冊中心也有故障的可能性。如果注冊中心發生故障,服務就有可能不能正常續約,或者當網絡分區故障發生時,微服務與Eureka-Server之間無法正常通信,而這個時候服務仍然是可以正常服務的,如果Eureka剔除任務將其剔除,則會造成誤殺。

怎么判斷Eureka Server故障了?

Eureka Server以一種謙虛的態度來判斷是不是自己出了問題,也就是當大量的服務續約超時時,就認為是自己出現問題了。(如果少量服務續約超時,則認為是服務故障)

Eureka自我保護機制

自我保護開啟條件

Eureka-Server通過判斷是否有大量續約失敗,來確定是否開啟自我保護。這里提供了一個閥值numberOfRenewsPerMinThreshold和上一分鐘的續約數進行對比,如果實際的續約數小于了自我保護閥值,則開啟自我保護。

public boolean isLeaseExpirationEnabled() {if(!isSelfPreservationModeEnabled()) {// The self preservation mode is disabled, hence allowing the instances to expire.return true;}return numberOfRenewsPerMinThreshold > 0 && getNumOfRenewsInLastMin() > numberOfRenewsPerMinThreshold; }

?

自我保護閥值的計算

有三個地方會重新計算自我保護閥值numberOfRenewsPerMinThreshold。

1、當新服務注冊(register)時

synchronized (lock) {if (this.expectedNumberOfClientsSendingRenews > 0) {// Since the client wants to register it, increase the number of clients sending renewsthis.expectedNumberOfClientsSendingRenews = this.expectedNumberOfClientsSendingRenews + 1;updateRenewsPerMinThreshold();}}

2、當服務退出(cancel)時

public boolean cancel(final String appName, final String id, final boolean isReplication) {if (super.cancel(appName, id, isReplication)) {replicateToPeers(Action.Cancel, appName, id, null, null, isReplication);synchronized (lock) {if (this.expectedNumberOfClientsSendingRenews > 0) {// Since the client wants to cancel it, reduce the number of clients to send renewsthis.expectedNumberOfClientsSendingRenews = this.expectedNumberOfClientsSendingRenews - 1;updateRenewsPerMinThreshold(); } }return true; }return false; }

3、TimerTask定時任務(默認15分鐘)

/*** Schedule the task that updates <em>renewal threshold</em> periodically.* The renewal threshold would be used to determine if the renewals drop* dramatically because of network partition and to protect expiring too* many instances at a time.**/private void scheduleRenewalThresholdUpdateTask() {timer.schedule(new TimerTask() {@Overridepublic void run() {updateRenewalThreshold();}}, serverConfig.getRenewalThresholdUpdateIntervalMs(),serverConfig.getRenewalThresholdUpdateIntervalMs());}

?

自我保護閥值的計算公式

protected void updateRenewsPerMinThreshold() {this.numberOfRenewsPerMinThreshold = (int) (this.expectedNumberOfClientsSendingRenews* (60.0 / serverConfig.getExpectedClientRenewalIntervalSeconds())* serverConfig.getRenewalPercentThreshold());}

自我保護閥值 = 服務總數 * 每分鐘續約數 * 自我保護續約百分比閥值因子。

每分鐘續約數 =(60S/客戶端續約間隔)

最后自我保護閥值的計算公式為:

自我保護閥值 = 服務總數 * (60S/客戶端續約間隔) * 自我保護續約百分比閥值因子。

備注:在老版本中閥值的計算有點錯誤,新版本中修復成現在的計算公式。

自我保護邏輯

Eureka Server提供了一個EvictionTask定時清理續約超時的服務。清理之前首先判斷是否需要清理(isLeaseExpirationEnabled())。這個判斷方法首先會判斷是否開啟了自我保護開關,如果開啟了,則會繼續判斷上一分鐘的續約數是否小于自我保護閥值,如果上一分鐘的續約數(numOfRenewsInLastMin)小于自我保護閥值(numberOfRenewsPerMinThreshold),則開啟自我保護機制,不再進行服務的剔除。

public boolean isLeaseExpirationEnabled() {if (!isSelfPreservationModeEnabled()) {// The self preservation mode is disabled, hence allowing the instances to expire.return true;}return numberOfRenewsPerMinThreshold > 0 && getNumOfRenewsInLastMin() > numberOfRenewsPerMinThreshold;}

備注1:numberOfRenewsPerMinThreshold是對所有服務總閥值的計算,而不是單個服務的計算。

備注2: Eureka默認的自我保護閥值因子是85%,可以通過renewalPercentThreshold修改。

?

自我保護開關

Eureka自我保護機制,通過配置eureka.server.enable-self-preservation來true打開/false禁用自我保護機制,默認打開狀態,建議生產環境打開此配置。

?

開發環境配置

開發環境中如果要實現服務失效能自動移除,只需要修改以下配置。

1、 注冊中心關閉自我保護機制,修改檢查失效服務的時間。

eureka:server: # 該配置可以移除這種自我保護機制,防止失效的服務也被一直訪問 (Spring Cloud默認該配置是 true)enable-self-preservation: false#該配置可以修改檢查失效服務的時間,每隔30s檢查失效服務,并移除列表 (Spring Cloud默認該配置是 60s)eviction-interval-timer-in-ms: 3000

2、 微服務修改減短服務心跳的時間。

# 告知服務端30秒還未收到心跳的話,就將該服務移除列表,默認90秒 lease-expiration-duration-in-seconds: 30 # 每隔10s發送一次心跳,默認30秒 lease-renewal-interval-in-seconds: 10

以上配置建議在生產環境使用默認的時間配置。

?

Spring Cloud實戰項目Jbone地址

github地址:https://github.com/417511458/jbone

碼云地址:https://gitee.com/majunwei2017/jbone

?

轉自:小馬過河 - 深入理解Eureka - Eureka自我保護機制

總結

以上是生活随笔為你收集整理的深入理解Eureka自我保护机制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。