Eureka client 源码解析(二)

575 阅读1分钟

服务注册相关代码

image.png 入口

instanceInfoReplicator.start(clientConfig.getInitialInstanceInfoReplicationIntervalSeconds());

image.png

image.png

image.png

image.png 调用注册接口

image.png 服务端接收

image.png 同步给其他节点

image.png com.netflix.eureka.registry.AbstractInstanceRegistry#invalidateCache

image.png 清空读写缓存 image.png

服务拉取相关

全量拉取

image.png http 调用

image.png 服务端相关接口

image.png 缓存中获取

image.png 只读缓存,读写缓存获取相关代码

image.png 其中读写缓存的构造

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;
                    }
                });

从本地缓存中读取 image.png

image.png

增量拉取

image.png 也是先拉取增量接口,没有的话获取全量接口,有的话,就是获取的数据与本地进行比对

image.png

即使拉取回来也可能会出现客户端和服务端数据不一样的情况,Eureka 用了很巧妙的方式去处理的

image.png 这里他是用了HashCode进行比对的,不同的话就全量拉取服务端的数据

image.png 这个比对我们可以学习借鉴

总结

Eureka源码到这里就分析结束了,可能讲不好哈.我觉得重要的是框架的思想和设计思路,我在这里学到了

  • 怎样动态的设置延时
  • 三级缓存
  • 怎样进行新旧数据的对比 下一个分析Ribbon