SpringBoot2.0-整合Redis

版权声明: https://blog.csdn.net/pbrlovejava/article/details/82717817

目录


由于版本原因,SpringBoot2.0整合Redis和低版本的SpringBoot不太一样,经测试,本文这套整合方案可以使用。


一、build.gradle

//redis clienet
    compile("redis.clients:jedis:2.9.0")
    //commons pool
    compile("org.apache.commons:commons-pool2:2.6.0")
    //redis starter
    compile("org.springframework.boot:spring-boot-starter-redis:2.0.4.RELEASE")
    //redis data
    compile("org.springframework.data:spring-data-redis:2.0.5.RELEASE")

二、application.properties

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

注意,如果使用的SpringBoot版本是1.5,那么spring.redis.jedis.pool.max-idl写成spring.redis.pool.max-idl


三、Controller

 @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @RequestMapping(value="/redis")
    @ResponseBody
    public String redis(){
        System.out.println("hello");
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String hello = ops.get("hello");
        ops.set("redisTest","hello Redis",10000);
        return hello;
    }

经测试,以上配置全部可用

猜你喜欢

转载自blog.csdn.net/pbrlovejava/article/details/82717817