栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Cloud Loadbalancer 修改默认缓存为Caffeine,修改微服务启动关于Loadbalancer的WARN

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Cloud Loadbalancer 修改默认缓存为Caffeine,修改微服务启动关于Loadbalancer的WARN

一、背景描述
[mate-uaa:192.168.3.9:20001] 2022-01-22 23:27:52.907 WARN 17966 [] [main] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.

在微服务引入loadbalancer的包,如下所示:


    org.springframework.cloud
    spring-cloud-starter-loadbalancer

默认情况下未做任何设置在启动的时候,就会出现篇首的告警信息。

二、建议优化 2.1 引入Caffeine依赖

    com.github.ben-manes.caffeine
    caffeine
    3.0.5

2.2 项目中增加配置
spring:
  cloud:
	# 负载均衡器缓存
    loadbalancer:
      cache:
        enabled: true
        caffeine:
          spec: initialCapacity=500,expireAfterWrite=5s
2.3 自定义缓存配置代码(非必须)
package org.springframework.cloud.loadbalancer.cache;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier;
import org.springframework.util.StringUtils;

public class CaffeinebasedLoadBalancerCacheManager extends CaffeineCacheManager implements LoadBalancerCacheManager {
    public CaffeinebasedLoadBalancerCacheManager(String cacheName, LoadBalancerCacheProperties properties) {
        super(new String[]{cacheName});
        if (!StringUtils.isEmpty(properties.getCaffeine().getSpec())) {
            this.setCacheSpecification(properties.getCaffeine().getSpec());
        } else {
            this.setCaffeine(Caffeine.newBuilder().initialCapacity(properties.getCapacity()).expireAfterWrite(properties.getTtl()).softValues());
        }

    }

    public CaffeinebasedLoadBalancerCacheManager(LoadBalancerCacheProperties properties) {
        this(CachingServiceInstanceListSupplier.SERVICE_INSTANCE_CACHE_NAME, properties);
    }
}

其中

this.setCaffeine(Caffeine.newBuilder().initialCapacity(properties.getCapacity()).expireAfterWrite(properties.getTtl()).softValues());

就是使用默认的缓存配置信息对Caffeine进行配置

微服务平台推荐

matecloud微服务平台

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/711119.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号