spring-boot - 使用多个 Jedis 集群与 Spring Boot 进行缓存

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

我有 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。

最佳答案

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

@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,您可能必须排除以下自动配置(如果它们妨碍您):RedisAutoConfigurationRedisRepositoriesAutoConfiguration

关于spring-boot - 使用多个 Jedis 集群与 Spring Boot 进行缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48181614/

相关文章:

java - Spring Boot RedisTemplate 等同于 incr 方法

python - 如何忽略第一屈服值?

java - Spring Boot多模块Maven项目@Autowired不工作

java - 将列表对象转换为列表objectDto java的最佳实践

caching - HTML5 缓存 list : what exactly does it do? 文档不清楚

ruby-on-rails - 如何保持 has_many :through relationships when serializing to JSON and back in Rails 4. 0.3?

spring-boot - 为什么 MockMvc 无法在 spring-boot 中测试 @WebServlet

java - 如何将 Java HashMap<String, Pojo> 变量名映射到 typescript

caching - 超出 Neo4j GC 开销限制

asp.net - 为什么(以及如何)将 ASP.NET 缓存存储在非托管内存中?