SpringBoot2.0-integrated Redis

directory

Due to the version, SpringBoot 2.0 integration Redis is not the same as the lower version of SpringBoot. After testing, this integration solution can be used.

一,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 database index (default is 0)
Spring.redis.database=0
# Redis server address
Spring.redis.host=localhost
# Redis server connection port
Spring.redis.port=6379
# Redis server connection password (default is empty)
Spring.redis.password=
# Connection pool maximum number of connections (use negative values ​​to indicate no limit)
Spring.redis.jedis.pool.max-active=8
#Connection pool maximum blocking wait time (using a negative value means no limit)
Spring.redis.jedis.pool.max-wait=-1
# Maximum free connection in the connection pool
Spring.redis.jedis.pool.max-idle=8
# The smallest idle connection in the connection pool
Spring.redis.jedis.pool.min-idle=0
#Connection timeout (ms)
Spring.redis.timeout=1000

Note that if the SpringBoot version used is 1.5, then spring.redis.jedis.pool.max-idl is written as spring.redis.pool.max-idl

3, 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;
    }

经测试, all the above configurations are available