Spring Redis(5)Redis集群

Redis集群

Redis Cluster 支持Redi3.0+

连接集群配置



@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class ClusterConfigurationProperties {

    /*
     * spring.redis.cluster.nodes[0] = 127.0.0.1:7379
     * spring.redis.cluster.nodes[1] = 127.0.0.1:7380
     * ...
     */
    List<String> nodes;

    /**
     * Get initial collection of known cluster nodes in format {@code host:port}.
     *
     * @return
     */
    public List<String> getNodes() {
        return nodes;
    }

    public void setNodes(List<String> nodes) {
        this.nodes = nodes;
    }
}

@Configuration
public class AppConfig {

    /**
     * Type safe representation of application.properties
     */
    @Autowired ClusterConfigurationProperties clusterProperties;

    public @Bean RedisConnectionFactory connectionFactory() {

        return new JedisConnectionFactory(
            new RedisClusterConfiguration(clusterProperties.getNodes()));
    }
}

RedisClusterConfiguration同样可以通过文件配置,具有以下主要配置属性
- spring.redis.cluster.nodes: 逗号分隔的host:port对.
- spring.redis.cluster.max-redirects: Number of allowed cluster redirections.

Redis Cluster Connection

Redis Cluster和单节点的行为有很大不同。因为在集群下要将一个key自动分配到跨节点的16384个槽中,因此操作超过1个Key的命令必须保证所有操作的Key在同一个槽中,以避免跨槽执行错误。集群中的一个节点只服务拥有的一组key,访问一个节点的命令将只返回这个节点的数据。因此要访问集群下的所有Key必须至少访问所有已知的Master节点才能正确得到结果。

为了解决这个问题,提供了更高层级的RedisClusterConnection掩盖复杂的操作。

RedisTemplate 和 ClusterOperations

RedisTemplate 为集群的特殊操作提供了ClusterOperations 接口,可通过RedisTemplate.opsForCluster()方法获取。

ClusterOperations clusterOps = redisTemplate.opsForCluster();
clusterOps.shutdown(NODE_7379);    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值