SpringBoot整合Redis(手把手教你4大使用步骤)

SpringBoot整合Redis(手把手教你4大使用步骤)-mikechen

SpringBoot如何快速整合Redis,本篇就来重点详解SpringBoot使用Redis的4大步骤@mikechen

1.引入依赖包

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>
  6. <!--进行redisTemplate配置时需要此包-->
  7. <dependency>
  8. <groupId>com.fasterxml.jackson.core</groupId>
  9. <artifactId>jackson-annotations</artifactId>
  10. </dependency>
  11.  
  12. <!--reids版本为1.4.1版本以上需要添加-->
  13. <dependency>
  14. <groupId>org.apache.commons</groupId>
  15. <artifactId>commons-pool2</artifactId>
  16. </dependency>
  17.  
  18. </dependencies>

这里主要就是引入了 Spring Data Redis 与连接池。

 

2.添加配置文件

配置文件主要包含:服务器地址、端口、密码、连接池最大连接、最大最小空闲连接等信息。

主要分为两部分: Redis 的基本信息+连接池信息,详细的配置如下:

  1. # Redis数据库索引(默认为0)
  2. spring.redis.database=0
  3.  
  4. # Redis服务器地址
  5. spring.redis.host=127.0.0.1
  6.  
  7. # Redis服务器连接端口
  8. spring.redis.port=6379
  9.  
  10. # Redis服务器连接密码(默认为空)
  11. spring.redis.password=mikechen
  12.  
  13. # 连接池最大连接数(使用负值表示没有限制) 默认 8
  14. spring.redis.lettuce.pool.max-active=8
  15.  
  16. # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
  17. spring.redis.lettuce.pool.max-wait=-1
  18.  
  19. # 连接池中的最大空闲连接 默认 8
  20. spring.redis.lettuce.pool.max-idle=8
  21.  
  22. # 连接池中的最小空闲连接 默认 0
  23. spring.redis.lettuce.pool.min-idle=0
  24.  
  25. #超时时间,单位为毫秒
  26. spring.redis.timeout=30000

 

3.Redis自动配置

添加完依赖和连接配置参数之后,Redis自动化配置就会生效。

参考 Redis 的自动配置类:

  1. org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

核心源码如下:

  1. @Configuration
  2. @ConditionalOnClass(RedisOperations.class)
  3. @EnableConfigurationProperties(RedisProperties.class)
  4. @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
  5. public class RedisAutoConfiguration {
  6. @Bean
  7. @ConditionalOnMissingBean(name = "redisTemplate")
  8. public RedisTemplate<Object, Object> redisTemplate(
  9. RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  10. RedisTemplate<Object, Object> template = new RedisTemplate<>();
  11. template.setConnectionFactory(redisConnectionFactory);
  12. return template;
  13. }
  14. @Bean
  15. @ConditionalOnMissingBean
  16. public StringRedisTemplate stringRedisTemplate(
  17. RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  18. StringRedisTemplate template = new StringRedisTemplate();
  19. template.setConnectionFactory(redisConnectionFactory);
  20. return template;
  21. }
  22. }

自动配置提供了两种操作模板:

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

  1. @Service
  2. public class HelloService {
  3. @Autowired
  4. RedisTemplate redisTemplate;
  5. public void hello() {
  6. ValueOperations ops = redisTemplate.opsForValue();
  7. ops.set("k1", "v1");
  8. Object k1 = ops.get("k1");
  9. System.out.println(k1);
  10. }
  11. }

RedisTemplate 中,key 默认的序列化方案是 JdkSerializationRedisSerializer 。

不过开发者也可以自行修改 RedisTemplate 中的序列化方案,如下:

  1. @Service
  2. public class HelloService {
  3. @Autowired
  4. RedisTemplate redisTemplate;
  5. public void hello() {
  6. redisTemplate.setKeySerializer(new StringRedisSerializer());
  7. ValueOperations ops = redisTemplate.opsForValue();
  8. ops.set("k1", "v1");
  9. Object k1 = ops.get("k1");
  10. System.out.println(k1);
  11. }
  12. }

 

2)使用 StringRedisTemplate

  1. @Service
  2. public class HelloService {
  3. @Autowired
  4. StringRedisTemplate stringRedisTemplate;
  5. public void hello2() {
  6. ValueOperations ops = stringRedisTemplate.opsForValue();
  7. ops.set("k2", "v2");
  8. Object k1 = ops.get("k2");
  9. System.out.println(k1);
  10. }
  11. }

StringRedisTemplate 中,key 默认的序列化方案是 StringRedisSerializer ,因此,如果使用 StringRedisTemplate ,默认情况下 key 前面不会有前缀。

关注「mikechen」,十余年BAT架构经验倾囊相授!

评论交流
    说说你的看法
欢迎您,新朋友,感谢参与互动!