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

版本 awsVersion = ‘1.11.277’

缓存 类型
registry ConcurrentHashMap>> AbstractInstanceRegistry成员变量
readWriteCacheMap LoadingCache ResponseCacheImpl成员变量
readOnlyCacheMap ConcurrentMap ResponseCacheImpl成员变量

registry

// 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

// 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

// 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

你可能感兴趣的:(eureka,eureka)