[英]@ActiveProfiles annotation doesn't work after I upgrade my springboot to version 2.4.2
[英]Redis cache metrics doesn't work in SpringBoot version 2.4.2
我們正在嘗試向 Prometheus 公開我們的 redis 緩存指標。 以下是我們所做的。
我們有一個 class CachingConfig
如下,
@Configuration
@EnableCaching
public class CachingConfig {
private final Duration cacheEntryTtl;
public CachingConfig(
@Value("${spring.cache.redis.entryTtl}")
final Duration cacheEntryTtl
) {
this.cacheEntryTtl = cacheEntryTtl;
}
@Bean
public CacheManager cacheManager(final RedisConnectionFactory redisConnectionFactory) {
final Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
cacheConfigurations.put("cacheA",cacheConfiguration(cacheEntryTtl));
cacheConfigurations.put("cacheB",cacheConfiguration(cacheEntryTtl));
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(cacheConfiguration(cacheEntryTtl))
.withInitialCacheConfigurations(cacheConfigurations)
.build();
}
}
然后我們在我們的 class 中使用 Redis 緩存,如下所示。
public class BusinessService {
public static final String CACHE_A_NAME = "cacheA"
private final BusinessServiceClient businessServiceClient;
private final CacheManager cacheManager;
private final CacheMetricsRegistrar cacheMetricsRegistrar;
@PostConstruct
public void postConstruct() {
final Cache cache = cacheManager.getCache(CACHE_A_NAME);
cacheMetricsRegistrar.bindCacheToRegistry(cache);
}
@Cacheable(cacheNames = CACHE_A_NAME)
public Set<String> getOwnersOfProviderAccount(String para1, String para2) {
return businessServiceClient.getResonponse(para1, para2);
}
}
根據這個,我還在我們的application.properties
文件中添加了以下幾行。
spring.cache.type=redis
spring.cache.redis.enable-statistics=true
所以理論上,Redis 緩存指標應該能夠工作,但是當我從以下 URL 檢查我們的緩存指標時。
GET .../actuator/metrics/cache.gets?tag=name:cacheA
響應總是如下所示,COUNT 總是為零,似乎統計數據不起作用,但我們的 Redis 緩存可以工作。
{
"name":"cache.gets",
"description":"The number of pending requests",
"baseUnit":null,
"measurements":[
{
"statistic":"COUNT",
"value":0.0
}
],
"availableTags":[
{
"tag":"result",
"values":[
"hit",
"pending",
"miss"
]
},
{
"tag":"cache",
"values":[
"cacheA"
]
},
{
"tag":"application",
"values":[
"business-service"
]
},
{
"tag":"cacheManager",
"values":[
"cacheManager"
]
}
]
}
而且,如果我們檢查來自/management/prometheus
的指標,這就是我們得到的,所有值都是零。
# HELP cache_gets_total the number of times cache lookup methods have returned an uncached (newly loaded) value, or null
# TYPE cache_gets_total counter
cache_gets_total{application="business-service",cache="cacheA",cacheManager="cacheManager",name="cacheA",result="miss",} 0.0
cache_gets_total{application="business-service",cache="cacheA",cacheManager="cacheManager",name="cacheA",result="pending",} 0.0
cache_gets_total{application="business-service",cache="cacheA",cacheManager="cacheManager",name="cacheA",result="hit",} 0.0
我在配置 Redis 緩存指標時有什么遺漏的嗎? 謝謝,任何建設性的建議表示贊賞。
您正在自己創建RedisCacheManager
,因此緩存管理器自動配置已退出。 因此, spring.cache.type=redis
是不必要的,更重要的是, spring.cache.redis.enable-statistics=true
將沒有效果。
要啟用RedisCacheManager
的統計信息,請在 cacheManager @Bean
方法中調用構建器的cacheManager
enableStatistics()
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.