網站首頁 編程語言 正文
Spring Cloud Loadbalancer 修改默認緩存為Caffeine,修改微服務啟動關于Loadbalancer的WARN
作者:MateCloud微服務 更新時間: 2022-05-17 編程語言一、背景描述
[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的包,如下所示:
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-loadbalancerartifactId>
dependency>
默認情況下未做任何設置在啟動的時候,就會出現篇首的告警信息。
二、建議優化
2.1 引入Caffeine依賴
<dependency>
<groupId>com.github.ben-manes.caffeinegroupId>
<artifactId>caffeineartifactId>
<version>3.0.5version>
dependency>
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微服務平臺
原文鏈接:https://matecloud.blog.csdn.net/article/details/122645539
相關推薦
- 2022-03-14 如何在Git hub上快速查找合適的項目(如何在github上找到自己想要的代碼)
- 2023-10-16 Element UI日期組件-選擇月份具體到當月最后一天
- 2022-08-01 flask上使用websocket的方法示例_python
- 2022-12-03 .Net?Core和RabbitMQ限制循環消費的方法_實用技巧
- 2022-04-07 C#實現Socket服務器及多客戶端連接的方式_C#教程
- 2022-10-05 Python數據可視化制作全球地震散點圖_python
- 2023-05-09 Linux?解壓縮文件到指定目錄_linux shell
- 2022-10-07 關于pyqt5彈出提示框的詳細介紹_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支