-
Notifications
You must be signed in to change notification settings - Fork 336
监控统计功能
xiaolyuh123 edited this page Sep 24, 2020
·
3 revisions
Layering Cache 的监控统计功能默认是开启的
直接在声明CacheManager Bean的时候将stats设置成true。
/**
* 多级缓存配置
*
* @author yuhao.wang3
*/
@Configuration
@EnableAspectJAutoProxy
public class CacheConfig {
@Bean
public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
LayeringCacheManager layeringCacheManager = new LayeringCacheManager(redisTemplate);
// 默认开启统计功能
layeringCacheManager.setStats(true);
return layeringCacheManager;
}
...
}
在application.properties文件中添加以下配置即可
layering-cache.stats=true
去实现CacheStatsReportService
接口,然后将实现类注入到CacheManager
中,如下是上报到CAT的示例:
/**
* 多级缓存配置
*
* @author yuhao.wang3
*/
@Configuration
@Import({RedisConfig.class})
@EnableAspectJAutoProxy
public class CacheConfig {
@Bean
public CacheManager layeringCacheManager(RedisClient layeringCacheRedisClient, CacheStatsReportService cacheStatsReportService, LayeringCacheProperties layeringCacheProperties) {
LayeringCacheManager layeringCacheManager = new LayeringCacheManager(layeringCacheRedisClient);
// 默认开启统计功能
layeringCacheManager.setStats(layeringCacheProperties.isStats());
// 上报缓存统计信息
layeringCacheManager.setCacheStatsReportService(cacheStatsReportService);
// 设置缓存命名空间
GlobalConfig.setNamespace(StringUtils.isBlank(layeringCacheProperties.getNamespace()) ? applicationName : layeringCacheProperties.getNamespace());
return layeringCacheManager;
}
// 上报CAT示例
@Bean
public CacheStatsReportService cacheStatsReportService() {
return cacheStatsInfos -> {
for (CacheStatsInfo cacheStatsInfo : cacheStatsInfos) {
Transaction transaction = Cat.newTransaction("layering-cache", cacheStatsInfo.getCacheName());
try {
// 记录一个事件
Cat.logEvent("layering-cache", cacheStatsInfo.getCacheName(), Message.SUCCESS, JSON.toJSONString(cacheStatsInfo));
// 记录一个业务指标
String name = StringUtils.isBlank(cacheStatsInfo.getDepict()) ? cacheStatsInfo.getCacheName() : cacheStatsInfo.getDepict();
Cat.logMetricForSum(name + "-hitRate", cacheStatsInfo.getHitRate(), (int) cacheStatsInfo.getRequestCount());
Cat.logMetricForCount(name + "-firstSize", (int) cacheStatsInfo.getFirstCacheSize());
Cat.logMetricForCount(name + "-requestCount", (int) cacheStatsInfo.getRequestCount());
transaction.setDurationInMillis(cacheStatsInfo.getTotalLoadTime());
transaction.setStatus(Message.SUCCESS);
} catch (Exception e) {
transaction.setStatus(e);
} finally {
transaction.complete();
}
}
};
}
}
直接单独部署layering-cache-web
服务即可。
日志格式:
Layering Cache 统计信息:{"cacheName":"people1","depict":"查询用户信息1","firstCacheMissCount":3,"firstCacheRequestCount":4575,"hitRate":99.9344262295082,"internalKey":"4000-15000-8000","layeringCacheSetting":{"depict":"查询用户信息1","firstCacheSetting":{"allowNullValues":true,"expireMode":"WRITE","expireTime":4,"initialCapacity":10,"maximumSize":5000,"timeUnit":"SECONDS"},"internalKey":"4000-15000-8000","secondaryCacheSetting":{"allowNullValues":true,"expiration":15,"forceRefresh":true,"preloadTime":8,"timeUnit":"SECONDS","usePrefix":true},"useFirstCache":true},"missCount":3,"requestCount":4575,"secondCacheMissCount":3,"secondCacheRequestCount":100,"totalLoadTime":142}
- 如果项目集成了ELK之类的日志框架,那我们可以直接基于以上日志做监控和告警。
- 统计数据每隔一分钟采集一次
- 添加redis配置(需要查看缓存统计的redis服务器)
- 查询缓存命中率
- 缓存详细
作者博客:https://xiaolyuh.blog.csdn.net/
作者邮箱: xiaolyuh@163.com
github 地址:https://github.com/xiaolyuh/layering-cache
微信: