引入依赖
建议使用 Lettuce 连接驱动
<!--一:Jedis连接驱动缺点:Jedis基于TCP的阻塞性的连接方式1. 阻塞性IO2. 不能异步3. 线程不安全的Lettuce连接驱动优点:Lettuce基于Netty的多路复用的异步非阻塞的连接方式,1. 线程安全2. 基于Netty框架的事件驱动通信,可以异步的3. 适合于分布式的缓存。二:Springboot 1.x 默认redis驱动使用JedisSpringboot 2.x 默认redis驱动使用LettuceJedis驱动使用连接池是 commons-pool2Lettuce驱动使用连接池也是 commons-pool2--><!-- Redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- 显式导入 commons-pool2 连接池 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>
配置文件
spring:redis:# 集群cluster:# 集群地址nodes:redis:7000,redis:7001,redis:7002,redis:7003,redis:7004,redis:7005 # redis换成ip# 连接驱动lettuce:# 连接池配置pool:# 最大空闲连接max-idle: 8# 最大连接数max-active: 8# 最小空闲连接min-idle: 2# 最大建立连接等待时间max-wait: 1ms
配置类
配置文件读取类
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Component
@Data
public class RedisProperties {// 集群节点列表private String nodes;}
/*** Redis连接池配置信息读取类*/
@Component
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
@Data
public class RedisPoolProperties {// 最大空闲连接private Integer maxIdle;// 最小空闲连接private Integer minIdle;// 最大连接数private Integer maxActive;// 最大建立连接等待时间private Duration maxWait;
}
配置文件Config
@Configuration
public class RedisConfig {// Redis配置信息读取类@Resourceprivate RedisProperties redisProperties;// 连接池配置信息读取类@Resourceprivate RedisPoolProperties redisPoolProperties;@Beanpublic RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();// key,value序列化配置/* ************************ 常用的RedisSerializer** 1. Jackson2JsonRedisSerializer* 2. GenericFastJsonRedisSerializer 或 FastJsonRedisSerializer (fastJson2)* 3. StringRedisSerializer (只能支持String)* *********************/redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer());// 连接驱动的配置redisTemplate.setConnectionFactory(lettuceConnectionFactory());return redisTemplate;}/*** Lettuce连接驱动的配置*/@Beanpublic LettuceConnectionFactory lettuceConnectionFactory() {// redis集群的配置Map<String, Object> map = new HashMap<>();map.put("spring.redis.cluster.nodes", redisProperties.getNodes());RedisClusterConfiguration redisClusterConfiguration =new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration",map));/* ************************ redis集群方式的不同,需要使用对应的Redis配置实现类** RedisClusterConfiguration: 集群* RedisSentinelConfiguration: 哨兵* RedisStandaloneConfiguration:单机** *********************/// Lettuce连接池的配置GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();genericObjectPoolConfig.setMaxWait(redisPoolProperties.getMaxWait());genericObjectPoolConfig.setMinIdle(redisPoolProperties.getMinIdle());genericObjectPoolConfig.setMaxTotal(redisPoolProperties.getMaxActive());genericObjectPoolConfig.setMaxWait(redisPoolProperties.getMaxWait());LettucePoolingClientConfiguration lettucePoolingClientConfiguration =LettucePoolingClientConfiguration.builder().poolConfig(genericObjectPoolConfig).build();return new LettuceConnectionFactory(redisClusterConfiguration,lettucePoolingClientConfiguration);}
}
工具类
/*** Redis工具类*/
@Component
public class RedisUtil {@Resourceprivate RedisTemplate<String, Object> redisTemplate;public void stringSet(String key, Object value) {redisTemplate.opsForValue().set(key, value);}public Object stringGet(String key) {return redisTemplate.opsForValue().get(key);}public void hashSet(String key, Map<String, Object> map) {redisTemplate.opsForHash().putAll(key, map);}public Object hashGet(String key, String hashKey) {return redisTemplate.opsForHash().get(key, hashKey);}
}