电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案

瀏覽:5日期:2023-07-23 09:27:20
背景

項(xiàng)目中,使用@Cacheable進(jìn)行數(shù)據(jù)緩存。發(fā)現(xiàn):當(dāng)redis宕機(jī)之后,@Cacheable注解的方法并未進(jìn)行緩存沖突,而是直接拋出異常。而這樣的異常會導(dǎo)致服務(wù)不可用。

原因分析

我們是通過@EnableCaching進(jìn)行緩存啟用的,因此可以先看@EnableCaching的相關(guān)注釋

Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案

通過@EnableCaching的類注釋可發(fā)現(xiàn),spring cache的核心配置接口為:org.springframework.cache.annotation.CachingConfigurer

/** * Interface to be implemented by @{@link org.springframework.context.annotation.Configuration * Configuration} classes annotated with @{@link EnableCaching} that wish or need to * specify explicitly how caches are resolved and how keys are generated for annotation-driven * cache management. Consider extending {@link CachingConfigurerSupport}, which provides a * stub implementation of all interface methods. * * <p>See @{@link EnableCaching} for general examples and context; see * {@link #cacheManager()}, {@link #cacheResolver()} and {@link #keyGenerator()} * for detailed instructions. * * @author Chris Beams * @author Stephane Nicoll * @since 3.1 * @see EnableCaching * @see CachingConfigurerSupport */public interface CachingConfigurer { /** * Return the cache manager bean to use for annotation-driven cache * management. A default {@link CacheResolver} will be initialized * behind the scenes with this cache manager. For more fine-grained * management of the cache resolution, consider setting the * {@link CacheResolver} directly. * <p>Implementations must explicitly declare * {@link org.springframework.context.annotation.Bean @Bean}, e.g. * <pre class='code'> * Configuration * EnableCaching * public class AppConfig extends CachingConfigurerSupport { * Bean // important! * Override * public CacheManager cacheManager() { * // configure and return CacheManager instance * } * // ... * } * </pre> * See @{@link EnableCaching} for more complete examples. */ CacheManager cacheManager(); /** * Return the {@link CacheResolver} bean to use to resolve regular caches for * annotation-driven cache management. This is an alternative and more powerful * option of specifying the {@link CacheManager} to use. * <p>If both a {@link #cacheManager()} and {@code #cacheResolver()} are set, * the cache manager is ignored. * <p>Implementations must explicitly declare * {@link org.springframework.context.annotation.Bean @Bean}, e.g. * <pre class='code'> * Configuration * EnableCaching * public class AppConfig extends CachingConfigurerSupport { * Bean // important! * Override * public CacheResolver cacheResolver() { * // configure and return CacheResolver instance * } * // ... * } * </pre> * See {@link EnableCaching} for more complete examples. */ CacheResolver cacheResolver(); /** * Return the key generator bean to use for annotation-driven cache management. * Implementations must explicitly declare * {@link org.springframework.context.annotation.Bean @Bean}, e.g. * <pre class='code'> * Configuration * EnableCaching * public class AppConfig extends CachingConfigurerSupport { * Bean // important! * Override * public KeyGenerator keyGenerator() { * // configure and return KeyGenerator instance * } * // ... * } * </pre> * See @{@link EnableCaching} for more complete examples. */ KeyGenerator keyGenerator(); /** * Return the {@link CacheErrorHandler} to use to handle cache-related errors. * <p>By default,{@link org.springframework.cache.interceptor.SimpleCacheErrorHandler} * is used and simply throws the exception back at the client. * <p>Implementations must explicitly declare * {@link org.springframework.context.annotation.Bean @Bean}, e.g. * <pre class='code'> * Configuration * EnableCaching * public class AppConfig extends CachingConfigurerSupport { * Bean // important! * Override * public CacheErrorHandler errorHandler() { * // configure and return CacheErrorHandler instance * } * // ... * } * </pre> * See @{@link EnableCaching} for more complete examples. */ CacheErrorHandler errorHandler();}

該接口errorHandler方法可配置異常的處理方式。通過該方法上的注釋可以發(fā)現(xiàn),默認(rèn)的CacheErrorHandler實(shí)現(xiàn)類是org.springframework.cache.interceptor.SimpleCacheErrorHandler

/** * A simple {@link CacheErrorHandler} that does not handle the * exception at all, simply throwing it back at the client. * * @author Stephane Nicoll * @since 4.1 */public class SimpleCacheErrorHandler implements CacheErrorHandler { @Override public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) { throw exception; } @Override public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) { throw exception; } @Override public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) { throw exception; } @Override public void handleCacheClearError(RuntimeException exception, Cache cache) { throw exception; }}

SimpleCacheErrorHandler類注釋上說明的很清楚:對cache的異常不做任何處理,直接將該異常拋給客戶端。因此默認(rèn)的情況下,redis服務(wù)器異常后,直接就阻斷了正常業(yè)務(wù)

解決方案

