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

相关推荐
十二同学啊2 天前
Docker 容器化技术详解:镜像、容器、网络与应用部署设计
网络·docker·eureka
江湖有缘2 天前
Docker实战 | 使用Docker部署Readeck书签管理工具
java·docker·eureka
江湖有缘2 天前
Docker实战 | 使用Docker部署Foxel私有云盘服务
docker·容器·eureka
Brilliantee4043 天前
docker迁移SOP
docker·容器·eureka
2501_933923254 天前
SpringCloud组件:Eureka服务注册与发现
java·开发语言·后端·spring cloud·eureka
灯澜忆梦6 天前
【docker】#2 | 基础命令
docker·容器·eureka
蜡笔婧萱6 天前
Docker介绍及其基础简介(一)
云原生·eureka
Wang's Blog10 天前
Java框架 SpringCloud 快速入门: Eureka 服务发现与服务名调用改造
java·spring cloud·eureka
Wang's Blog10 天前
Java框架 SpringCloud 快速入门: Eureka 注册中心原理分析
java·spring cloud·eureka
Wang's Blog10 天前
Java框架 SpringCloud 快速入门: Eureka 服务注册
java·spring cloud·eureka