Eureka 源碼解析 —— 註冊表 InstanceRegistry 類關係

摘要: 原創出處 http://www.iocoder.cn/Eureka/instance-registry-class-diagram/ 「芋道源碼」歡迎轉載,保留摘要,謝謝!

本文主要基於 Eureka 1.8.X 版本



1. 概述

本文主要簡介 註冊表 InstanceRegistry 的類關係,爲後文的應用實例註冊發現Eureka-Server 集羣複製做整體的鋪墊。

推薦 Spring Cloud 書籍

2. 類圖

  • com.netflix.eureka.registry.AwsInstanceRegistry,主要用於亞馬遜 AWS,跳過。
  • com.netflix.eureka.registry.RemoteRegionRegistry,筆者暫時不太理解它的用途。目前猜測 Eureka-Server 集羣和集羣之間的註冊信息的交互方式。查閱官方資料,《Add ability to retrieve instances from any remote region》 在做了簡單介紹。翻看目前網絡上的博客、書籍、項目實戰,暫時都沒提及此塊。估摸和亞馬遜 AWS 跨區域( region ) 機制有一定關係,先暫時跳過。有了解此塊的同學,麻煩告知下筆者,萬分感謝。TODO[0009]:RemoteRegionRegistry。
  • 藍框部分,本文主角。

3. LookupService

com.netflix.discovery.shared.LookupService,查找服務接口,提供簡單單一的方式獲取應用集合(com.netflix.discovery.shared.Applications) 和 應用實例信息集合( com.netflix.appinfo.InstanceInfo )。接口代碼如下:


public interface LookupService<T> {
Application getApplication(String appName);
Applications getApplications();
List<InstanceInfo> getInstancesById(String id);
InstanceInfo getNextServerFromEureka(String virtualHostname, boolean secure);
}

  • 在 Eureka-Client 裏,EurekaClient 繼承該接口。
  • 在 Eureka-Server 裏,com.netflix.eureka.registry.InstanceRegistry 繼承該接口。

4. LeaseManager

com.netflix.eureka.lease.LeaseManager,租約管理器接口,提供租約的註冊、續租、取消( 主動下線 )、過期( 過期下線 )。接口代碼如下:


public interface LeaseManager<T> {
void register(T r, int leaseDuration, boolean isReplication);
boolean cancel(String appName, String id, boolean isReplication);
boolean renew(String appName, String id, boolean isReplication);
void evict();
}

5. InstanceRegistry

com.netflix.eureka.registry.InstanceRegistry應用實例註冊表接口。它繼承了 LookupService 、LeaseManager 接口,提供應用實例的註冊發現服務。另外,它結合實際業務場景,定義了更加豐富的接口方法。接口代碼如下:


public interface InstanceRegistry extends LeaseManager<InstanceInfo>, LookupService<String> {
// ====== 開啓與關閉相關 ======
void openForTraffic(ApplicationInfoManager applicationInfoManager, int count);
void shutdown();
void clearRegistry();
// ====== 應用實例狀態變更相關 ======
void storeOverriddenStatusIfRequired(String appName, String id, InstanceStatus overriddenStatus);
boolean statusUpdate(String appName, String id, InstanceStatus newStatus,
String lastDirtyTimestamp, boolean isReplication);
boolean deleteStatusOverride(String appName, String id, InstanceStatus newStatus,
String lastDirtyTimestamp, boolean isReplication);
Map<String, InstanceStatus> overriddenInstanceStatusesSnapshot();
// ====== 響應緩存相關 ======
void initializedResponseCache();
ResponseCache getResponseCache();
// ====== 自我保護模式相關 ======
long getNumOfRenewsInLastMin();
int getNumOfRenewsPerMinThreshold();
int isBelowRenewThresold();
boolean isSelfPreservationModeEnabled();
public boolean isLeaseExpirationEnabled();
// ====== 調試/監控相關 ======
List<Pair<Long, String>> getLastNRegisteredInstances();
List<Pair<Long, String>> getLastNCanceledInstances();
}

6. AbstractInstanceRegistry

com.netflix.eureka.registry.AbstractInstanceRegistry,應用對象註冊表抽象實現

這裏先不拓展開,《Eureka 源碼解析 —— 應用實例註冊發現》系列 逐篇分享。

7. PeerAwareInstanceRegistry

com.netflix.eureka.registry.PeerAwareInstanceRegistry,PeerAware ( 暫時找不到合適的翻譯 ) 應用對象註冊表接口,提供 Eureka-Server 集羣內註冊信息的同步服務。接口代碼如下:


public interface PeerAwareInstanceRegistry extends InstanceRegistry {
void init(PeerEurekaNodes peerEurekaNodes) throws Exception;
int syncUp();
boolean shouldAllowAccess(boolean remoteRegionRequired);
void register(InstanceInfo info, boolean isReplication);
void statusUpdate(final String asgName, final ASGResource.ASGStatus newStatus, final boolean isReplication);
}

8. PeerAwareInstanceRegistryImpl

com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl,PeerAware ( 暫時找不到合適的翻譯 ) 應用對象註冊表實現類

這裏先不拓展開,《Eureka 源碼解析 —— Eureka-Server 集羣》系列 逐篇分享。

發佈了22 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章