Skip to content

Commit

Permalink
完善redis cluster集群高可用方案
Browse files Browse the repository at this point in the history
  • Loading branch information
wosyingjun committed Jul 24, 2016
1 parent b7e5b8b commit 69e948e
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- **分布式事务(待完善)**
- **分布式锁(待完善)**
- **dubbo服务集群、负载均衡策略(待完善)**
- **Redis集群高可用方案(待完善)**
- **[Redis Cluster集群高可用方案](https://wosyingjun.iteye.com/blog/2289220)**
- **[Zookeeper集群高可用方案](https://wosyingjun.iteye.com/blog/2312960)**
- **消息中间件 ActiveMQ 的引入(待完善)**
- **ActiveMQ 集群高可用方案(待完善)**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.yingjun.ssm.common.util.cache;

import java.util.List;
import java.util.Set;

import com.yingjun.ssm.common.util.ProtoStuffSerializerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.yingjun.ssm.common.util.ProtoStuffSerializerUtil;
import java.util.List;
import java.util.Set;


/**
* redis缓存
*
*
* 采用Jedis或Jedis Sentinel
*
* @author yingjun10627
*
*/
Expand Down Expand Up @@ -125,10 +126,9 @@ public void deleteCacheWithPattern(String pattern) {

/**
* 清空所有缓存
*
* @param key
*/
public void clearCache() {
deleteCacheWithPattern(RedisCache.CAHCENAME+"|*");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.yingjun.ssm.common.util.cache;

import com.yingjun.ssm.common.util.ProtoStuffSerializerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

import java.util.*;


/**
* redis缓存
*
* 采用Jedis Cluster
*
* @author yingjun
*
*/
@Component
public class RedisClusterCache {


public final static String CAHCENAME="cache";//缓存名
public final static int CAHCETIME=60;//默认缓存时间

@Autowired
private JedisCluster jedisCluster;


public <T> void putCache(String key, T obj) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
jedisCluster.set(bkey,bvalue);
}

public <T> void putCacheWithExpireTime(String key, T obj, int expireTime) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
jedisCluster.setex(bkey, expireTime, bvalue);
}

public <T> void putListCache(String key, List<T> objList) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
jedisCluster.set(bkey,bvalue);
}

public <T> void putListCacheWithExpireTime(String key, List<T> objList, int expireTime) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
jedisCluster.setex(bkey, expireTime, bvalue);
}

public <T> T getCache(final String key, Class<T> targetClass) {
byte[] result =jedisCluster.get(key.getBytes());
if (result == null) {
return null;
}
return ProtoStuffSerializerUtil.deserialize(result, targetClass);
}

public <T> List<T> getListCache(String key, Class<T> targetClass) {
byte[] result =jedisCluster.get(key.getBytes());
if (result == null) {
return null;
}
return ProtoStuffSerializerUtil.deserializeList(result, targetClass);
}

/**
* 精确删除key
*
* @param key
*/
public void deleteCache(String key) {
jedisCluster.del(key);
}

/**
* 模糊删除key
*
* @param pattern
*/
public void deleteCacheWithPattern(String pattern) {
Set<String> keys =this.keys(pattern);
for(String key:keys){
jedisCluster.del(key);
}
}

/**
* 清空所有缓存
*/
public void clearCache() {
deleteCacheWithPattern(RedisClusterCache.CAHCENAME+"|*");
}

/**
* 由于JedisCluster没有提供对keys命令的封装,只能自己实现
* @param pattern
* @return
*/
public Set<String> keys(String pattern){
Set<String> keys = new HashSet<>();
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
for(String k : clusterNodes.keySet()){
JedisPool jp = clusterNodes.get(k);
Jedis connection = jp.getResource();
try {
keys.addAll(connection.keys(pattern));
} catch(Exception e){
e.printStackTrace();
} finally{
//用完一定要close这个链接!!!
connection.close();
}
}
return keys;
}
}
18 changes: 16 additions & 2 deletions beautyssm_common/src/main/resources/redis.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,24 @@ redis.pool.testOnBorrow=true
redis.ip=192.168.11.99
redis.port=6379

#redis \u9AD8\u53EF\u7528\u914D\u7F6E
#redis \u9AD8\u53EF\u7528\u914D\u7F6E\uFF08\u57FA\u4E8Esentinel\u7684\u65B9\u5F0F\uFF09
#sentinel1.ip=192.168.11.100
#sentinel1.port=63791
#sentinel2.ip=192.168.11.101
#sentinel2.port=63792
#sentinel3.ip=192.168.11.102
#sentinel3.port=63792
#sentinel3.port=63792

#redis \u96C6\u7FA4\u9AD8\u53EF\u7528\u914D\u7F6E\uFF08\u57FA\u4E8Ecluster\u7684\u65B9\u5F0F\uFF09
#redis.ip1=192.168.11.100
#redis.port1=7111
#redis.ip2=192.168.11.101
#redis.port2=7112
#redis.ip3=192.168.11.102
#redis.port3=7113
#redis.ip4=192.168.11.103
#redis.port4=7114
#redis.ip5=192.168.11.104
#redis.port5=7115
#redis.ip6=192.168.11.105
#redis.port6=7116
36 changes: 36 additions & 0 deletions beautyssm_service_goods/src/main/resources/spring/spring-redis.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,42 @@
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>


<!-- JedisCluster 集群高可用配置 -->
<!--<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg index="0">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.ip1}" />
<constructor-arg index="1" value="${redis.port1}" type="int" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.ip2}" />
<constructor-arg index="1" value="${redis.port2}" type="int" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.ip3}" />
<constructor-arg index="1" value="${redis.port3}" type="int" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.ip4}" />
<constructor-arg index="1" value="${redis.port4}" type="int" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.ip5}" />
<constructor-arg index="1" value="${redis.port5}" type="int" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.ip6}" />
<constructor-arg index="1" value="${redis.port6}" type="int" />
</bean>
</set>
</constructor-arg>
<constructor-arg index="1" value="2000" type="int"></constructor-arg>
<constructor-arg index="2" value="100" type="int"></constructor-arg>
<constructor-arg index="3" ref="jedisPoolConfig"></constructor-arg>
</bean>-->

<!--redis Sentinel主从高可用方案配置 -->
<!-- <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
Expand Down

0 comments on commit 69e948e

Please sign in to comment.