1 配置文件
redis.properties
##配置redis集群
redis.cluster=192.168.126.129:7000,192.168.126.129:7001,192.168.126.129:7002,192.168.126.129:7003,192.168.126.129:7004,192.168.126.129:7005
交给spring管理
package com.jt.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class JedisConfig {
@Value("${redis.cluster")
private String cluster;
@Bean
public JedisCluster jedisCluster() {
Set<HostAndPort> set = new HashSet<>();
if (cluster != null) {
String[] list = cluster.split(",");
for (String node : list) {
String[] param = node.split(":");
String host = param[0];
Integer port = Integer.valueOf(param[1]);
set.add(new HostAndPort(host, port));
}
}
return new JedisCluster(set);;
}
}