spring boot从1.x升级到2.x之后,原先在spring-boot-actuator里默认的一些metrics不在自动生效,而是将这些metrics的功能从spring-boot-actuator迁移到了micrometer.io项目里,作为一个独立的微服务监控项目维护。
为此,为了使spring cache的相关metrics生效,需要进行如下配置:
management:
endpoints:
web:
exposure:
include: info, health, metrics, caches
spring:
cache:
cache-names: books, rooms
如果不指定cache-names,spring cache metrics是不会生效的,因为spring是在加载的过程中来确认需要对哪些cache来监控,像Cacheable之类需要动态加入的cache,spring在加载过程中无法感知到。
spring:
cache:
cache-names: books, rooms
caffeine:
spec: recordStats
{
"names":[
"jvm.memory.max",
"jvm.threads.states",
"process.files.max",
"jvm.gc.memory.promoted",
"cache.puts",
"cache.size",
"cache.evictions",
"system.load.average.1m",
"jvm.memory.used",
"jvm.gc.max.data.size",
"jvm.gc.pause",
"jvm.memory.committed",
"system.cpu.count",
"logback.events",
"http.server.requests",
"tomcat.global.sent",
"jvm.buffer.memory.used",
"cache.eviction.weight",
"tomcat.sessions.created",
"jvm.threads.daemon",
"system.cpu.usage",
"jvm.gc.memory.allocated",
"tomcat.global.request.max",
"tomcat.global.request",
"cache.gets",
"tomcat.sessions.expired",
"jvm.threads.live",
"jvm.threads.peak",
"tomcat.global.received",
"process.uptime",
"tomcat.sessions.rejected",
"process.cpu.usage",
"tomcat.threads.config.max",
"jvm.classes.loaded",
"jvm.classes.unloaded",
"tomcat.global.error",
"tomcat.sessions.active.current",
"tomcat.sessions.alive.max",
"jvm.gc.live.data.size",
"tomcat.threads.current",
"process.files.open",
"jvm.buffer.count",
"jvm.buffer.total.capacity",
"tomcat.sessions.active.max",
"tomcat.threads.busy",
"process.start.time"
]
}
{
"name":"cache.gets",
"description":"The number of times cache lookup methods have returned a cached value.",
"baseUnit":null,
"measurements":[
{
"statistic":"COUNT",
"value":0
}
],
"availableTags":[
{
"tag":"result",
"values":[
"hit",
"miss"
]
},
{
"tag":"cache",
"values":[
"rooms",
"books"
]
},
{
"tag":"name",
"values":[
"rooms",
"books"
]
},
{
"tag":"cacheManager",
"values":[
"cacheManager"
]
}
]
}
{
"name":"cache.gets",
"description":"The number of times cache lookup methods have returned a cached value.",
"baseUnit":null,
"measurements":[
{
"statistic":"COUNT",
"value":0
}
],
"availableTags":[
{
"tag":"name",
"values":[
"books"
]
},
{
"tag":"cacheManager",
"values":[
"cacheManager"
]
}
]
}
默认coffeine.spec的配置是对全体配置的,如果要分开配置可以自定义实现CacheManager,参见common-spring-cache-configurer. 使用时,直接引入该jar包即可。
com.iqiyi.intl.common
common-spring-cache-configurer
1.4.0-SNAPSHOT
使用配置如下(替换掉原先的spring.cache的配置):
cache.items:
- name: books
spec: recordStats,softValues, maximumSize=1,expireAfterWrite=100s
- name: rooms
spec: expireAfterWrite=50s, maximumSize=10000
CacheManager自定义实现如下:
@Configuration
@ConditionalOnClass({ Caffeine.class, CaffeineCacheManager.class })
@EnableConfigurationProperties(CacheProperties.class)
public class AutoCustomizedCaffeineCacheConfigurer {
@Autowired
private CacheProperties cacheProperties;
@Bean
@Primary
public CacheManager caffeineCacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<CaffeineCache> caches = cacheProperties.getItems().stream()
.map(item -> new CaffeineCache(item.getName(), Caffeine.from(item.getSpec()).build()))
.collect(Collectors.toList());
cacheManager.setCaches(caches);
return cacheManager;
}
}
可以这么理解:
首先通过size或者weight来确定缓存总共占用的最大空间,softValues是用于兜底策略,防止万一size/weight设置的不正确,导致的OOM。对于ttl/tti,则是针对业务场景,来保证数据的时效性,用于程序运行的正确性。
举例如下:
cache.items:
- name: books
spec: softValues, maximumSize=1,expireAfterWrite=100s
说明: