SpringBoot集成Redis
引入依赖
建议使用 Lettuce 连接驱动
<!--
一:
Jedis连接驱动缺点:
Jedis基于TCP的阻塞性的连接方式
1. 阻塞性IO
2. 不能异步
3. 线程不安全的
Lettuce连接驱动优点:
Lettuce基于Netty的多路复用的异步非阻塞的连接方式,
1. 线程安全
2. 基于Netty框架的事件驱动通信,可以异步的
3. 适合于分布式的缓存。
二:
Springboot 1.x 默认redis驱动使用Jedis
Springboot 2.x 默认redis驱动使用Lettuce
Jedis驱动使用连接池是 commons-pool2
Lettuce驱动使用连接池也是 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配置信息读取类
@Resource
private RedisProperties redisProperties;
// 连接池配置信息读取类
@Resource
private RedisPoolProperties redisPoolProperties;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// key,value序列化配置
/* **********************
*
* 常用的RedisSerializer
*
* 1. Jackson2JsonRedisSerializer
* 2. GenericFastJsonRedisSerializer 或 FastJsonRedisSerializer (fastJson2)
* 3. StringRedisSerializer (只能支持String)
* *********************/
GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
// string key & value
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);
// hash key & value
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(genericFastJsonRedisSerializer);
// 连接驱动的配置
redisTemplate.setConnectionFactory(lettuceConnectionFactory());
return redisTemplate;
}
/**
* Lettuce连接驱动的配置
*/
@Bean
public 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 {
@Resource
private 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);
}
}