2026-01-06T14:37:35.549+08:00 WARN 112300 --- [gateway-service] [ main] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. While this cache implementation is useful for development and tests, it's recommended to use Caffeine cache in production.You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
Spring Cloud Gateway(或其他使用了 spring-cloud-starter-loadbalancer 的服务)正在使用 Spring Cloud LoadBalancer 的默认缓存机制。Spring 官方建议:在生产环境中,应该使用 Caffeine 缓存来替代默认缓存,以获得更好的性能。
详细解释
发生了什么?
Spring Cloud LoadBalancer(客户端负载均衡器)需要缓存服务实例列表。
目前它使用的是默认的实现(通常是一个简单的
DefaultLoadBalancerCache)。这个默认实现虽然能满足开发和简单测试的需求,但在高并发、大规模的生产环境下,其性能(读写速度、淘汰策略)不如 Caffeine。
影响是什么?
功能上: 没有任何影响。你的服务可以正常启动,服务发现和负载均衡功能也能正常工作。
性能上: 在极高的流量下,默认缓存的效率可能不如 Caffeine,可能导致微服务间的调用延迟略微增加。
解决方案
根据日志提示,你只需要在项目中添加 Caffeine 的相关依赖,Spring Boot 就会自动切换到 Caffeine 缓存管理器。
如果你是 Maven 项目
在 pom.xml 中添加以下依赖:
<!-- Spring Cache 抽象 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Caffeine 具体实现 -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
评论