美文网首页
Spring Boot 2 整合Reids

Spring Boot 2 整合Reids

作者: 茶艺瑶 | 来源:发表于2020-07-23 14:34 被阅读0次

Redis是什么,我这里就不多说了。

先在pom.xml添加依赖包

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

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

我们先看看 这些包都依赖那些内容


image.png

我们需要做一个自动序列化的Bean

/**
 * Redis 初始化配置
 */
@Configuration
@EnableCaching // 开启注解
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Serializable>
    redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

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

        return redisTemplate;
    }
}

SpringBoot 开发效率其实和PHP已经差不多了

我们暂时只用 opsForValue,这是Redis常用的类型。我们可以理距为字符串类型吧,如果你要用Redis 的list类型的话 就是 opsForList

@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);
    }
}

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

image.png

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

image.png

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

image.png image.png

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

image.png

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

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

如何验证呢?不能瞎说

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

image.png

果然如此!

简单的流程就是:

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

2.解析封装成RedisProperties

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

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

相关文章

网友评论

      本文标题:Spring Boot 2 整合Reids

      本文链接:https://www.haomeiwen.com/subject/jgqhlktx.html