Spring Cache監(jiān)控配置與使用規(guī)范的建議
建議
- 程序中使用的緩存,請在cache-names里指明,如此,可以通過配置文件來明白程序中用到了哪些spring cache。
- 請盡量對每一個cache分別設置緩存策略,因為不用的cache其使用的場景與緩存對象大小都不一樣。分別設置緩存請使用common-spring-cache-configuere。默認spring不支持。
- 緩存策略建議閱讀緩存策略最佳配置
- Cacheable的sync無特殊情況都設置為true,這樣,取數(shù)據(jù)時有類似LoadingCache的效果,同時利用computeIfAbsent方法實現(xiàn)了線程安全,也防止雪崩問題。
spring boot 2.x 監(jiān)控設置
spring boot從1.x升級到2.x之后,原先在spring-boot-actuator里默認的一些metrics不在自動生效,而是將這些metrics的功能從spring-boot-actuator遷移到了micrometer.io項目里,作為一個獨立的微服務監(jiān)控項目維護。
為此,為了使spring cache的相關metrics生效,需要進行如下配置:
1.在management.endpoints.web里添加caches與metrics兩個接口暴露
默認只暴露health、info。
management: endpoints: web: exposure: include: info, health, metrics, caches
2.在spring.cache里指定cache-names的配置
spring: cache: cache-names: books, rooms
如果不指定cache-names,spring cache metrics是不會生效的,因為spring是在加載的過程中來確認需要對哪些cache來監(jiān)控,像Cacheable之類需要動態(tài)加入的cache,spring在加載過程中無法感知到。
3.對cache provider啟動調(diào)用記錄功能
以caffeine為例,在spec中加入recordStats,如下:
spring: cache: cache-names: books, rooms caffeine: spec: recordStats
4.相關url
- /actuator/caches 獲取cache manager內(nèi)的cache實例
- /actuator/metrics/cache.gets 獲取cache內(nèi)緩存對象的情況,包括hit/miss的情況,具體參考tags drillDown的使用方式https://docs.spring.io/spring-boot/docs/2.0.0.BUILD-SNAPSHOT/actuator-
- api//html/#metrics-drilling-down
- /actuator/metrics/cache.puts
- /actuator/metrics/cache.eviction
5.舉例
curl localhost:8080/actuator/metrics/ 訪問所有支持的metrics
{ "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" ] }
curl localhost:8080/actuator/metrics/cache.gets 訪問緩存get情況
{ "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" ] } ] }
curl “localhost:8080/actuator/metrics/cache.gets?tag=result:hit&tag=cache:books” 獲取名為books的cache的命中次數(shù)
{ "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的配置是對全體配置的,如果要分開配置可以自定義實現(xiàn)CacheManager,參見common-spring-cache-configurer. 使用時,直接引入該jar包即可。
<dependency> <groupId>com.iqiyi.intl.common</groupId> <artifactId>common-spring-cache-configurer</artifactId> <version>1.4.0-SNAPSHOT</version> </dependency>
使用配置如下(替換掉原先的spring.cache的配置):
cache.items: - name: books spec: recordStats,softValues, maximumSize=1,expireAfterWrite=100s - name: rooms spec: expireAfterWrite=50s, maximumSize=10000
CacheManager自定義實現(xiàn)如下:
@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; } }
緩存策略最佳配置
- 設置softvalues, 當JVM內(nèi)存耗盡時,會觸發(fā)GC,回收這些cache的內(nèi)存,防止OOM。
- 設置size或weight,限定緩存的最大占用空間,保證系統(tǒng)正常運行。
- 設置ttl/tti,設置過期時間,根據(jù)業(yè)務實際場景決定時長。
可以這么理解:
首先通過size或者weight來確定緩存總共占用的最大空間,softValues是用于兜底策略,防止萬一size/weight設置的不正確,導致的OOM。對于ttl/tti,則是針對業(yè)務場景,來保證數(shù)據(jù)的時效性,用于程序運行的正確性。
舉例如下:
cache.items: - name: books spec: softValues, maximumSize=1,expireAfterWrite=100s
說明:
- 所有的配置都是針對每一個cache生效的,對于cacheManager, 也就是會有多個cache,所以占用的內(nèi)容空間也會增加多份。所以cache不能設置過大。
- ttl/tti說明,ttl (time to live), 即expireAfterWrite;tti (time to idle), 即expireAfterRead, 如果設置tti,則意味著,只要這個key直接被讀到,則緩存會一直不失效,所以請慎用tti。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot?SpringSecurity?詳細介紹(基于內(nèi)存的驗證)
這篇文章主要介紹了SpringBoot?SpringSecurity?介紹(基于內(nèi)存的驗證),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04SpringBoot SpEL語法掃盲與查詢手冊的實現(xiàn)
這篇文章主要介紹了SpringBoot SpEL語法掃盲與查詢手冊的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05JUC并發(fā)編程LinkedBlockingQueue隊列深入分析源碼
LinkedBlockingQueue 是一個可選有界阻塞隊列,這篇文章主要為大家詳細介紹了Java中LinkedBlockingQueue的實現(xiàn)原理與適用場景,感興趣的可以了解一下2023-04-04解決spring @ControllerAdvice處理異常無法正確匹配自定義異常
這篇文章主要介紹了解決spring @ControllerAdvice處理異常無法正確匹配自定義異常的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06java并發(fā)學習之BlockingQueue實現(xiàn)生產(chǎn)者消費者詳解
這篇文章主要介紹了java并發(fā)學習之BlockingQueue實現(xiàn)生產(chǎn)者消費者詳解,具有一定參考價值,需要的朋友可以了解下。2017-11-11