Eureka 学习笔记1:服务端实例缓存

版本 awsVersion = '1.11.277'

缓存 类型
registry ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>> AbstractInstanceRegistry成员变量
readWriteCacheMap LoadingCache ResponseCacheImpl成员变量
readOnlyCacheMap ConcurrentMap<Key, Value> ResponseCacheImpl成员变量

registry

java 复制代码
// com.netflix.eureka.registry.AbstractInstanceRegistry
protected void postInit() {
    evictionTaskRef.set(new EvictionTask());
    evictionTimer.schedule(
        evictionTaskRef.get(),
        serverConfig.getEvictionIntervalTimerInMs(),
        // 配置evictionIntervalTimerInMs
        serverConfig.getEvictionIntervalTimerInMs());
}
class EvictionTask extends TimerTask {
    public void run() {
        long compensationTimeMs = getCompensationTimeMs();
        evict(compensationTimeMs);
    }
    // ...
}

evictionIntervalTimerInMs 指定清理未续约服务实例的时间间隔,默认 60s


readWriteCacheMap

java 复制代码
// com.netflix.eureka.registry.ResponseCacheImpl
this.readWriteCacheMap =
    CacheBuilder.newBuilder()
        // 配置initialCapacityOfResponseCache
        .initialCapacity(serverConfig.getInitialCapacityOfResponseCache())
        // 配置responseCacheAutoExpirationInSeconds
        .expireAfterWrite(serverConfig.getResponseCacheAutoExpirationInSeconds(), TimeUnit.SECONDS)

initialCapacityOfResponseCache 指定 readWriteCacheMap 缓存容量大小,默认 1000

responseCacheAutoExpirationInSeconds 指定 readWriteCacheMap 缓存有效时间,默认 180s


readOnlyCacheMap

java 复制代码
// com.netflix.eureka.registry.ResponseCacheImpl
// 配置shouldUseReadOnlyResponseCache
if (shouldUseReadOnlyResponseCache) {
    timer.schedule(getCacheUpdateTask(),
        new Date(((System.currentTimeMillis() / responseCacheUpdateIntervalMs) * responseCacheUpdateIntervalMs)
                            + responseCacheUpdateIntervalMs),
        // 配置responseCacheUpdateIntervalMs
        responseCacheUpdateIntervalMs);
}

private TimerTask getCacheUpdateTask() {
    return new TimerTask() {
        public void run() {
            // ...    
            for (Key key : readOnlyCacheMap.keySet()) {
                CurrentRequestVersion.set(key.getVersion());
                Value cacheValue = readWriteCacheMap.get(key);
                Value currentCacheValue = readOnlyCacheMap.get(key);
                if (cacheValue != currentCacheValue) {
                    readOnlyCacheMap.put(key, cacheValue);
                }
             }
        }
    };
}

shouldUseReadOnlyResponseCache 指定是否使用 readOnlyCacheMap,默认 true

responseCacheUpdateIntervalMs 指定 readOnlyCacheMap 更新的时间间隔,默认 30s

相关推荐
会飞的土拨鼠呀1 天前
Docker 容器维护手册
docker·容器·eureka
慕伏白1 天前
【慕伏白】Docker 常用指令
docker·容器·eureka
bin91531 天前
使用Docker安装github项目 源码中含有 docker-compose.yml
docker·eureka·github
吠品4 天前
PHP里取月份最后一天的几种方式
java·开发语言·eureka
十年磨一剑~6 天前
docker 为何每次 docker tag才能 docker push
docker·容器·eureka
Tian_Hang10 天前
Eclipse Ditto 物模型相关代码
java·运维·服务器·ide·eureka·eclipse
江畔柳前堤11 天前
第16章:docker企业级实战综合项目
运维·git·安全·docker·容器·eureka
江畔柳前堤11 天前
第11章:Docker 安全加固
运维·git·安全·docker·容器·eureka·github
江畔柳前堤11 天前
第15章:docker故障排查与面试题
大数据·运维·git·elasticsearch·docker·容器·eureka
江畔柳前堤11 天前
第17章:Docker 大厂面试题精选(腾讯/阿里/字节/美团)
运维·网络·spring cloud·docker·容器·eureka