我们正试图将我们的redis缓存指标暴露给Prometheus。下面是我们所做的工作。
我们有一个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();
}
}然后我们在我们的类中使用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);
}
}根据this的说法,我还在我们的application.properties文件中添加了以下行。
spring.cache.type=redis
spring.cache.redis.enable-statistics=true因此,理论上,Redis缓存指标应该能够工作,但是当我从以下URL检查我们的缓存指标时。
GET .../actuator/metrics/cache.gets?tag=name:cacheA响应总是像下面这样,计数总是零,统计似乎不起作用,但我们的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缓存指标时,我是否遗漏了什么?谢谢,任何有建设性的建议都将不胜感激。
发布于 2021-02-26 04:13:22
您将自己创建RedisCacheManager,因此缓存管理器自动配置已停止。因此,spring.cache.type=redis是不必要的,更重要的是,spring.cache.redis.enable-statistics=true不会有任何影响。
要在RedisCacheManager上启用统计信息,请在cacheManager @Bean方法中调用构建器上的enableStatistics()方法。
https://stackoverflow.com/questions/66358386
复制相似问题