spring boot集成redisTemplate redis集群-极简版(非哨兵)

    首先自己平台组搭建好redis集群~给一堆ip 三主三从

1.导包


org.springframework.boot
spring-boot-starter-data-redis

redis.clients
jedis
2.9.0

2.配置yml文件 

redis:
open: true # 是否开启redis缓存 true开启 false关闭
database: 0
host: **.***.**.***:6379,***.**.***.**:6380,***.***.***.***:6379l略
password: ***** # 密码(默认为空)
timeout: 6000 # 连接超时时长(毫秒)
jedis:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
3.配置config
package com.huatai.server.config;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

@Configuration
@ConditionalOnClass({JedisCluster.class})
public class RedisAutoConfiguration {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int connectionTimeout;
/* @Value("${redis.so_timeout:2000}")
private int soTimeout;
@Value("${redis.max_attempts:10}")
private int maxAttempts;*/
@Value("${spring.redis.jedis.pool.max-active}")
private int maxTotal;
@Value("${spring.redis.jedis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private int maxWaitMillis;

@Bean(name = "myRedisClusterConfiguration")
public RedisClusterConfiguration jedisCluster(){
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
String [] hosts = host.split(",");
Set nodeList = new HashSet();
for (String hostAndPort : hosts){
String [] hostOrPort = hostAndPort.split(":");
nodeList.add(new RedisNode(hostOrPort[0],Integer.parseInt(hostOrPort[1])));
}
redisClusterConfiguration.setClusterNodes(nodeList);
redisClusterConfiguration.setMaxRedirects();
return redisClusterConfiguration;
}

@Bean(name = "myJedisPoolConfig")
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(this.maxIdle);
poolConfig.setMinIdle(this.minIdle);
poolConfig.setTestOnCreate(true);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
return poolConfig;
}

@Bean(name = "MyJedisConnectionFactory")
public JedisConnectionFactory getJedisConnectionFactory(@Qualifier("myRedisClusterConfiguration") RedisClusterConfiguration redisClusterConfiguration,
@Qualifier("myJedisPoolConfig") JedisPoolConfig jedisPoolConfig){
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, jedisPoolConfig);
// 判断密码是否存在,存在设置值
checkPasswordIfNull(jedisConnectionFactory);
return jedisConnectionFactory;
}



@Bean(name = "myRedisTemplates")
public RedisTemplate redisTemplate(@Qualifier("MyJedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory){
StringRedisTemplate myRedisTemplate = new StringRedisTemplate();
myRedisTemplate.setConnectionFactory(jedisConnectionFactory);
return myRedisTemplate;
}

private void checkPasswordIfNull(JedisConnectionFactory jedisConnectionFactory){
if (!StringUtils.isBlank(password)) {
jedisConnectionFactory.setPassword(password);

}
}

}
4.大功告成 验证结果

 

你可能感兴趣的