需要使用的包
org.springframework.boot spring-boot-starter-redis 1.4.3.RELEASE
写配置文件application.properties
redis.host=10.211.55.8redis.port=6379redis.password=uwo_comredis.database=0redis.timeout=1000000redis.pool.max-total=1000redis.pool.max-idle=200redis.pool.min-idle=10redis.pool.max-wait=10000
编写RedisProperties文件
package com.uwo.oss.redis.configuration;import org.springframework.boot.context.properties.ConfigurationProperties;/** * Created by yanhao on 2017/6/1. */@ConfigurationProperties(prefix = "redis", locations = {"application-{profile}.properties"})public class RedisProperties { private String host; private Integer database; private Integer port; private String password; private Integer timeout; private RedisPoolProperties pool; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public Integer getDatabase() { return database; } public void setDatabase(Integer database) { this.database = database; } public Integer getPort() { return port; } public void setPort(Integer port) { this.port = port; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getTimeout() { return timeout; } public void setTimeout(Integer timeout) { this.timeout = timeout; } public RedisPoolProperties getPool() { return pool; } public void setPool(RedisPoolProperties pool) { this.pool = pool; } public static class RedisPoolProperties{ private Integer maxTotal; private Integer maxIdle; private Integer minIdle; private Integer maxWait; public Integer getMaxTotal() { return maxTotal; } public void setMaxTotal(Integer maxTotal) { this.maxTotal = maxTotal; } public Integer getMaxIdle() { return maxIdle; } public void setMaxIdle(Integer maxIdle) { this.maxIdle = maxIdle; } public Integer getMinIdle() { return minIdle; } public void setMinIdle(Integer minIdle) { this.minIdle = minIdle; } public Integer getMaxWait() { return maxWait; } public void setMaxWait(Integer maxWait) { this.maxWait = maxWait; } }}
编写RedisConfiguration类
package com.uwo.oss.redis.configuration; import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.StringRedisTemplate;import redis.clients.jedis.JedisPoolConfig; /** * Created by yanhao on 2017/6/1. */@Configuration@EnableConfigurationProperties({RedisProperties.class})public class RedisConfiguration { private final Logger log = Logger.getLogger(RedisConfiguration.class); @Autowired private RedisProperties redisProperties; @Bean public RedisConnectionFactory jedisConnectionFactory(){ log.warn(String.format("redis connection factory host = %s, " + "database = %d ,port = %d ,password = %s ,timeout = %d, " + "pool max-idle = %d, min-idle = %d, max-wait = %d, max-total = %d", redisProperties.getHost(), redisProperties.getDatabase(), redisProperties.getPort(), redisProperties.getPassword(), redisProperties.getTimeout(), redisProperties.getPool().getMaxIdle(), redisProperties.getPool().getMinIdle(), redisProperties.getPool().getMaxWait(), redisProperties.getPool().getMaxTotal())); JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(redisProperties.getPool().getMaxTotal()); poolConfig.setMaxIdle(redisProperties.getPool().getMaxIdle()); poolConfig.setMinIdle(redisProperties.getPool().getMinIdle()); poolConfig.setMaxWaitMillis(redisProperties.getPool().getMaxWait()); poolConfig.setTestOnBorrow(true); poolConfig.setTestOnReturn(true); poolConfig.setTestWhileIdle(true); JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig); jedisConnectionFactory.setHostName(redisProperties.getHost()); jedisConnectionFactory.setPassword(redisProperties.getPassword()); jedisConnectionFactory.setTimeout(redisProperties.getTimeout()); jedisConnectionFactory.setDatabase(redisProperties.getDatabase()); jedisConnectionFactory.setPort(redisProperties.getPort()); return jedisConnectionFactory; } @Bean public StringRedisTemplate redisTemplate() { log.warn("redis template"); StringRedisTemplate redisTemplate = new StringRedisTemplate(jedisConnectionFactory()); return redisTemplate; } }