網站首頁 編程語言 正文
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-08-16 Kotlin空安全空類型淺談_Android
- 2024-01-11 String數組轉List的三種方式
- 2022-09-03 pandas?如何保存數據到excel,csv_python
- 2022-09-22 lex yacc與C++編寫代碼解析字符串代碼示例
- 2023-01-09 Android自定義ViewGroup實現九宮格布局_Android
- 2022-08-20 Python中range()與np.arange()的具體使用_python
- 2022-05-21 基于C++實現信息管理系統_C 語言
- 2022-11-22 sqlserver給表添加新字段、給表和字段添加備注、更新備注及查詢備注(sql語句)_MsSql
- 最近更新
-
- 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同步修改后的遠程分支