Spring Boot 1 和 Redis集成

Spring Boot2 和 Redis集成参考 https://my.oschina.net/u/3777515/blog/1631864java

一、引入maven依赖node

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

在学习过程当中发现有两种引入redis的依赖artifactId,分别是redis

spring-boot-starter-redisspring

spring-boot-starter-data-redis数据库

查询资料,有说是spring-boot-starter-redis 更名成了 spring-boot-starter-data-redis,建议使用服务器

spring-boot-starter-data-redis。app

二、在 application.properties 应用配置文件,增长 Redis 相关配置maven

# Redis 配置
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器链接端口
spring.redis.port=6379
# Redis服务器链接密码(默认为空)
spring.redis.password=
# 链接池最大链接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 链接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 链接池中的最大空闲链接
spring.redis.pool.max-idle=8
# 链接池中的最小空闲链接
spring.redis.pool.min-idle=0
# 链接超时时间(毫秒)
spring.redis.timeout=0

详细解释能够参考注释。对应的配置类:spring-boot

org.springframework.boot.autoconfigure.data.redis.RedisProperties学习

Spring Boot1 中的 RedisProperties.java类

package org.springframework.boot.autoconfigure.data.redis;

import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(
    prefix = "spring.redis"
)
public class RedisProperties {
    private int database = 0;
    private String url;
    private String host = "localhost";
    private String password;
    private int port = 6379;
    private boolean ssl;
    private int timeout;
    private RedisProperties.Pool pool;
    private RedisProperties.Sentinel sentinel;
    private RedisProperties.Cluster cluster;

    // 省略set get方法    

    public static class Sentinel {
        private String master;
        private String nodes;

        // 省略set get方法 
    }

    public static class Cluster {
        private List<String> nodes;
        private Integer maxRedirects;

        // 省略set get方法 
    }

    public static class Pool {
        private int maxIdle = 8;
        private int minIdle = 0;
        private int maxActive = 8;
        private int maxWait = -1;

        // 省略set get方法 
    }
}
相关文章
相关标签/搜索