redis与springboot

51 阅读2分钟

引入redis

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>

    <!-- 实现对 Spring Data Redis 的自动化配置 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
        <exclusions>
            <!-- 去掉对 Lettuce 的依赖,因为 Spring Boot 优先使用 Lettuce 作为 Redis 客户端 -->
            <exclusion>
                <groupId>io.lettuce</groupId>
                <artifactId>lettuce-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- 引入 Jedis 的依赖,这样 Spring Boot 实现对 Jedis 的自动化配置 -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
    </dependency>

    <!-- 方便等会写单元测试 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- 等会示例会使用 fastjson 作为 JSON 序列化的工具 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.61</version>
    </dependency>

    <!-- Spring Data Redis 默认使用 Jackson 作为 JSON 序列化的工具 -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

</dependencies>

Jedis实例是线程不安全的,多线程环境下需要基于连接池来使用。

Lettuce是基于Netty实现的,支持同步、异步和响应式编程方式,并且是线程安全的。支持Redis的哨兵模式、集群模式和管道模式。

jedis配置

spring:  
# 对应 RedisProperties 类  
    redis:  
        host: 127.0.0.1  
        port: 6379  
        password: # Redis 服务器密码,默认为空。生产中,一定要设置 Redis 密码!  
        database: 0 # Redis 数据库号,默认为 0 。  
        timeout: 0 # Redis 连接超时时间,单位:毫秒。  
# 对应 RedisProperties.Jedis 内部类  
jedis:  
    pool:  
        max-active: 8 # 连接池最大连接数,默认为 8 。使用负数表示没有限制。  
        max-idle: 8 # 默认连接数最小空闲的连接数,默认为 8 。使用负数表示没有限制。  
        min-idle: 0 # 默认连接池最小空闲的连接数,默认为 0 。允许设置 0 和 正数。  
        max-wait: -1 # 连接池最大阻塞等待时间,单位:毫秒。默认为 -1 ,表示不限制。

lettuce

spring:
  redis:
    # Redis服务器地址
    host: 127.0.0.1
    # Redis服务器端口号
    port: 6379
    # 使用的数据库索引,默认是0
    database: 0
    # 连接超时时间
    timeout: 1800000
     # 设置密码
    password: "123456"
    lettuce:
      pool:
        # 最大阻塞等待时间,负数表示没有限制
        max-wait: -1
        # 连接池中的最大空闲连接
        max-idle: 5
        # 连接池中的最小空闲连接
        min-idle: 0
        # 连接池中最大连接数,负数表示没有限制
        max-active: 20

redisTemplate

序列化

image.png

@Bean
public RedisTemplate<String, Object> RedisTemplateSerializerHandler(RedisConnectionFactory redisRepositoryFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    // 关闭启用默认配置
    template.setEnableDefaultSerializer(false);
    template.setConnectionFactory(redisRepositoryFactory);
    // key 序列化方式
    template.setKeySerializer(RedisSerializer.string());
    // value 序列化方式
    template.setValueSerializer(RedisSerializer.json());
    // hash key 序列化方式
    template.setHashKeySerializer(RedisSerializer.string());
    // hash value 序列化方式
    template.setHashValueSerializer(RedisSerializer.json());
    // 配置完成
    template.afterPropertiesSet();
    return template;
}