【问题标题】:Using multiple Jedis clusters for caching with Spring Boot通过 Spring Boot 使用多个 Jedis 集群进行缓存
【发布时间】:2018-01-10 06:37:47
【问题描述】:

我有 2 个绝地缓存:

  • 本地主机:6379
  • cache.servermachine.com:6380,password=abcdef

一个 redis 实例在本地托管,一个在受保护的机器上使用密码。我有一个 Spring Boot 配置类。

public class RedisCacheConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

@Bean
JedisConnectionFactory jedisConnectionFactory() {
    return new JedisConnectionFactory();
}

@Bean
RedisTemplate<Object, Object> redisTemplate() {
    RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    return redisTemplate;
}

@Bean
CacheManager cacheManager() {
    return new RedisCacheManager(redisTemplate());
}
}

在我的application.yml 文件中,如何将spring.redis.cluster 选项修改为具有多个节点,其中一个具有密码?

我正在使用 Jedis 1.9.0。

【问题讨论】:

  • Jedis 没有 1.9.0 版本。也许是 2.9.0?

标签: spring-boot caching redis jedis spring-data-redis


【解决方案1】:

您可以根据需要创建任意数量的连接工厂,使用它们创建多个 RedisTemplate bean,然后创建多个 CacheManager bean:

@Configuration
@EnableConfigurationProperties
public class RedisCacheConfiguration {

    public static class RedisProperties {
        @NotNull @Size(min = 1) private List<String> nodes;
        @NotNull private String password;
        // getters and setters omitted 
    }

    @ConfigurationProperties(prefix = "spring.redis.clusters")
    @Validated
    public static class MultipleRedisProperties {
        @NotNull @Valid private RedisProperties local;
        @NotNull @Valid private RedisProperties remote;
        // getters and setters omitted 
    }

    @Bean MultipleRedisProperties multipleRedisProperties() { 
        return new MultipleRedisProperties (); 
    }

    @Bean JedisConnectionFactory localJedisCF() {
        RedisClusterConfiguration clusterCfg = new RedisClusterConfiguration(redisProperties().getLocal().getNodes());
        JedisConnectionFactory factory = new JedisConnectionFactory(clusterCfg );
        factory.setPassword(redisProperties().getPassword());
        factory.setUsePool(true);

        return factory;
    }

    @Bean RedisTemplate<Object, Object> localRedisTemplate() {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(localJedisCF());
        return redisTemplate;
    }

    @Bean CacheManager localJedisCacheManager() { 
        new RedisCacheManager(redisTemplate()); 
    }

    // similar bean definitions for the remote cluster omitted
}

然后,您的配置如下(YAML 中的示例):

spring.redis.clusters:
  local:
    nodes: localhost:6379
    password:
  remote:
    nodes: cache.servermachine.com:6380
    password: abcdef

您可以将创建的缓存管理器与缓存抽象一起使用:

@Cacheable(key = "#name", cacheManager = "localRedisCacheManager")
public String greet(@PathVariable String name) {
    return "Hello " + name;
}

如果您使用的是 Spring Data Redis,则可能需要排除以下自动配置:RedisAutoConfiguration,RedisRepositoriesAutoConfiguration

【讨论】:

    猜你喜欢
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 2020-12-18
    • 1970-01-01
    • 2020-03-09
    • 2015-10-13
    • 1970-01-01
    相关资源
    最近更新 更多