
SpringBoot如何快速整合Redis,本篇就来重点详解SpringBoot使用Redis的4大步骤@mikechen
1.引入依赖包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--进行redisTemplate配置时需要此包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<!--reids版本为1.4.1版本以上需要添加-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
这里主要就是引入了 Spring Data Redis 与连接池。
2.添加配置文件
配置文件主要包含:服务器地址、端口、密码、连接池最大连接、最大最小空闲连接等信息。
主要分为两部分: Redis 的基本信息+连接池信息,详细的配置如下:
# Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=127.0.0.1 # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password=mikechen # 连接池最大连接数(使用负值表示没有限制) 默认 8 spring.redis.lettuce.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1 spring.redis.lettuce.pool.max-wait=-1 # 连接池中的最大空闲连接 默认 8 spring.redis.lettuce.pool.max-idle=8 # 连接池中的最小空闲连接 默认 0 spring.redis.lettuce.pool.min-idle=0 #超时时间,单位为毫秒 spring.redis.timeout=30000
3.Redis自动配置
添加完依赖和连接配置参数之后,Redis自动化配置就会生效。
参考 Redis 的自动配置类:
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
核心源码如下:
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
自动配置提供了两种操作模板:
1)RedisTemplate<Object, Object>
key-value 都为 Object 对象,并且默认用的 JDK 的序列化/反序列化器:
org.springframework.data.redis.serializer.JdkSerializationRedisSerializer
使用这个序列化器,key 和 value 都需要实现 java.io.Serializable 接口。
2)StringRedisTemplate
key-value 都为 String 对象,默认用的 String UTF-8 格式化的序列化/反序列化器:
org.springframework.data.redis.serializer.StringRedisSerializer
上面提到了两种序列化器,另外还有两种 JSON 的序列化器:
-
Jackson2JsonRedisSerializer -
GenericJackson2JsonRedisSerializer
使用方式上,两种都可以序列化、反序列化 JSON 数据,Jackson2JsonRedisSerializer 效率高,但 GenericJackson2JsonRedisSerializer 更为通用,不需要指定泛型类型。
4.直接使用案例
接下来,可以直接在 Service 中注入 StringRedisTemplate 或者 RedisTemplate 来使用:
1)使用RedisTemplate
@Service
public class HelloService {
@Autowired
RedisTemplate redisTemplate;
public void hello() {
ValueOperations ops = redisTemplate.opsForValue();
ops.set("k1", "v1");
Object k1 = ops.get("k1");
System.out.println(k1);
}
}
RedisTemplate 中,key 默认的序列化方案是 JdkSerializationRedisSerializer 。
不过开发者也可以自行修改 RedisTemplate 中的序列化方案,如下:
@Service
public class HelloService {
@Autowired
RedisTemplate redisTemplate;
public void hello() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
ValueOperations ops = redisTemplate.opsForValue();
ops.set("k1", "v1");
Object k1 = ops.get("k1");
System.out.println(k1);
}
}
2)使用 StringRedisTemplate
@Service
public class HelloService {
@Autowired
StringRedisTemplate stringRedisTemplate;
public void hello2() {
ValueOperations ops = stringRedisTemplate.opsForValue();
ops.set("k2", "v2");
Object k1 = ops.get("k2");
System.out.println(k1);
}
}
StringRedisTemplate 中,key 默认的序列化方案是 StringRedisSerializer ,因此,如果使用 StringRedisTemplate ,默认情况下 key 前面不会有前缀。
关于mikechen
mikechen睿哥,10年+大厂架构经验,资深技术专家,就职于阿里巴巴、淘宝、百度等一线互联网大厂。