pom 文件
org.springframework.boot spring-boot-starter-data-redis
application.properties(只需要配置一个节点即可,会自动发现其它节点)
# REDIS (RedisProperties)# Redis数据库索引(默认为0)spring.redis.database=0# Redis服务器地址spring.redis.host=xxxxxxxxx# Redis服务器连接端口spring.redis.port=7001# Redis集群服务器地址spring.redis.cluster.nodes=# 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
RedisConfig.java
package club.lemos.sso.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.interceptor.KeyGenerator;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import redis.clients.jedis.HostAndPort;import redis.clients.jedis.JedisCluster;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;import java.lang.reflect.Method;import java.util.HashSet;import java.util.Set;/** * Redis 集群配置 * Created by lg on 2017/3/21. */@Configuration@EnableCachingpublic class RedisConfig extends CachingConfigurerSupport { // @Value("${spring.redis.host}") // private String host; // @Value("${spring.redis.port}") // private int port; // @Value("${spring.redis.timeout}") // private int timeout; private static final Logger LOG = LoggerFactory.getLogger(RedisConfig.class); @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.pool.max-wait}") private long maxWaitMillis; /* * @Value("${spring.redis.password}") private String password; */ // Jedis连接池 @Bean public JedisPool redisPoolFactory() { LOG.info("JedisPool注入成功!!"); LOG.info("redis地址:" + host + ":" + port); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); // JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, // timeout, password); return new JedisPool(jedisPoolConfig, host, port, timeout); } // JedisCluster @Bean public JedisCluster JedisClusterFactory() { LOG.info("JedisCluster创建!!"); LOG.info("redis地址:" + host + ":" + port); SetjedisClusterNodes = new HashSet (); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); jedisClusterNodes.add(new HostAndPort(host, port)); return new JedisCluster(jedisClusterNodes, jedisPoolConfig); } @Bean public KeyGenerator wiselyKeyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } @Bean public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(host); factory.setPort(port); factory.setTimeout(timeout); // 设置连接超时时间 return factory; } @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); // Number of seconds before expiration. Defaults to unlimited (0) cacheManager.setDefaultExpiration(10); // 设置key-value超时时间 return cacheManager; } @Bean public RedisTemplate redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); setSerializer(template); // 设置序列化工具,这样ReportBean不需要实现Serializable接口 template.afterPropertiesSet(); return template; } private void setSerializer(StringRedisTemplate template) { Jackson2JsonRedisSerializer
工具类
接口
package club.lemos.sso.dao;/** * 'jedis 操作 * Created by lg on 2017/3/21. */public interface JedisClient { String get(String key); String set(String key, String value); String hget(String hkey, String field); Long hset(String hkey, String field, String value); Long incr(String key); Long expire(String key, int sec); Long ttl(String key); Long del(String key); Long hdel(String hkey, String field);}
单机版实现
@Repositorypublic class JedisClientSingle implements JedisClient { @Resource private JedisPool jedisPool; @Override public String get(String key) { Jedis resource = jedisPool.getResource(); String s = resource.get(key); resource.close(); return s; } @Override public String set(String key, String value) { Jedis resource = jedisPool.getResource(); String set = resource.set(key, value); resource.close(); return set; } @Override public String hget(String hkey, String field) { Jedis resource = jedisPool.getResource(); String hget = resource.hget(hkey, field); resource.close(); return hget; } @Override public Long hset(String hkey, String field, String value) { Jedis resource = jedisPool.getResource(); Long hset = resource.hset(hkey, field, value); resource.close(); return hset; } @Override public Long incr(String key) { Jedis resource = jedisPool.getResource(); Long incr = resource.incr(key); resource.close(); return incr; } @Override public Long expire(String key, int sec) { Jedis resource = jedisPool.getResource(); Long expire = resource.expire(key, sec); resource.close(); return expire; } @Override public Long ttl(String key) { Jedis resource = jedisPool.getResource(); Long ttl = resource.ttl(key); resource.close(); return ttl; } @Override public Long del(String key) { Jedis resource = jedisPool.getResource(); Long del = resource.del(key); resource.close(); return del; } @Override public Long hdel(String hkey, String field) { Jedis resource = jedisPool.getResource(); Long hdel = resource.hdel(hkey, field); resource.close(); return hdel; }}
集群版实现
package club.lemos.sso.dao.impl;import club.lemos.sso.dao.JedisClient;import redis.clients.jedis.JedisCluster;import javax.annotation.Resource;/** * redis集群客户端 * Created by lg on 2017/3/21. */public class JedisClientCluster implements JedisClient { /** * 引入RedisConfig中的 jedisCluster Bean */ @Resource private JedisCluster jedisCluster; @Override public String get(String key) { return jedisCluster.get(key); } @Override public String set(String key, String value) { return jedisCluster.set(key, value); } @Override public String hget(String hkey, String field) { return jedisCluster.hget(hkey, field); } @Override public Long hset(String hkey, String field, String value) { return jedisCluster.hset(hkey, field, value); } @Override public Long incr(String key) { return jedisCluster.incr(key); } @Override public Long expire(String key, int sec) { return jedisCluster.expire(key, sec); } @Override public Long ttl(String key) { return jedisCluster.ttl(key); } @Override public Long del(String key) { return jedisCluster.del(key); } @Override public Long hdel(String hkey, String field) { return jedisCluster.hdel(hkey, field); }}