欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

原因分析

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

通過(guò)@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方法可配置異常的處理方式。通過(guò)該方法上的注釋可以發(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類注釋上說(shuō)明的很清楚:對(duì)cache的異常不做任何處理,直接將該異常拋給客戶端。因此默認(rèn)的情況下,redis服務(wù)器異常后,直接就阻斷了正常業(yè)務(wù)

解決方案

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

public class RedisConfig extends CachingConfigurerSupport {

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

相關(guān)文章

  • 淺談Java設(shè)置PPT幻燈片背景——純色、漸變、圖片背景

    淺談Java設(shè)置PPT幻燈片背景——純色、漸變、圖片背景

    這篇文章主要介紹了Java設(shè)置PPT幻燈片背景——純色、漸變、圖片背景,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 詳解Java修飾符

    詳解Java修飾符

    Java語(yǔ)言提供了很多修飾符,主要分為以下兩類:訪問(wèn)修飾符;非訪問(wèn)修飾符。修飾符用來(lái)定義類、方法或者變量,通常放在語(yǔ)句的最前端。我們通過(guò)下面的例子來(lái)說(shuō)明,下面就跟小編一起來(lái)看下吧
    2016-12-12
  • 關(guān)于eclipse安裝spring插件報(bào)錯(cuò)An error occurred while collecting items to be installed...解決方案

    關(guān)于eclipse安裝spring插件報(bào)錯(cuò)An error occurred while collecting item

    這篇文章主要介紹了關(guān)于eclipse安裝spring插件報(bào)錯(cuò)An error occurred while collecting items to be installed...解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Java根據(jù)日期計(jì)算星期幾的四種方法

    Java根據(jù)日期計(jì)算星期幾的四種方法

    在我們?nèi)粘I(yè)務(wù)代碼中,經(jīng)常要用到星期幾,下面這篇文章主要給大家介紹了關(guān)于Java根據(jù)日期計(jì)算星期幾的四種方法,文中通過(guò)代碼將每種實(shí)現(xiàn)的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • Mybatis查詢Sql結(jié)果未映射到對(duì)應(yīng)得實(shí)體類上的問(wèn)題解決

    Mybatis查詢Sql結(jié)果未映射到對(duì)應(yīng)得實(shí)體類上的問(wèn)題解決

    使用mybatis查詢表數(shù)據(jù)得時(shí)候,發(fā)現(xiàn)對(duì)應(yīng)得實(shí)體類字段好多都是null,本文主要介紹了Mybatis查詢Sql結(jié)果未映射到對(duì)應(yīng)得實(shí)體類上的問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • Java項(xiàng)目如何防止SQL注入(多種方案匯總)

    Java項(xiàng)目如何防止SQL注入(多種方案匯總)

    SQL注入即是指web應(yīng)用程序?qū)τ脩糨斎霐?shù)據(jù)的合法性沒(méi)有判斷或過(guò)濾不嚴(yán),攻擊者可以在web應(yīng)用程序中事先定義好的查詢語(yǔ)句的結(jié)尾上添加額外的SQL語(yǔ)句,這篇文章主要介紹了?Java項(xiàng)目防止SQL注入的四種方案,需要的朋友可以參考下
    2023-12-12
  • 使用Java自定義注解實(shí)現(xiàn)一個(gè)簡(jiǎn)單的令牌桶限流器

    使用Java自定義注解實(shí)現(xiàn)一個(gè)簡(jiǎn)單的令牌桶限流器

    限流是在分布式系統(tǒng)中常用的一種策略,它可以有效地控制系統(tǒng)的訪問(wèn)流量,保證系統(tǒng)的穩(wěn)定性和可靠性,在本文中,我將介紹如何使用Java自定義注解來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的令牌桶限流器,需要的朋友可以參考下
    2023-10-10
  • Java中在控制臺(tái)讀取字符的實(shí)現(xiàn)示例

    Java中在控制臺(tái)讀取字符的實(shí)現(xiàn)示例

    Scanner是Java中的一個(gè)類,可以用于讀取控制臺(tái)輸入,通過(guò)Scanner對(duì)象可以方便地從控制臺(tái)讀取數(shù)字或字符串,本文就來(lái)介紹一下Java中在控制臺(tái)讀取字符的實(shí)現(xiàn)示例,感興趣的可以了解一下
    2023-10-10
  • Java將json對(duì)象轉(zhuǎn)換為map鍵值對(duì)案例詳解

    Java將json對(duì)象轉(zhuǎn)換為map鍵值對(duì)案例詳解

    這篇文章主要介紹了Java將json對(duì)象轉(zhuǎn)換為map鍵值對(duì)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • MyBatis逆向工程生成dao層增刪改查的操作

    MyBatis逆向工程生成dao層增刪改查的操作

    這篇文章主要介紹了MyBatis逆向工程生成dao層增刪改查的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評(píng)論