springboot集成JedisCluster

application.properties设置:

spring.redis.cluster.nodes=ip:6381,ip:6382,ip:6383,ip:6384,ip:6385,ip:6386
#以下为redis集群自定义属性
spring.redis.cluster.password=password
#连接超时时间
spring.redis.cluster.connectionTimeout=3000
#读取数据超时时间
spring.redis.cluster.soTimeout=1000
spring.redis.cluster.maxAttempts=3
#以下为连接池自定义属性
spring.redis.pool.max-total=1000
spring.redis.pool.max-idle=500
spring.redis.pool.min-idle=10
RedisClusterConfig配置文件:

RedisClusterConfig配置文件:

@Configuration
public class RedisClusterConfig {
	@Value("${spring.redis.cluster.nodes}")
	private String nodes;
	@Value("${spring.redis.cluster.password}")
	private String password;
	@Value("${spring.redis.cluster.connectionTimeout}")
	private int connectionTimeout;
	@Value("${spring.redis.cluster.soTimeout}")
	private int soTimeout;
	@Value("${spring.redis.cluster.maxAttempts}")
	private int maxAttempts;
	@Value("${spring.redis.pool.max-total}")
	private int maxTotal;
	@Value("${spring.redis.pool.max-idle}")
	private int maxIdle;
	@Value("${spring.redis.pool.min-idle}")
	private int minIdle;
	@Bean
	public JedisCluster getJedisCluster() {
		 String[] redisnodes = nodes.split(",");
	        Set<HostAndPort> nodes = new HashSet<>();
	        for(String node:redisnodes) {
	            String[] arr=node.split(":");
	            HostAndPort hostAndPort = new HostAndPort(arr[0], Integer.parseInt(arr[1]));
	            nodes.add(hostAndPort);
	        }
	        JedisPoolConfig config = new JedisPoolConfig();
	        config.setMaxTotal(maxTotal);
	        config.setMaxIdle(maxIdle);
	        config.setMinIdle(minIdle);
	        JedisCluster cluster = new JedisCluster(nodes, connectionTimeout, soTimeout, maxAttempts, password, config);
	        return cluster;

	}
}

你可能感兴趣的:(数据库,java,redis)