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);
还没有评论,来说两句吧...