Spring Redis(5)Redis集群

快来打我* 2022-06-17 00:19 253阅读 0赞

Redis集群

Redis Cluster 支持Redi3.0+

连接集群配置

  1. @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()方法获取。

  1. ClusterOperations clusterOps = redisTemplate.opsForCluster();
  2. clusterOps.shutdown(NODE_7379);

发表评论

表情:
评论列表 (有 0 条评论,253人围观)

还没有评论,来说两句吧...

相关阅读

    相关 redis5安装

    1、简要说明 2018年十月 Redis 发布了稳定版本的 5.0 版本,推出了各种新特性,其中一点是**放弃 Ruby的集群方式**,改为 使用 **C语言编写的 ...