在上文中介绍了基础类AbstractRegistry类的解释,在本篇中将继续介绍该包下的其他类。

FailbackRegistry

该类继承了AbstractRegistry,AbstractRegistry中的注册订阅等方法,实际上就是一些内存缓存的变化,而真正的注册订阅的实现逻辑在FailbackRegistry实现,并且FailbackRegistry提供了失败重试的机制。

初始化

// Scheduled executor service
// 定时任务执行器
private final ScheduledExecutorService retryExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("DubboRegistryFailedRetryTimer", true)); // Timer for failure retry, regular check if there is a request for failure, and if there is, an unlimited retry
// 失败重试定时器,定时去检查是否有请求失败的,如有,无限次重试。
private final ScheduledFuture<?> retryFuture; // 注册失败的URL集合
private final Set<URL> failedRegistered = new ConcurrentHashSet<URL>(); // 取消注册失败的URL集合
private final Set<URL> failedUnregistered = new ConcurrentHashSet<URL>(); // 订阅失败的监听器集合
private final ConcurrentMap<URL, Set<NotifyListener>> failedSubscribed = new ConcurrentHashMap<URL, Set<NotifyListener>>(); // 取消订阅失败的监听器集合
private final ConcurrentMap<URL, Set<NotifyListener>> failedUnsubscribed = new ConcurrentHashMap<URL, Set<NotifyListener>>(); // 通知失败的URL集合
private final ConcurrentMap<URL, Map<NotifyListener, List<URL>>> failedNotified = new ConcurrentHashMap<URL, Map<NotifyListener, List<URL>>>(); /**
* The time in milliseconds the retryExecutor will wait
*/
// 重试频率
private final int retryPeriod;

构造函数

public FailbackRegistry(URL url) {
super(url);
// 从url中读取重试频率,如果为空,则默认5000ms
this.retryPeriod = url.getParameter(Constants.REGISTRY_RETRY_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RETRY_PERIOD);
// 创建失败重试定时器
this.retryFuture = retryExecutor.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
// Check and connect to the registry
try {
//重试
retry();
} catch (Throwable t) { // Defensive fault tolerance
logger.error("Unexpected error occur at failed retry, cause: " + t.getMessage(), t);
}
}
}, retryPeriod, retryPeriod, TimeUnit.MILLISECONDS);
}

构造函数主要是创建了失败重试的定时器,重试频率从URL取,如果没有设置,则默认为5000ms。

在该类中对注册、取消注册、订阅、取消订阅进行了重写操作,代码逻辑相对简单。

 @Override
public void register(URL url) {
super.register(url);
//首先从失败的缓存中删除该url
failedRegistered.remove(url);
failedUnregistered.remove(url);
try {
// Sending a registration request to the server side
// 向注册中心发送一个注册请求
doRegister(url);
} catch (Exception e) {
Throwable t = e; // If the startup detection is opened, the Exception is thrown directly.
// 如果开启了启动时检测,则直接抛出异常
boolean check = getUrl().getParameter(Constants.CHECK_KEY, true)
&& url.getParameter(Constants.CHECK_KEY, true)
&& !Constants.CONSUMER_PROTOCOL.equals(url.getProtocol());
boolean skipFailback = t instanceof SkipFailbackWrapperException;
if (check || skipFailback) {
if (skipFailback) {
t = t.getCause();
}
throw new IllegalStateException("Failed to register " + url + " to registry " + getUrl().getAddress() + ", cause: " + t.getMessage(), t);
} else {
logger.error("Failed to register " + url + ", waiting for retry, cause: " + t.getMessage(), t);
} // Record a failed registration request to a failed list, retry regularly
// 把这个注册失败的url放入缓存,并且定时重试。
failedRegistered.add(url);
}
}

在注册中它会失败的注册缓存和失败的未注册缓存集合中移除该URL,然后向注册中心执行注册。

AbstractRegistryFactory

该类实现了RegistryFactory接口,抽象了createRegistry方法,它实现了Registry的容器。

初始化

 private static final ReentrantLock LOCK = new ReentrantLock();

    // Registry Collection Map<RegistryAddress, Registry>
// Registry 集合
private static final Map<String, Registry> REGISTRIES = new ConcurrentHashMap<String, Registry>();

销毁所有的Registry对象,并清理缓存数据

public static Collection<Registry> getRegistries() {
return Collections.unmodifiableCollection(REGISTRIES.values());
} public static void destroyAll() {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Close all registries " + getRegistries());
}
// Lock up the registry shutdown process
// 获得锁
LOCK.lock();
try {
for (Registry registry : getRegistries()) {
try {
// 销毁
registry.destroy();
} catch (Throwable e) {
LOGGER.error(e.getMessage(), e);
}
}
// 清空缓存
REGISTRIES.clear();
} finally {
// Release the lock
// 释放锁
LOCK.unlock();
}
}

该方法是实现了Registry接口的方法,这里最要注意的是createRegistry,因为AbstractRegistryFfactory本身就是抽象类,而createRegistry也是抽象方法,为了让子类只要关注该方法,比如说redis实现的注册中心和zookeeper实现的注册中心创建方式肯定不同,而他们相同的一些操作都已经在AbstractRegistryFactory中实现,所以只要关注且实现该抽象方法即可。

@Override
public Registry getRegistry(URL url) {
// 修改url
url = url.setPath(RegistryService.class.getName())
.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName())
.removeParameters(Constants.EXPORT_KEY, Constants.REFER_KEY);
// 计算key值
String key = url.toServiceString();
// Lock the registry access process to ensure a single instance of the registry
// 获得锁
LOCK.lock();
try {
Registry registry = REGISTRIES.get(key);
if (registry != null) {
return registry;
}
// 创建Registry对象
registry = createRegistry(url);
if (registry == null) {
throw new IllegalStateException("Can not create registry " + url);
}
// 添加到缓存。
REGISTRIES.put(key, registry);
return registry;
} finally {
// Release the lock
// 释放锁
LOCK.unlock();
}
}

