添加依赖

或者在创建SpringBoot项目的时候勾选Redis

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

设置Redis的配置文件

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

到我们SpringBoot2.x版本,其内置的Redis中间件再也不是Jedis了,而是换成了lettuce。

那么lettuce是什么呢,其与Jedis有什么区别呢?

  • lettuce: Lettuce 是 一种可伸缩,线程安全,完全非阻塞的Redis客户端,多个线程可以共享一个RedisConnection,它利用Netty NIO 框架来高效地管理多个连接,从而提供了异步和同步数据访问方式,用于构建非阻塞的反应性应用程序。
  • Jedis: Jedis 在实现上是直连 redis server,多线程环境下非线程安全,除非使用连接池,为每个 redis实例增加 物理连接。 这种方式更加类似于我们 BIO 一条线程连一个客户端,并且是阻塞式的,会一直连接着客户端等待客户端的命令

    测试

    通过如上简单的配置以后,就可以在SpringBoot里面使用Redis了,用法也很简单

    1. @Autowired
    2. private RedisTemplate redisTemplate;
    3. @Test
    4. void contextLoads() {
    5. //opForXxxx:操作什么数据类型的方法
    6. //opsForValue()操作字符串
    7. //opsForList()操作List
    8. //......
    9. //常用的方法都可以直接通过redistemplate操作,比如事务和基本的CRUD
    10. //获取redis的连接对象,通过数据库连接对象可以对数据库进行操作
    11. // RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
    12. // connection.flushDb();
    13. // connection.flushAll();
    14. redisTemplate.opsForValue().set("spk1","lisi");
    15. Object spk1 = redisTemplate.opsForValue().get("spk1");
    16. System.out.println(spk1);
    17. }

在打开Shell在Redis里面查询全部键,发现用SpringBoot添加进去的key有乱码问题
image.png

源码

点开RedisTemplate,发现有一个属性,keySerializer,键的序列化
image.png
去看赋值,会发现默认用的jdk序列化,而它使用的编码是ISO-8859-1,会出现转义,这时候我们可能要用JSON序列化
image.png
这时候找到RedisAuto自动装配类,发现当redisTemplate不存在时,才注入自带的,否这不注入,所以可以重写这个redisTemplate方法。
image.png

RedisConfig配置

既然默认的redisTemaple无法满足情况,我们就自己写一个,配置类,这个配置类怎么写呢,我们直接照抄RedisAutoConfiguration里面的方法,进行修改

  1. package com.example.demo2.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.data.redis.connection.RedisConnectionFactory;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  7. import org.springframework.data.redis.serializer.StringRedisSerializer;
  8. @Configuration
  9. public class RedisConfig {
  10. //固定模板
  11. @Bean
  12. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  13. RedisTemplate<String, Object> template = new RedisTemplate();
  14. template.setConnectionFactory(redisConnectionFactory);
  15. //String序列化
  16. StringRedisSerializer serializer = new StringRedisSerializer();
  17. template.setKeySerializer(serializer);
  18. template.setValueSerializer(serializer);
  19. //Json序列化
  20. GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
  21. template.setHashKeySerializer(genericJackson2JsonRedisSerializer);
  22. template.setHashValueSerializer(genericJackson2JsonRedisSerializer);
  23. template.afterPropertiesSet();
  24. return template;
  25. }
  26. }

现在添加数据就不会出现上面的乱码问题了,如果用Shell查看,中文还出现上面的乱码问题。其实这不是乱码问题,应该是数据进制问题,用Shell重新打开Redis客户端,输入这个命令redis-cli --raw,然后就没有问题了。