【问题标题】:Make Redis as optional将 Redis 设为可选
【发布时间】:2020-09-18 09:32:02
【问题描述】:

我正在使用带有 Redis 的 Spring Boot。Redis 作为 Docker 容器运行

spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379 

Redis 是一个内存数据库,如果它根据 key 在 Redis 中找到数据,则从 Redis 中检索到,否则进入实际的 db 调用。 当 Redis 运行时,代码工作正常。但有时出于任何原因,如果 Redis 出现故障,我会收到异常 RedisConnectionException: Unable to connect to localhost:6379

我想让它成为可选的。如果它出现故障,代码应该通过调用实际的 DB 数据(sql 服务存储库)按原样工作。

有没有办法让 Redis 调用成为可选的。

if running, work with Redis, 
if down, can to actual DB without exception.

我的代码

@Cacheable(cacheNames = CACHE_USER_DETAILS)
    public User getUserDetails(String username) {
      //call data from sql serever via repositories.
}

【问题讨论】:

  • 您可以捕获异常并继续前进。
  • 如果配置reddis为数据源,可以尝试使用spring.datasource.continue-on-error=true
  • 捕获异常将不起作用。我没有从存储库数据库中获取数据。数据库作为数据源:不,我没有使用 redis 作为数据源。我正在使用@cacheble

标签: java spring spring-boot redis


【解决方案1】:

由于您使用@Cachaeble spring 抽象进行缓存,请编写一个实现 CacheErrorHandler 接口的类。您可以覆盖它的方法并执行您的逻辑方面(例如记录错误)。

如果redis宕机,getUserDetails(String username )会自动完成。

查看this question

希望这会有所帮助。

【讨论】:

  • 我会试试这个
【解决方案2】:

我通过创建自己的错误处理程序并覆盖了 spring 缓存错误处理程序来解决问题

package com.crif.credity.tenancy;

import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Configuration;

import com.crif.credity.config.CredityKeyGenerator;

import lombok.extern.slf4j.Slf4j;

@Configuration
public class CachingConfiguration extends CachingConfigurerSupport {
    
    @Override
    public KeyGenerator keyGenerator() {
        return new CredityKeyGenerator();
    }
    
    @Override
    public CacheErrorHandler errorHandler() {
        return new CredityRedisCacheErrorHandler();
    }

    
    @Slf4j
    public static class CredityRedisCacheErrorHandler implements CacheErrorHandler {

        @Override
        public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
            log.info("Unable to get from cache " + cache.getName() + " : " + exception.getMessage());
        }

        @Override
        public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
            log.info("Unable to put into cache " + cache.getName() + " : " + exception.getMessage());
        }

        @Override
        public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
            log.info("Unable to evict from cache " + cache.getName() + " : " + exception.getMessage());
        }

        @Override
        public void handleCacheClearError(RuntimeException exception, Cache cache) {
            log.info("Unable to clean cache " + cache.getName() + " : " + exception.getMessage());
        }
    }
    
    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-14
    • 2016-01-19
    • 2017-11-23
    • 2018-02-20
    • 2018-06-23
    • 2012-01-30
    • 2014-01-21
    • 1970-01-01
    相关资源
    最近更新 更多