官方提供了结合 Prometheus 的方案,如果想简单看下监控,可继续看下文。
直奔主题
@Api(tags = "监控-Caffeine")
@RestController
@RequestMapping("monitor/caffeine")
public class MonitorCaffeineController {
@Resource
private CacheManager caffeine;
@GetMapping("cacheNames")
@ApiOperation("所有缓存name")
public Collection`<String>` cacheNames() {
return caffeine.getCacheNames();
}
@GetMapping("stats")
@ApiOperation("根据缓存name查询缓存监控信息")
public Map`<String, Object>` stats(@RequestParam String cacheName) {
CaffeineCache caffeineCache = (CaffeineCache) caffeine.getCache(cacheName);
CacheStats stats = CacheStats.empty();
if (caffeineCache != null) {
stats = caffeineCache.getNativeCache().stats();
}
Map`<String, Object>` map = new HashMap<>(16);
map.put("请求次数", stats.requestCount());
map.put("命中次数", stats.hitCount());
map.put("未命中次数", stats.missCount());
map.put("加载成功次数", stats.loadSuccessCount());
map.put("加载失败次数", stats.loadFailureCount());
map.put("加载失败占比", stats.loadFailureRate());
map.put("加载总耗时", stats.totalLoadTime());
map.put("回收总次数", stats.evictionCount());
map.put("回收总权重", stats.evictionWeight());
return map;
}
}
2020年12月3日大约 3 分钟