Spring Boot 2.1整合Redis

1.普通字符串存储

1.引入spring-boot-starter-data-redisjar包,注意spring boot 2.1 没有对应的spring-boot-starter-redis版本,改名为spring-boot-starter-data-redis

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.在application中添加redis配置

spring:
  redis:
    host: 192.168.187.11
    port: 6379

3.测试

redis客户端查看,没有任何key

192.168.187.11:6379> keys *
(empty list or set)
@RestController
@RequestMapping(value = "/string")
public class RedisStringController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PutMapping(value = "/put")
    public void put(String key, @RequestParam(required = false, defaultValue = "default") String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @GetMapping(value = "/get")
    public Object get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }
}

使用postman送请求: localhost:8080/string/put?key=hello&value=world

《Spring Boot 2.1整合Redis》

localhost:8080/string/get?key=hello

《Spring Boot 2.1整合Redis》

2. 对象存储

在上述使用中,是无法存储对象的,存储对象的话需要使用RedisTemplate并且要使用响应的序列化机制,下面我们就来测试下:

1.引入序列化的jar包,这里我们是使用jackson

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.5</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.5</version>
</dependency>

2.添加redis配置类

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        //使用StringRedisSerializer来序列化和反序列化redis的ke
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        //开启事务
        redisTemplate.setEnableTransactionSupport(true);

        redisTemplate.setConnectionFactory(redisConnectionFactory);

        return redisTemplate;
    }
}

3.添加测试controller

@RestController
@RequestMapping(value = "/object")
public class RedisObjectController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/get/{username}")
    public Object get(@PathVariable String username) {
        return redisTemplate.opsForValue().get(username);
    }

    @PutMapping("/put")
    public void put(String username, String nickname) {
        User user = new User(username, nickname);
        redisTemplate.opsForValue().set(username, user);
    }
}

4.使用postman测试

《Spring Boot 2.1整合Redis》

《Spring Boot 2.1整合Redis》

使用redis客户端查看

192.168.187.11:6379> get qianjiangtao
"{\"@class\":\"com.lvmama.tony.model.User\",\"username\":\"qianjiangtao\",\"nickname\":\"Tony-J\"}"

3.spring boot整合redis自动化配置原理分析

我们都知道spring boot自动化配置中的配置都是通过spring-configuration-metadata.json来约束的,同理redis也是这样的,我们配置了spring.redis.host,不妨来找下这个配置项

{
"name": "spring.redis.host",
"type": "java.lang.String",
"description": "Redis server host.",
"sourceType": "org.springframework.boot.autoconfigure.data.redis.RedisProperties",
"defaultValue": "localhost"
}

从这能看出来redis的配置都是通过RedisProperties这个类来配置的,在这里面我们能看到众多的redis配置及默认配置,我们可以从一个入口切入,就拿port切入,来看下port在哪设置的

《Spring Boot 2.1整合Redis》
org.springframework.boot.autoconfigure.data.redis.RedisConnectionConfiguration#getStandaloneConfig
看出在RedisConnectionConfiguration中的getStandaloneConfig中赋值的,那这个方法又是谁调用的呢?继续找?

《Spring Boot 2.1整合Redis》

从图中能看出来有两个地方可能会调用,从类的名字能看出来,spring boot是支持Jedis和Lettuce两种客户端来操作redis,那到底是用哪个呢? 都看看呗

《Spring Boot 2.1整合Redis》

《Spring Boot 2.1整合Redis》

从图中截取的源码中能看出来,我是使用了LettuceConnectionConfiguration,看注解是我引入了RedisClient,我什么时候引入的?于是我就看看maven的依赖

《Spring Boot 2.1整合Redis》

从maven依赖中能看出一些重要的信息:

  • 1.spring-boot-starter-data-redis中其实用的是spring-data-redis,其实是包装了下
  • 2.依赖了lettuce-core,原来是从这里引入的,怪不得

如何验证呢?不能瞎说

​ 要想知道很简单的,在我们自己写的RedisConfig中打下断点,看看用的RedisConnectionFactory到底是不是LettuceConnectionFactory就能证明了

《Spring Boot 2.1整合Redis》

果然如此!

简单的流程就是:

1.spring boot通过application配置加载redis配置

2.解析封装成RedisProperties

3.根据@ConditionalOnClass判断使用哪个Redis客户端,封装成LettuceClientConfiguration并创建LettuceConnectionFactory

4.通过@Bean创建我们自己的配置类在LettuceConnectionFactory基础上添加我们自己自定义的配置

    原文作者:Spring Boot
    原文地址: https://segmentfault.com/a/1190000017805065
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