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

相关推荐
@寄居蟹19 小时前
Docker 命令大全
docker·容器·eureka
AI扶我青云志3 天前
Milvus 安装和启动指南
人工智能·云原生·eureka·大模型
楠有枝3 天前
普通用户使用docker命令
spring cloud·docker·eureka
朱皮皮呀4 天前
Spring Cloud——服务注册与服务发现原理与实现
运维·spring cloud·eureka·服务发现·php
Peter·Pan爱编程4 天前
Docker在Linux中安装与使用教程
linux·docker·eureka
春人.5 天前
PortainerCE 跨云管理:cpolar 内网穿透服务实现多环境统一控制
云原生·eureka
水痕017 天前
gin结合minio来做文件存储
java·eureka·gin
斯普信专业组9 天前
Eureka故障处理大汇总
云原生·eureka
奥格列的魔法拖鞋~9 天前
Docker-LNMP架构 创建多项目- 单个ngixn代理多个PHP容器服务
nginx·docker·eureka·架构·php·lnmp
江湖有缘10 天前
【Docker项目实战】使用Docker部署Vikunja任务管理工具
docker·容器·eureka