@Configuration
public class RedisClusterConfig {
@Value("${spring.redis.cluster.nodes}")
private String clusterNodes;
@Bean
public JedisCluster getJedisCluster() {
// 截取集群節點
String[] cluster = clusterNodes.split(",");
// 創建set集合
Set<HostAndPort> nodes = new HashSet<HostAndPort>();
// 循環數組把集群節點添加到set集合中
for (String node : cluster) {
String[] host = node.split(":");
//添加集群節點
nodes.add(new HostAndPort(host[0], Integer.parseInt(host[1])));
}
JedisCluster jc = new JedisCluster(nodes);
return jc;
}
}
在application.properties文件中,配置spring.redis.cluster.nodes=
在使用的時候直接得到JedisCluster對象使用