服务注册相关代码
入口
instanceInfoReplicator.start(clientConfig.getInitialInstanceInfoReplicationIntervalSeconds());
调用注册接口
服务端接收
同步给其他节点
com.netflix.eureka.registry.AbstractInstanceRegistry#invalidateCache
清空读写缓存
服务拉取相关
全量拉取
http 调用
服务端相关接口
缓存中获取
只读缓存,读写缓存获取相关代码
其中读写缓存的构造
this.readWriteCacheMap =
CacheBuilder.newBuilder().initialCapacity(1000)
.expireAfterWrite(serverConfig.getResponseCacheAutoExpirationInSeconds(), TimeUnit.SECONDS)
.removalListener(new RemovalListener<Key, Value>() {
@Override
public void onRemoval(RemovalNotification<Key, Value> notification) {
Key removedKey = notification.getKey();
if (removedKey.hasRegions()) {
Key cloneWithNoRegions = removedKey.cloneWithoutRegions();
regionSpecificKeys.remove(cloneWithNoRegions, removedKey);
}
}
})
.build(new CacheLoader<Key, Value>() {
@Override
public Value load(Key key) throws Exception {
if (key.hasRegions()) {
Key cloneWithNoRegions = key.cloneWithoutRegions();
regionSpecificKeys.put(cloneWithNoRegions, key);
}
Value value = generatePayload(key);
return value;
}
});
从本地缓存中读取
增量拉取
也是先拉取增量接口,没有的话获取全量接口,有的话,就是获取的数据与本地进行比对
即使拉取回来也可能会出现客户端和服务端数据不一样的情况,Eureka 用了很巧妙的方式去处理的
这里他是用了HashCode进行比对的,不同的话就全量拉取服务端的数据
这个比对我们可以学习借鉴
总结
Eureka源码到这里就分析结束了,可能讲不好哈.我觉得重要的是框架的思想和设计思路,我在这里学到了
- 怎样动态的设置延时
- 三级缓存
- 怎样进行新旧数据的对比 下一个分析Ribbon