10-springboot整合redis

在springboot 2.x之后spring 连接redis 底层已经由jedis变更为lettuce。
jedis与lettuce的区别

  • jedis底层采用的是直连redis 来一个请求就会创建一个连接 这样是不行的,当并发量大的时候会造成redis连接数过多,所以实际生产环境中一般采用jedispool(jedis提供的redis连接池) 但是这样当并发量过高时,连接池会将请求放入队里中等待,效率相对于lettuce来说太低了。(阻塞式IO BIO)
  • lettuce底层采用的netty(非阻塞式IO NIO) 实例可以在多个线程中共享 效率相对jedispool更高

springboot 所有的配置类 都有一个自动配置类(在spring-boot-autoconfigure.jar里) 对于本文就是下面的RedisAutoConfiguration类 这个类就是redis的自动配置类
在这里插入图片描述
自动配置类都会绑定一个properties配置文件类 对于本文就是RedisAutoConfiguration类的上面绑定了一个RedisProperties的类这个类就是springboot的redis配置 springboot集成redis需要的属性都在里面
在这里插入图片描述
源码分析

@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
    public RedisAutoConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean( //改注解表示当用户没有自定义redisTemplate时 下面的redisTemplate生效 如果自定义了 以用户自定义的template为准
        name = {"redisTemplate"}
    )
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    @ConditionalOnMissingBean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

导入依赖

 <dependencyManagement>
        <dependencies>
            <!--导入springboot的父类启动这种方式可以使得springboot工程不需要继承springboot父类启动器-->
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springboot的redis启动器 底层依赖 spring-data-redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

    <build>
        <!--标识打出来jar的名称-->
        <finalName>redis-test-02</finalName>
        <!-- 指定maven的jdk版本 -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

apllication.properties配置

#springboot 所有的配置类 都有一个自动配置类(在spring-boot-autoconfigure.jar里)
#自动配置类都会绑定一个properties配置文件类
spring.redis.host=127.0.0.1
spring.redis.port=6379
#指定redis操作的库为0库 默认也是0 不指定也可以
spring.redis.database=0
#jedis连接池的配置 springboot 2.x之后默认是不支持的 需要额外导入依赖
#spring.redis.jedis.pool.max-active=8
#lettuce连接池配置 springboot 2.x之后默认是采用的lettuce的连接池
#spring.redis.lettuce.pool.max-active=8

测试

@SpringBootApplication
public class App { //springboot启动类
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

@SpringBootTest(classes = App.class)
@RunWith(SpringRunner.class)
public class RedisTest02 { //springboot测试类

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test() throws Exception{
        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection(); //获取redis连接
        connection.flushDb(); //清空当前db
        redisTemplate.opsForValue().set("username", "张三");
        System.out.println(redisTemplate.opsForValue().get("username"));
    }
}

源码补充
从RedisAutoConfigration类中找到RedisTemplate点开
在这里插入图片描述
点开进入RedisTemplate类
可以看到redisTemplate中有很多值的序列化配置 都是null 其中有一个默认的序列化 搜索这个默认的序列化 可以看到当这个默认的序列化为null时指定的是JDK的jdk的序列化方法 如果需要使用自定义的序列化方法 可以通过自定义redisTemplate来设置
在这里插入图片描述
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值