Eureka 源码解析 —— 注册表 InstanceRegistry 类关系

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 集群》系列 逐篇分享。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值