1.前言

搭建好redis集群环境,如图:
Springboot2.X集成redis集群(Lettuce)连接_spring

2.导入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

3. 配置application.properties

spring:
# Redis 配置信息
redis:
timeout: 6000
cluster:
nodes: 172.81.235.217:7001,172.81.235.217:7002,172.81.235.217:7003,172.81.235.217:7004,172.81.235.217:7005,172.81.235.217:7006
###################以下为lettuce连接池增加的配置###########################
lettuce:
pool:
max-active: 100 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 100 # 连接池中的最大空闲连接
min-idle: 50 # 连接池中的最小空闲连接
max-wait: 10000 # 连接池最大阻塞等待时间(使用负值表示没有限制

4.新建下面的两个配置类

package com.micro.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;

import java.util.Set;

/**
* @BelongsProject: MicroService
* @BelongsPackage: com.micro.config
* @CreateTime: 2020-11-25 09:28
* @Description: TODO
*/
@Configuration
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisClusterConfig {

private Set<String> nodes;
private String master;

@Value("${spring.redis.timeout}")
private long timeout;
@Value("${spring.redis.lettuce.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.lettuce.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.lettuce.pool.max-wait}")
private long maxWait;
@Value("${spring.redis.lettuce.pool.max-active}")
private int maxActive;

/**
* 创建一个Redis连接工厂
* @return
*/
@Bean
public RedisConnectionFactory lettuceConnectionFactory() {

RedisClusterConfiguration redisClusterConfiguration= new RedisClusterConfiguration(nodes);

GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxIdle(maxIdle);
genericObjectPoolConfig.setMinIdle(minIdle);
genericObjectPoolConfig.setMaxTotal(maxActive);
genericObjectPoolConfig.setMaxWaitMillis(maxWait);

LettucePoolingClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder()
.poolConfig(genericObjectPoolConfig)
.build();
return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);
}



public void setNodes(Set<String> nodes) {
this.nodes = nodes;
}

public void setMaster(String master) {
this.master = master;
}
}
package com.micro.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

/**
* @BelongsProject: MicroService
* @BelongsPackage: com.micro.config
* @CreateTime: 2020-11-25 09:33
* @Description: TODO
*/
@Configuration
public class RedisTemplateConfig {

@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory factory) {
//创建Json序列化对象
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);

//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);

// 将默认序列化改为Jackson2JsonRedisSerializer序列化
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setKeySerializer(jackson2JsonRedisSerializer);// key序列化
template.setValueSerializer(jackson2JsonRedisSerializer);// value序列化
template.setConnectionFactory(factory);
template.afterPropertiesSet();
return template;
}
}

5.执行测试

@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisConfigurationTest {

@Autowired
private RedisTemplate redisTemplate;

@Test
public void redisTemplate() throws Exception {

redisTemplate.opsForValue().set("author", "Damein_xym");
}

}