1、maven依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
2、spring配置JedisCluster
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:redis.properties" />
</bean>
<!-- jedis 配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >
<!--最大空闲数-->
<property name="maxIdle" value="${redis.maxIdle}" />
<!--最大建立连接等待时间-->
<property name="maxWaitMillis" value="${redis.maxWait}" />
<!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="minIdle" value="${redis.minIdle}" />
</bean >
<bean id="jedisCluster" class="com.jsun.service.redis.impl.JedisClusterFactory" >
<property name="addressConfig">
<value>classpath:redis.properties</value>
</property>
<property name="addressKeyPrefix" value="cluster" /> <!-- 属性文件里 key的前缀 -->
<property name="timeout" value="300000" />
<property name="maxRedirections" value="6" />
<property name="genericObjectPoolConfig" ref="poolConfig" />
</bean >
</beans>
3、redis.properties配置
#最大空闲数
redis.maxIdle=100
#最大连接数
redis.maxActive=300
#最大建立连接等待时间
redis.maxWait=1000
#客户端超时时间单位是毫秒
redis.timeout=100000
redis.maxTotal=1000
redis.minIdle=8
#明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.testOnBorrow=true
#cluster
cluster1.host.port=119.254.166.136:7031
cluster2.host.port=119.254.166.136:7032
cluster3.host.port=119.254.166.136:7033
cluster4.host.port=119.254.166.136:7034
cluster5.host.port=119.254.166.136:7035
cluster6.host.port=119.254.166.136:7036
#cluster
4、JedisClusterFactory
public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {
private Resource addressConfig;
private String addressKeyPrefix ;
private JedisCluster jedisCluster;
private Integer timeout;
private Integer maxRedirections;
private GenericObjectPoolConfig genericObjectPoolConfig;
private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");
@Override
public JedisCluster getObject() throws Exception {
return jedisCluster;
}
@Override
public Class<? extends JedisCluster> getObjectType() {
return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
}
@Override
public boolean isSingleton() {
return true;
}
private Set<HostAndPort> parseHostAndPort() throws Exception {
try {
Properties prop = new Properties();
prop.load(this.addressConfig.getInputStream());
Set<HostAndPort> haps = new HashSet<HostAndPort>();
for (Object key : prop.keySet()) {
if (!((String) key).startsWith(addressKeyPrefix)) {
continue;
}
String val = (String) prop.get(key);
boolean isIpPort = p.matcher(val).matches();
if (!isIpPort) {
throw new IllegalArgumentException("ip 或 port 不合法");
}
String[] ipAndPort = val.split(":");
HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));
haps.add(hap);
}
return haps;
} catch (IllegalArgumentException ex) {
throw ex;
} catch (Exception ex) {
throw new Exception("解析 jedis 配置文件失败", ex);
}
}
@Override
public void afterPropertiesSet() throws Exception {
Set<HostAndPort> haps = this.parseHostAndPort();
jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);
}
public void setAddressConfig(Resource addressConfig) {
this.addressConfig = addressConfig;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public void setMaxRedirections(int maxRedirections) {
this.maxRedirections = maxRedirections;
}
public void setAddressKeyPrefix(String addressKeyPrefix) {
this.addressKeyPrefix = addressKeyPrefix;
}
public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
this.genericObjectPoolConfig = genericObjectPoolConfig;
}
}
5、单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config.xml")
public class JedeisClusterTest {
@Autowired
private JedisCluster jedisCluster;
@Test
public void testJedisCluster(){
jedisCluster.set("name", "啊芝");
String val = jedisCluster.get("name");
System.out.println(val);
}
}
6、问题总结
1)、redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections?
报出这个问题首先要确定spring配置文件中的maxRedirections属性是否配置并大于0,其次可以参照 http://blog.csdn.net/wzl19870309/article/details/50994234 文中进行响应的调整。
2)、程序的归程序,redis服务的归redis服务
有的时候我们会提出这样的问题,在单节点主从+哨兵配置或者多节点集群+哨兵配置中,如果主挂了,哨兵自动进行主从替换,程序如何完成主从替换?
对于这样的问题,始终要记住【程序的归程序,redis服务的归redis服务】
具体的理解是:比如程序连接配置了主服务器的连接,但是此时主服务器挂调,哨兵会将从服务器变成主服务器,但程序是不知道的,出现无法连接异常;重启宕掉的服务,将作为从服务器运行,默认不可写,程序出现写异常;虽然redis服务机制比较完善,但是程序并未做到响应变化
对于单节点主从来说,从节点主要作为灾备服务来对待,主服务挂掉之后,用从服务同步的数据还原主服务数据。多节点集群同理。
3)、JedisCluster管理集群与ShardedJedisPool分片连接池实现分布式的区别
ShardedJedisPool是redis没有集群功能之前客户端实现的一个数据分布式方案,redis3.0提供集群之后,客户端则采用JedisCluster实现连接redis集群环境。
ShardedJedisPool使用的是JedisShardInfo的instance的顺序或者name来做的一致性哈希,JedisCluster使用的是CRC16算法来做的哈希槽。
redis cluster配置集群与使用Jedis的ShardedJedis做Redis集群的区别
4)、集群环境,各个服务之间的数据是隔离的
无论是ShardedJedisPool的一致性哈希算法还是JedisCluster的CRC16哈希槽算法,都是把所有的服务叠加然后进行均匀的分割,分割出来的每一个段或槽都是不重复的,所以导致存储的数据彼此之间也是处于隔离状态的。
5)、jediscluster并不能实现客户端程序高可用
如果集群环境中,某一个master挂掉,交由jediscluster管理的集群访问程序,必然会出现异常,所以jediscluster并未实现集群环境下的高可用,只是实现了分布式,只有重启服务重新初始化新的集群环境,程序方可正常运行。
详细信息可以查看下一篇文章 分布式缓存技术redis学习系列(八)——JedisCluster源码解读:集群初始化、slot(槽)的分配、值的存取