通过结合jedis和spring-data-redis包开发的一个简易方便的redis 集群访问接口。对于redis-cluster方式集群安装参照:http://blog.csdn.net/cweeyii/article/details/71298905
整个工程代码:https://github.com/cweeyii/redis-parent 接口类:
public interface IRedis<K,HK,HV> { void setHashObject(K key, Map<HK, HV> fieldValues, int expireSecond); void setHashObject(K key, Map<HK, HV> fieldValues); Map<HK, HV> getHashObject(K key, List<HK> fields); boolean expire(K key, int expireSeconds); void setObject(K key, HV value, int expireSecond); void setObject(K key, HV value); HV getObject(K key); }实现类:
@Component public class RedisImpl<K, HK, HV> implements IRedis<K, HK, HV> { private static final int DEFAULT_EXPIRED_TIME=7*24*3600; @Resource private RedisTemplate<K, HV> redisTemplate; public void setHashObject(K key, Map<HK, HV> fieldValues, int expireSecond) { HashOperations<K, HK, HV> valueOperate = redisTemplate.opsForHash(); if (!CollectionUtils.isEmpty(fieldValues)) { valueOperate.putAll(key, fieldValues); } expire(key, expireSecond); } @Override public void setHashObject(K key, Map<HK, HV> fieldValues) { setHashObject(key,fieldValues, DEFAULT_EXPIRED_TIME); } public Map<HK, HV> getHashObject(K key, List<HK> fields) { HashOperations<K, HK, HV> valueOperate = redisTemplate.opsForHash(); Map<HK, HV> map = new HashMap<>(); if (!CollectionUtils.isEmpty(fields)) { for (HK field : fields) { HV obj = valueOperate.get(key, field); map.put(field, obj); } } return map; } public boolean expire(K key, int expireSeconds) { return redisTemplate.expire(key, expireSeconds, TimeUnit.SECONDS); } public void setObject(K key, HV value, int expireSecond) { ValueOperations<K, HV> valueOperate = redisTemplate.opsForValue(); valueOperate.set(key, value); redisTemplate.expire(key, expireSecond, TimeUnit.SECONDS); } @Override public void setObject(K key, HV value) { setObject(key,value,DEFAULT_EXPIRED_TIME); } public HV getObject(K key) { ValueOperations<K, HV> valueOperate = redisTemplate.opsForValue(); return valueOperate.get(key); } }spring-bean配置文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> #开启@Component和@Service注解 <context:component-scan base-package="com.cweeyii.**"/> #指定配置文件的位置 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:redis.properties</value> </list> </property> </bean> #redis-cluster的具体配置 #客户端链接池配置 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}"/> <property name="maxTotal" value="${redis.maxTotal}"/> <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> <property name="testOnReturn" value="${redis.testOnReturn}"/> </bean> #redis-cluster集群地址 <bean id="redisCluterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <constructor-arg index="0"> <set> <value>${redis.host_and_port1}</value> <value>${redis.host_and_port2}</value> <value>${redis.host_and_port3}</value> <value>${redis.host_and_port4}</value> <value>${redis.host_and_port5}</value> <value>${redis.host_and_port6}</value> </set> </constructor-arg> </bean> #redis-client与redis-cluster的链接配置 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg index="0" ref="redisCluterConfig"/> <constructor-arg index="1" ref="poolConfig"/> </bean> #spring-data-redis包装,指定key,value, hkey,hvaue序列化和反序列类 <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <bean id="valueRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> <property name="keySerializer" ref="stringRedisSerializer"/> <property name="hashKeySerializer" ref="stringRedisSerializer"/> <property name="valueSerializer" ref="valueRedisSerializer"/> <property name="hashValueSerializer" ref="valueRedisSerializer"/> </bean>redis相关配置
redis.host=192.168.31.88 redis.port=6379 redis.maxIdle=200 redis.maxTotal=1000 redis.maxWaitMillis=2000 redis.testOnBorrow=true redis.testOnReturn=true redis.host1=192.168.31.88 redis.port1=6379 redis.host2=192.168.31.88 redis.port2=6380 redis.host3=192.168.31.234 redis.port3=6379 redis.host4=192.168.31.234 redis.port4=6380 redis.host5=192.168.31.186 redis.port5=6379 redis.host6=192.168.31.186 redis.port6=6380 redis.host_and_port1=192.168.31.88:6379 redis.host_and_port2=192.168.31.88:6380 redis.host_and_port3=192.168.31.234:6379 redis.host_and_port4=192.168.31.234:6380 redis.host_and_port5=192.168.31.186:6379 redis.host_and_port6=192.168.31.186:6380spring测试程序
@Ignore @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:applicationContext-redis.xml"}) public class BaseTest { private static final Logger LOGGER = LoggerFactory.getLogger(BaseTest.class); @BeforeClass public static void setUpBeforeClass() throws Exception { LOGGER.info("run setUpBeforeClass"); } @AfterClass public static void tearDownAfterClass() throws Exception { LOGGER.info("run tearDownAfterClass"); } @Test public void doNothing() { LOGGER.info("run doNothing"); } } public class RedisClientTest extends BaseTest { @Resource private IRedis iRedis; @Test public void testSetObject() { for (int i = 0; i < 1000; i++) { String key = "firstKey" + i; String value = "firstValue" + i; iRedis.setObject(key, value, DateUtils.getRandomExpireDays(21, 7) * 24 * 3600); } for (int i = 0; i < 10; i++) { String key = "firstKey" + i; String value = (String) iRedis.getObject(key); System.out.println(value); } } @Test public void testHash() { Map<String, Integer> multiValueMap = new HashMap<>(); for (int i = 0; i < 1000; i++) { String hkey = "firstKey" + i; multiValueMap.put(hkey, i); } String key = "key"; iRedis.setHashObject(key, multiValueMap); List<String> hKeyList = new ArrayList<>(); for (int i = 0; i < 10; i++) { hKeyList.add("firstKey" + i); } Map<String, Integer> valueMap = iRedis.getHashObject(key, hKeyList); System.out.print(valueMap); } }输出结果 setObject
firstValue0 firstValue1 firstValue2 firstValue3 firstValue4 firstValue5 firstValue6 firstValue7 firstValue8 firstValue9setHashObject
{firstKey8=8, firstKey7=7, firstKey9=9, firstKey0=0, firstKey2=2, firstKey1=1, firstKey4=4, firstKey3=3, firstKey6=6, firstKey5=5}