Spring Boot 2.1整合Redis

1.普通字符串存储

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

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

2.在application中添加redis配置redis

spring:
  redis:
    host: 192.168.187.11
    port: 6379

3.测试spring

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

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=worldjson

clipboard.png

localhost:8080/string/get?key=helloapp

clipboard.png

2. 对象存储

在上述使用中,是没法存储对象的,存储对象的话须要使用RedisTemplate而且要使用响应的序列化机制,下面咱们就来测试下:maven

1.引入序列化的jar包,这里咱们是使用jacksonspring-boot

<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配置类post

@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测试

clipboard.png

clipboard.png

使用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在哪设置的

clipboard.png
org.springframework.boot.autoconfigure.data.redis.RedisConnectionConfiguration#getStandaloneConfig
看出在RedisConnectionConfiguration中的getStandaloneConfig中赋值的,那这个方法又是谁调用的呢?继续找?

clipboard.png

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

clipboard.png

clipboard.png

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

clipboard.png

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

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

如何验证呢?不能瞎说

​ 要想知道很简单的,在咱们本身写的RedisConfig中打下断点,看看用的RedisConnectionFactory究竟是不是LettuceConnectionFactory就能证实了

clipboard.png

果真如此!

简单的流程就是:

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

2.解析封装成RedisProperties

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

4.经过@Bean建立咱们本身的配置类在LettuceConnectionFactory基础上添加咱们本身自定义的配置

相关文章
相关标签/搜索