最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年02月19日 08:36:58   作者:神農(nóng)L  
這篇文章主要介紹了Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

背景

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

原因分析

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

通過@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ù)操作異常處理。該方法處理邏輯:在日志中打印出錯(cuò)誤信息,但是放行。
   * 保證redis服務(wù)器出現(xiàn)連接等問題的時(shí)候不影響程序的正常運(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Maven配置文件pom.xml詳解

    Maven配置文件pom.xml詳解

    什么是POM?這篇文章主要介紹了Maven的配置文件pom.xml,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Java equals 方法與hashcode 方法的深入解析

    Java equals 方法與hashcode 方法的深入解析

    面試時(shí)經(jīng)常會(huì)問起字符串比較相關(guān)的問題,比如:字符串比較時(shí)用的什么方法,內(nèi)部實(shí)現(xiàn)如何?hashcode的作用,以及重寫equal方法,為什么要重寫hashcode方法?以下就為大家解答,需要的朋友可以參考下
    2013-07-07
  • Java中ShardingSphere分庫分表實(shí)戰(zhàn)

    Java中ShardingSphere分庫分表實(shí)戰(zhàn)

    我們做項(xiàng)目的時(shí)候,數(shù)據(jù)量比較大,單表千萬級別的,需要分庫分表,本文主要介紹了Java中ShardingSphere分庫分表實(shí)戰(zhàn),感興趣的可以了解一下
    2021-09-09
  • spring?Bean的初始化過程解析

    spring?Bean的初始化過程解析

    這篇文章主要介紹了spring?Bean的初始化過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • Java排序之冒泡排序的實(shí)現(xiàn)與優(yōu)化

    Java排序之冒泡排序的實(shí)現(xiàn)與優(yōu)化

    冒泡排序是一種簡單的交換排序。之所以叫做冒泡排序,因?yàn)槲覀兛梢园衙總€(gè)元素當(dāng)成一個(gè)小氣泡,根據(jù)氣泡大小,一步一步移動(dòng)到隊(duì)伍的一端,最后形成一定對的順序。本文將利用Java實(shí)現(xiàn)冒泡排序,并進(jìn)行一定的優(yōu)化,希望對大家有所幫助
    2022-11-11
  • JavaWeb三大組件之監(jiān)聽器Listener詳解

    JavaWeb三大組件之監(jiān)聽器Listener詳解

    這篇文章主要介紹了JavaWeb三大組件之監(jiān)聽器Listener詳解,在JavaWeb應(yīng)用程序中,Listener監(jiān)聽器是一種機(jī)制,用于監(jiān)聽和響應(yīng)特定的事件,它可以感知并響應(yīng)與應(yīng)用程序相關(guān)的事件,從而執(zhí)行相應(yīng)的邏輯處理,需要的朋友可以參考下
    2023-10-10
  • Java 數(shù)組轉(zhuǎn)List的四種方式小結(jié)

    Java 數(shù)組轉(zhuǎn)List的四種方式小結(jié)

    本文主要介紹了四種將Java數(shù)組轉(zhuǎn)換為List的方法,包括使用Arrays.asList、ArrayList構(gòu)造器、Collections.addAll以及JDK8的Stream,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-10-10
  • Spring和SpringMVC掃描注解類沖突的解決方案

    Spring和SpringMVC掃描注解類沖突的解決方案

    這篇文章主要介紹了Spring和SpringMVC掃描注解類沖突的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java設(shè)計(jì)簡單學(xué)生管理系統(tǒng)

    java設(shè)計(jì)簡單學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)簡單學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Spring Boot ActiveMQ連接池配置過程解析

    Spring Boot ActiveMQ連接池配置過程解析

    這篇文章主要介紹了Spring Boot ActiveMQ連接池配置過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07

最新評論

怀远县| 连州市| 普兰县| 会同县| 遵义市| 涿州市| 新竹县| 囊谦县| 巍山| 普宁市| 调兵山市| 武夷山市| 宜阳县| 玛纳斯县| 卢龙县| 贵溪市| 青海省| 桂平市| 德阳市| 合江县| 高平市| 探索| 关岭| 阳春市| 廊坊市| 彭泽县| 阿图什市| 攀枝花市| 高陵县| 米易县| 泾源县| 呼图壁县| 定边县| 金山区| 富川| 正阳县| 齐齐哈尔市| 雷波县| 永清县| 龙泉市| 龙泉市|