【问题标题】:How to disable Redis cache with Spring Boot?如何使用 Spring Boot 禁用 Redis 缓存?
【发布时间】:2019-05-09 11:09:51
【问题描述】:

在 Spring Boot 2.1 中,我在配置文件中使用 Java 配置定义了一个 RedisCacheManager bean。一切正常,但有时我想禁用它,例如在测试中。 Spring Boot 提供了spring.cache.type=NONE 来禁用缓存,根据这个documentation。但是,此属性将不起作用,因为我已经定义了一个 CacheManager,因此 Spring Boot 不会配置我想要的 NoOpCacheManager(NoOpCacheConfiguration 上有一个@ConditionalOnMissingBean(CacheManager.class),其优先级低于RedisCacheConfiguration)。

在定义缓存时,无论提供者是什么(例如 Caffeine),我们通常将它们定义为 bean,然后由 Spring Boot 的自动配置解析为 SimpleCacheManager。 比如

    @Bean
    public Cache myCache() {
        return new CaffeineCache(
                "my-cache",
                Caffeine.newBuilder()
                        .maximumSize(10)
                        .build());
    }

不幸的是,Redis 无法做到这一点,因为它的 Cache 实现 RedisCache 是不公开的。

我们喜欢做的另一件事是定义一个 bean CacheManagerCustomizer<?>,例如使用 Caffeine

    @Bean
    public CacheManagerCustomizer<CaffeineCacheManager> caffeineCacheManager() {
        return cacheManager -> cacheManager
                .setCaffeine(Caffeine.newBuilder()
                        .expireAfterWrite(1, TimeUnit.MINUTES));
    }

再一次,这在 Redis 中是不可能的,因为 RedisCacheManager不可变的

所以目前唯一的解决方案是创建我们自己的 RedisCacheManager,但这会阻止 spring.cache.type: NONE 的使用。

所以这是我的问题。使用 Spring Boot 配置 Redis 缓存以便我们可以根据需要禁用它的最佳方法是什么?

【问题讨论】:

  • 您可以使用@Profile("!test")忽略测试配置文件中不需要的每个Bean

标签: spring spring-boot spring-test spring-data-redis spring-cache


【解决方案1】:

我需要从 spring.cache.type 属性启用/禁用 Redis 自动配置。下面的代码解决了我的问题。这可能对任何想通过更改单个属性来禁用/启用 redis 的人有所帮助,在我的例子中是 spring.cache.type=redis。以下是主要配置。

@SpringBootApplication(exclude = {RedisAutoConfiguration.class})
public class SpringBootApp extends SpringBootServletInitializer {

}
@ConditionalOnProperty(prefix = "spring", name = "cache.type", havingValue = "redis")
@Configuration
@Import({ RedisAutoConfiguration.class })
public class ApplicationRedisConfig {

}

application.yaml启用自动配置

spring:
  cache:
    type: redis
  redis.host: redis

当 redis 不可用时,健康检查会给出以下响应,这表明已包含自动配置。

{
  "status": "DOWN",
  "details": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 486730272768,
        "free": 216405499904,
        "threshold": 10485760
      }
    },
    "db": {
      "status": "UP",
      "details": {
        "database": "PostgreSQL",
        "hello": 1
      }
    },
    "elasticsearch": {
      "status": "UP"
    },
    "redis": {
      "status": "DOWN",
      "details": {
        "error": "org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to redis:6379"
      }
    }
  }
}

application.yaml禁用自动配置

spring:
  cache:
    type: simple
  redis.host: redis

运行状况检查给出以下响应,显示 redis 已从自动配置中排除。

{
  "status": "UP",
  "details": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 486730272768,
        "free": 215928782848,
        "threshold": 10485760
      }
    },
    "db": {
      "status": "UP",
      "details": {
        "database": "PostgreSQL",
        "hello": 1
      }
    },
    "elasticsearch": {
      "status": "UP"
    }
  }
}

【讨论】:

  • 非常简洁明了的解决方案,谢谢!
【解决方案2】:

@SpringBootApplication 注释的排除属性,例如:@SpringBootApplication( exclude = { RedisAutoConfiguration.class } )

和 设置:spring.data.redis.repositories.enabled=false

【讨论】:

  • 我不希望依赖配置排除,因为它可能会破坏我们应用程序的某些部分。例如,如果我们想在某处注入RedisCacheManager(无论出于何种原因),应用程序都会失败。 spring.cache.type: NONE 让我们这样做
  • 好的,谢谢。所以最后,我添加了 3 个属性来使它工作:spring.cache.type: simple, spring.data.redis.repositories.enabled: false, spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration。我还添加了一个CacheManager bean 来测试配置以配置缓存(以及将@ConditionalOnProperty(name = "redis.host") 添加到生产配置中)
猜你喜欢
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多