通過上面的分析可知,我們可以通過自定義CacheErrorHandler來干預(yù)@Cacheable的異常處理邏輯。具體代碼如下:

public class RedisConfig extends CachingConfigurerSupport { /** * redis數(shù)據(jù)操作異常處理。該方法處理邏輯:在日志中打印出錯誤信息,但是放行。 * 保證redis服務(wù)器出現(xiàn)連接等問題的時候不影響程序的正常運(yùn)行 */ @Override public CacheErrorHandler errorHandler() { return new CacheErrorHandler() { @Override public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {handleRedisErrorException(exception, key); } @Override public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {handleRedisErrorException(exception, key); } @Override public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {handleRedisErrorException(exception, key); } @Override public void handleCacheClearError(RuntimeException exception, Cache cache) {handleRedisErrorException(exception, null); } }; } protected void handleRedisErrorException(RuntimeException exception, Object key) { log.error('redis異常:key=[{}]', key, exception); }}

到此這篇關(guān)于Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案的文章就介紹到這了,更多相關(guān)Spring @Cacheable redis異常內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 棕刚玉_白刚玉_铝酸钙-锐石新材料| 杭州|上海贴标机-百科| 营养师网,营养师考试时间,报名入口—网站首页 | 智能门锁电机_智能门锁离合器_智能门锁电机厂家-温州劲力智能科技有限公司 | Win10系统下载_32位/64位系统/专业版/纯净版下载 | 专注氟塑料泵_衬氟泵_磁力泵_卧龙泵阀_化工泵专业品牌 - 梭川泵阀 | 振动筛-交叉筛-螺旋筛-滚轴筛-正弦筛-方形摇摆筛「新乡振动筛厂家」 | 北京翻译公司_同传翻译_字幕翻译_合同翻译_英语陪同翻译_影视翻译_翻译盖章-译铭信息 | 超声骨密度仪-动脉硬化检测仪器-人体成分分析仪厂家/品牌/价格_南京科力悦 | 营养师网,营养师考试时间,报名入口—网站首页 | 日本细胞免疫疗法_肿瘤免疫治疗_NK细胞疗法 - 免疫密码 | 游泳池设计|设备|配件|药品|吸污机-东莞市太平洋康体设施有限公司 | 小型UV打印机-UV平板打印机-大型uv打印机-UV打印机源头厂家 |松普集团 | 罗氏牛血清白蛋白,罗氏己糖激酶-上海嵘崴达实业有限公司 | 净水器代理,净水器招商,净水器加盟-FineSky德国法兹全屋净水 | 岩棉切条机厂家_玻璃棉裁条机_水泥基保温板设备-廊坊鹏恒机械 | 书法培训-高考书法艺考培训班-山东艺霖书法培训凭实力挺进央美 | 学生作文网_中小学生作文大全与写作指导 | 远程会诊系统-手术示教系统【林之硕】医院远程医疗平台 | 首页|光催化反应器_平行反应仪_光化学反应仪-北京普林塞斯科技有限公司 | 美国HASKEL增压泵-伊莱科elettrotec流量开关-上海方未机械设备有限公司 | 超声波破碎仪-均质乳化机(供应杭州,上海,北京,广州,深圳,成都等地)-上海沪析实业有限公司 | NBA直播_NBA直播免费观看直播在线_NBA直播免费高清无插件在线观看-24直播网 | 控显科技 - 工控一体机、工业显示器、工业平板电脑源头厂家 | 烟气换热器_GGH烟气换热器_空气预热器_高温气气换热器-青岛康景辉 | 小型数控车床-数控车床厂家-双头数控车床 | 企业VI设计_LOGO设计公司_品牌商标设计_【北京美研】 | 吉林污水处理公司,长春工业污水处理设备,净水设备-长春易洁环保科技有限公司 | 润东方环保空调,冷风机,厂房车间降温设备-20年深圳环保空调生产厂家 | ET3000双钳形接地电阻测试仪_ZSR10A直流_SXJS-IV智能_SX-9000全自动油介质损耗测试仪-上海康登 | 密度电子天平-内校-外校电子天平-沈阳龙腾电子有限公司 | 999范文网_优质范文下载写作帮手 | 桁架机器人_桁架机械手_上下料机械手_数控车床机械手-苏州清智科技装备制造有限公司 | 粉末冶金-粉末冶金齿轮-粉末冶金零件厂家-东莞市正朗精密金属零件有限公司 | 硅胶布|电磁炉垫片|特氟龙胶带-江苏浩天复合材料有限公司 | 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | Q361F全焊接球阀,200X减压稳压阀,ZJHP气动单座调节阀-上海戎钛 | 丹尼克尔拧紧枪_自动送钉机_智能电批_柔性振动盘_螺丝供料器品牌 | 电子海图系统-电梯检验系统-智慧供热系统开发-商品房预售资金监管系统 | 密集架-手摇-智能-移动-价格_内蒙古档案密集架生产厂家 | 水成膜泡沫灭火剂_氟蛋白泡沫液_河南新乡骏华消防科技厂家 |