Dubbo-服务注册中心之AbstractRegistryFactory等源码的更多相关文章

  1. [源码阅读] 阿里SOFA服务注册中心MetaServer(1)

    [源码阅读] 阿里SOFA服务注册中心MetaServer(1) 目录 [源码阅读] 阿里SOFA服务注册中心MetaServer(1) 0x00 摘要 0x01 服务注册中心 1.1 服务注册中心简 ...

  2. [源码阅读] 阿里SOFA服务注册中心MetaServer(2)

    [源码阅读] 阿里SOFA服务注册中心MetaServer(2) 目录 [源码阅读] 阿里SOFA服务注册中心MetaServer(2) 0x00 摘要 0x01 MetaServer 注册 1.1 ...

  3. [源码阅读] 阿里SOFA服务注册中心MetaServer(3)

    [源码阅读] 阿里SOFA服务注册中心MetaServer(3) 目录 [源码阅读] 阿里SOFA服务注册中心MetaServer(3) 0x00 摘要 0x01 概念 1.1 分布式一致性 1.2 ...

  4. 搭建高可用服务注册中心-Spring Cloud学习第一天(非原创)

    文章大纲 一.Spring Cloud基础知识介绍二.创建单一的服务注册中心三.创建一个服务提供者四.搭建高可用服务注册中心五.项目源码与参考资料下载六.参考文章   一.Spring Cloud基础 ...

  5. 基于ZooKeeper的服务注册中心

    本文介绍基于ZooKeeper的Dubbo服务注册中心的原理. 1.ZooKeeper中的节点 ZooKeeper是一个树形结构的目录服务,支持变更推送,因此非常适合作为Dubbo服务的注册中心. 注 ...

  6. SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载)

    场景 SpringCloud学习之运行第一个Eureka程序: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/90611451 S ...

  7. Dubbo多注册中心和Zookeeper服务的迁移

    一.Dubbo多注册中心 1. 应用场景 例如阿里有些服务来不及在青岛部署,只在杭州部署,而青岛的其它应用需要引用此服务,就可以将服务同时注册到两个注册中心. consumer.xml <?xm ...

  8. 阿里dubbo服务注册原理解析

           阿里分布式服务框架 dubbo现在已成为了外面很多中小型甚至一些大型互联网公司作为服务治理的一个首选或者考虑方案,相信大家在日常工作中或多或少都已经用过或者接触过dubbo了.但是我搜了 ...

  9. SpringCloud学习(3)——Eureka服务注册中心及服务发现

    Eureka概述: Eureka是Netflix的一个子模块, 也是核心模块之一.Eureka是一个基于REST的服务, 用于定位服务, 以实现云端中间层服务发现和故障转移.服务注册与发现对于微服务框 ...

随机推荐

  1. [Redis-CentOS7]Redis打开远程连接(十) Could not connect to Redis at 127.0.0.1:6379: Connection refused

    通过网络无法访问Redis redis-cli 172.16.1.111 Could not connect to Redis at 127.0.0.1:6379: Connection refuse ...

  2. Centos 7.5 搭建FTP配置虚拟用户

    Centos 7.5 搭建FTP配置虚拟用户 1.安装vsftpd #vsftpd下载地址 http://mirror.centos.org/centos/7/os/x86_64/Packages/v ...

  3. Linux 文件、目录操作

    Linux中的路径只能使用/,不能使用\ 或\\. cd   切换目录 cd  /    切换到系统根目录,cd即change dir cd  /bin  切换到根目录下的bin目录 cd  ..  ...

  4. 00.JS前言

    前言: 学习一门编程语言的基本步骤(01)了解背景知识 1.了解背景知识   1)什么是 JavaScript 语言?     JavaScript 是一种轻量级的脚本语言.所谓“脚本语言”(scri ...

  5. kong服务网关API

    kong服务网关API pingforever关注 0.1762017.05.23 11:16:08字数 834阅读 7,367 kong简介 Kong 是在客户端和(微)服务间转发API通信的API ...

  6. 返回一个整数数组中最大子数组的和——java程序设计

    一.题目要求 1.输入一个整形数组,数组里有正数也有负数.2.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.3.求所有子数组的和的最大值.要求时间复杂度为O(n) 二.设计思想 解决 ...

  7. 服务器控件Repeater

    在使用aspx开发中,如果一个页面做纯数据展示,Repeater会比GridView更适合,因为它是轻量级的 下面是最基本的用法:  aspx: <table> <asp:Repea ...

  8. C语言再学习part3—算法

    君子远庖厨,万物皆备于我.—孟子 这篇文章主要总结程序的主要要素,以及程序的构成是什么样子的.最后说说我学到的一种奇特的表示算法的方式—伪代码. 让我们开始吧! 一个程序应该包括以下两个主要要素: 1 ...

  9. 新年上新!极光认证 Web SDK 首版上线

    新年伊始,极光开发者服务也抢先为各位开发者朋友带来了"新年大礼包",几款明星产品都悉数有不少更新: 极光认证 Web SDK 版本上线 相信不少小伙伴早已熟知极光认证这款产品,3秒 ...

  10. day 12 函数

    函数 函数的定义和调用 def 函数名(形参): 函数体 return 返回值 调用 函数名(实参) 站在形参的角度上: 位置参数, *args, 默认参数(陷阱), 关键字参数, **kwargs ...