spring:
redis:
cluster:
# 各 Redis 节点信息
nodes: 密码
# 执行命令超时时间
command-timeout: 15000
# 重试次数
max-attempts: 5
# 跨集群执行命令时要遵循的最大重定向数量
max-redirects: 3
# 连接池最大连接数(使用负值表示没有限制)
max-active: 16
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
# 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
test-on-borrow: true
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.RedisConnectionFactory;
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.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* @Author:HappyGiraffe
* @Description:Redis 配置类
* @CreateDate:13:48 2018/7/4
*/
@Configuration
@ConditionalOnClass(JedisCluster.class)
public class RedisConfig {
@Resource
private RedisProperties redisProperties;
/**
* 配置 Redis 连接池信息
*/
@Bean
public JedisPoolConfig getJedisPoolConfig() {
JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
jedisPoolConfig.setTestOnBorrow(redisProperties.isTestOnBorrow());
return jedisPoolConfig;
}
/**
* 配置 Redis Cluster 信息
*/
@Bean
public RedisClusterConfiguration getJedisCluster() {
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
redisClusterConfiguration.setMaxRedirects(redisProperties.getMaxRedirects());
List nodeList = new ArrayList<>();
String[] cNodes = redisProperties.getNodes().split(",");
//分割出集群节点
for(String node : cNodes) {
String[] hp = node.split(":");
nodeList.add(new RedisNode(hp[0], Integer.parseInt(hp[1])));
}
redisClusterConfiguration.setClusterNodes(nodeList);
return redisClusterConfiguration;
}
/**
* 配置 Redis 连接工厂
*/
@Bean
public JedisConnectionFactory getJedisConnectionFactory(RedisClusterConfiguration redisClusterConfiguration, JedisPoolConfig jedisPoolConfig) {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, jedisPoolConfig);
return jedisConnectionFactory;
}
/**
* 设置数据存入redis 的序列化方式
* redisTemplate序列化默认使用的jdkSerializeable
* 存储二进制字节码,导致key会出现乱码,所以自定义序列化类
*/
@Bean
public RedisTemplate
添加第二个配置
/**
* @Author:HappyGiraffe
* @Description:Redis 基本环境信息
* @CreateDate:13:48 2018/7/4
*/
@Component
//
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data
public class RedisProperties {
private String nodes;
private Integer commandTimeout;
private Integer maxAttempts;
private Integer maxRedirects;
private Integer maxActive;
private Integer maxWait;
private Integer maxIdle;
private Integer minIdle;
private boolean testOnBorrow;
public String getNodes() {
return nodes;
}
public void setNodes(String nodes) {
this.nodes = nodes;
}
public Integer getCommandTimeout() {
return commandTimeout;
}
public void setCommandTimeout(Integer commandTimeout) {
this.commandTimeout = commandTimeout;
}
public Integer getMaxAttempts() {
return maxAttempts;
}
public void setMaxAttempts(Integer maxAttempts) {
this.maxAttempts = maxAttempts;
}
public Integer getMaxRedirects() {
return maxRedirects;
}
public void setMaxRedirects(Integer maxRedirects) {
this.maxRedirects = maxRedirects;
}
public Integer getMaxActive() {
return maxActive;
}
public void setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
}
public Integer getMaxWait() {
return maxWait;
}
public void setMaxWait(Integer maxWait) {
this.maxWait = maxWait;
}
public Integer getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(Integer maxIdle) {
this.maxIdle = maxIdle;
}
public Integer getMinIdle() {
return minIdle;
}
public void setMinIdle(Integer minIdle) {
this.minIdle = minIdle;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
}