1.概述
看下 《Dubbo 用户指南 —— Redis 注册中心》 文档,内容如下:
基于 Redis实现的注册中心 :
使用 Redis 的 Key/Map 结构存储数据结构:
- 主 Key 为服务名和类型
- Map 中的 Key 为 URL 地址
- Map 中的 Value 为过期时间,用于判断脏数据,脏数据由监控中心删除
类图如下:
2. RedisRegistry
2.1 属性和构造方法
/**
* 默认端口
*/
private static final int DEFAULT_REDIS_PORT = 6379;
/**
* 默认 Redis 根节点
*/
private final static String DEFAULT_ROOT = "dubbo";
/**
* Redis Key 过期机制执行器
*/
private final ScheduledExecutorService expireExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("DubboRegistryExpireTimer", true));
/**
* Redis Key 过期机制 Future
*/
private final ScheduledFuture<?> expireFuture;
/**
* Redis 根节点
*/
private final String root;
/**
* JedisPool 集合
*
* key:ip:port
*/
private final Map<String, JedisPool> jedisPools = new ConcurrentHashMap<String, JedisPool>();
/**
* 通知器集合
*
* key:Root + Service ,例如 `/dubbo/com.alibaba.dubbo.demo.DemoService`
*/
private final ConcurrentMap<String, Notifier> notifiers = new ConcurrentHashMap<String, Notifier>();
/**
* 重连周期,单位:毫秒
*/
private final int reconnectPeriod;
/**
* 过期周期,单位:毫秒
*/
private final int expirePeriod;
/**
* 是否监控中心
*
* 用于判断脏数据,脏数据由监控中心删除 {@link #clean(Jedis)}
*/
private volatile boolean admin = false;
/**
* 是否复制模式
*/
private boolean replicate;
public RedisRegistry(URL url) {
super(url);
if (url.isAnyHost()) {
throw new IllegalStateException("registry address == null");
}
// 创建 GenericObjectPoolConfig 对象
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setTestOnBorrow(url.getParameter("test.on.borrow", true));
config.setTestOnReturn(url.getParameter("test.on.return", false));
config.setTestWhileIdle(url.getParameter("test.while.idle", false));
if (url.getParameter("max.idle", 0) > 0)
config.setMaxIdle(url.getParameter("max.idle", 0));
if (url.getParameter("min.idle", 0) > 0)
config.setMinIdle(url.getParameter("min.idle", 0));
if (url.getParameter("max.active", 0) > 0)
config.setMaxTotal(url.getParameter("max.active", 0));
if (url.getParameter("max.total", 0) > 0)
config.setMaxTotal(url.getParameter("max.total", 0));
if (url.getParameter("max.wait", url.getParameter("timeout", 0)) > 0)
config.setMaxWaitMillis(url.getParameter("max.wait", url.getParameter("timeout", 0)));
if (url.getParameter("num.tests.per.eviction.run", 0) > 0)
config.setNumTestsPerEvictionRun(url.getParameter("num.tests.per.eviction.run", 0));
if (url.getParameter("time.between.eviction.runs.millis", 0) > 0)
config.setTimeBetweenEvictionRunsMillis(url.getParameter("time.between.eviction.runs.millis", 0));
if (url.getParameter("min.evictable.idle.time.millis", 0) > 0)
config.setMinEvictableIdleTimeMillis(url.getParameter("min.evictable.idle.time.millis", 0));
// 是否复制模式
String cluster = url.getParameter("cluster", "failover");
if (!"failover".equals(cluster) && !"replicate".equals(cluster)) {
throw new IllegalArgumentException("Unsupported redis cluster: " + cluster + ". The redis cluster only supported failover or replicate.");
}
replicate = "replicate".equals(cluster);
// 解析
List<String> addresses = new ArrayList<String>();
addresses.add(url.getAddress());
String[] backups = url.getParameter(Constants.BACKUP_KEY, new String[0]);
if (backups != null && backups.length > 0) {
addresses.addAll(Arrays.asList(backups));
}
// 创建 JedisPool 对象
String password = url.getPassword();
for (String address : addresses) {
int i = address.indexOf(':');
String host;
int port;
if (i > 0) {
host = address.substring(0, i);
port = Integer.parseInt(address.substring(i + 1));
} else {
host = address;
port = DEFAULT_REDIS_PORT;
}
if (StringUtils.isEmpty(password)) { // 无密码连接
this.jedisPools.put(address, new JedisPool(config, host, port,
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)));
} else { // 有密码连接
this.jedisPools.put(address, new JedisPool(config, host, port,
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT), password));
}
}
// 解析重连周期
this.reconnectPeriod = url.getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD);
// 获得 Redis 根节点
String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);