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

版本 awsVersion = ‘1.11.277’

缓存类型
registryConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>>AbstractInstanceRegistry成员变量
readWriteCacheMapLoadingCacheResponseCacheImpl成员变量
readOnlyCacheMapConcurrentMap<Key, Value>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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值