操作系统:centos 6.3

redis版本:3.0.6

java客户端版本: jedis 2.7.2

redis客户端图形用户界面:RedisDesktopManager

 

1.redis服务端集群搭建步骤:

  1.下载redis安装包,进行解压安装

  2.安装ruby、rubygems install ruby ,安装ruby的原因是,在进行集群的时候,使用的是ruby语言工具实现的,所以在集群之前首先需要搭建ruby的环境

  3.在上述步骤完成之后,便可以搭建集群环境,redis提供了两种集群搭建方法,执行脚本方法(安装包下面的util包中)和手动搭建。

  注意:

  1.在集群的时候,如果是远端客户端访问redis服务端,那么在分片的时候,需要使用Ip进行分片,下面会详细说

  2.在创建每个节点的时候,不要只用redis-server ,使用绝对路径下的redis-server xxx

  

2.客户端(java):

注意:

1.本文的客户端使用的是java,官网中对于java客户端也提供了不少的client,但是本文使用的是官方推荐的jedis。

2.在项目开发中,一般情况下都会用到spring来管理应用,本文也是如此,spring 本身也提供了对redis的集成支持,具体的网址:http://projects.spring.io/spring-data-redis,

 但是好像目前spring-data-redis不提供集群的功能,所以本文没有使用它,而是使用了原装的jedis来进行开发,如果在项目中没有用到集群的功能,则可以使用spirng-data-redis。

 

下面是具体的代码实现

1.maven依赖


<dependency>
		    <groupId>redis.clients</groupId>
		    <artifactId>jedis</artifactId>
		    <version>2.7.2</version>
		</dependency>

2.applicationContext.xml中的配置


<!-- jedis cluster config -->
    <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" >
			<property name="maxWaitMillis" value="-1" />
			<property name="maxTotal" value="1000" />
			<property name="minIdle" value="8" />
			<property name="maxIdle" value="100" />
	</bean>

	<bean id="jedisCluster" class="com.besttone.subscribe.util.JedisClusterFactory">
		<property name="addressConfig">
			<value>classpath:redis-config.properties</value>
		</property>
		<property name="addressKeyPrefix" value="address" />  
		
		<property name="timeout" value="300000" />
		<property name="maxRedirections" value="6" />
		<property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
	</bean>

 

 

3.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;
    }

}

 

4.redis-config.properties文件

 

   这是一个集群环境,六个节点(不同端口),三个master ,三个slaver

 

address1=192.168.30.139:7000
address2=192.168.30.139:7001
address3=192.168.30.139:7002
address4=192.168.30.139:7003
address5=192.168.30.139:7004
address6=192.168.30.139:7005

 

5.项目目录图

 

 

datebase java配置redis java配置redis集群_ruby


 

 

6.代码中使用(此代码为从redis中获取相关信息)

 

 

datebase java配置redis java配置redis集群_redis_02


 

 

ok,运行之后,会发现redis会根据不同的key,把它们放入到不同的节点中,如下图

 

 

7.三个master节点中的数据

 

datebase java配置redis java配置redis集群_数据库_03


 

 8.三个slave节点中的数据

 

datebase java配置redis java配置redis集群_数据库_04


 

 

 

实践过程中碰到的问题:

 

1.在一切准备好了之后,在操作redis的时候,却报错误:Too many Cluster redirections

由于,我是windows开发环境,在本机开了一个虚拟机,然后在虚拟机中搭建的linux集群环境,本机的ip和虚拟机中的ip不相同,所以报这个错误,

解决方法:在redis集群搭建过程中,在为每个节点分hash槽的时候,执行如下代码(其中,xxx为集群环境中的ip):

./redis-trib.rb create --replicas 1 xxx.xxx.xxx.xxx:7000  xxx.xxx.xxx.xxx:7001 xxx.xxx.xxx.xxx:7002 xxx.xxx.xxx.xxx:7003 xxx.xxx.xxx.xxx:7004 xxx.xxx.xxx.xxx:7005./redis-trib.rb create --replicas 1 127.0.0.1:7000  127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

 

 

 ok,以上满足使用,结束!!