Redis的cluster集群

目前Redis实现集群的方法主要是采用一致性哈稀分片(Shard),将不同的key分配到不同的redis server上,达到横向扩展的目的。
对于一致性哈稀分片的算法,Jedis-2.0.0已经提供了,下面是使用示例代码(以ShardedJedisPool为例):
public class RedisShardPoolTest {

        static ShardedJedisPool pool;

        static {

                JedisPoolConfig config = new JedisPoolConfig();// Jedis池配置

                config.setMaxActive(500);// 最大活动的对象个数

                config.setMaxIdle(1000 * 60);// 对象最大空闲时间

                config.setMaxWait(1000 * 10);// 获取对象时最大等待时间

                config.setTestOnBorrow(true);

                String hostA = "127.0.0.1";

                int portA = 6378;

                String hostB = "127.0.0.1";

                int portB = 6379;

                List<JedisShardInfo> jdsInfoList = new ArrayList<JedisShardInfo>(2);

                JedisShardInfo infoA = new JedisShardInfo(hostA, portA);

                infoA.setPassword("testpass");

                JedisShardInfo infoB = new JedisShardInfo(hostB, portB);

                infoB.setPassword("testpass");

                jdsInfoList.add(infoA);

                jdsInfoList.add(infoB);

                pool = new ShardedJedisPool(config, jdsInfoList, Hashing.MURMUR_HASH,

                Sharded.DEFAULT_KEY_TAG_PATTERN);


        }

        /**
         * 
         * @param args
         * 
         */

        public static void main(String[] args) {

                for (int i = 0; i < 100; i++) {

                        String key = generateKey();
                        ShardedJedis jds = null;

                        try {
                                jds = pool.getResource();

                                System.out.println(key + ":"
                                                + jds.getShard(key).getClient().getHost());
                                System.out.println(key + ":"
                                                + jds.getShard(key).getClient().getPort());

                                System.out.println(jds.get(key));
                                System.out.println(jds.set(key,"1111111111111111111111111111111"));
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                        finally {
                                pool.returnResource(jds);

                        }
                }
        }

        private static int index = 1;

        public static String generateKey() {

                return String.valueOf(Thread.currentThread().getId()) + "_" + (index++);

        }

}

从运行结果中可以看到,不同的key被分配到不同的Redis-Server上去了。

上面的集群模式还存在两个问题:

 

1.      扩容问题:


因为使用了一致性哈稀进行分片,那么不同的key分布到不同的Redis-Server上,当我们需要扩容时,需要增加机器到分片列表中,这时候会使得同样的key算出来落到跟原来不同的机器上,这样如果要取某一个值,会出现取不到的情况,对于这种情况,Redis的作者提出了一种名为Pre-Sharding的方式:


Pre-Sharding方法是将每一个台物理机上,运行多个不同断口的Redis实例,假如有三个物理机,每个物理机运行三个Redis实际,那么我们的分片列表中实际有9Redis实例,当我们需要扩容时,增加一台物理机,步骤如下:


A.    在新的物理机上运行Redis-Server


B.     Redis-Server从属于(slaveof)分片列表中的某一Redis-Server(假设叫RedisA);


C.     等主从复制(Replication)完成后,将客户端分片列表中RedisAIP和端口改为新物理机上Redis-ServerIP和端口;


D.    停止RedisA


这样相当于将某一Redis-Server转移到了一台新机器上。Prd-Sharding实际上是一种在线扩容的办法,但还是很依赖Redis本身的复制功能的,如果主库快照数据文件过大,这个复制的过程也会很久,同时会给主库带来压力。所以做这个拆分的过程最好选择为业务访问低峰时段进行。




2.      单点故障问题:


还是用到Redis主从复制的功能,两台物理主机上分别都运行有Redis-Server,其中一个Redis-Server是另一个的从库,采用双机热备技术,客户端通过虚拟IP访问主库的物理IP,当主库宕机时,切换到从库的物理IP。只是事后修复主库时,应该将之前的从库改为主库(使用命令slaveofno one),主库变为其从库(使命令slaveofIP PORT),这样才能保证修复期间新增数据的一致性



最终部署的情况会是,分布式的每台server 会有一个对应的备机(从机),这样即使有一个分布式的片机挂掉,对应的备机会接管,不会导致因为片机挂掉导致部分数据不能写进或取出的问题

附件是一份windows下redis服务端程序和客户端需要的jar包,附件下载后后缀改为.rar后解压缩
可以自己配置分布式和主从测试,
配置文件着重关注以下配置
服务端口和 绑定网卡IP
# Accept connections on the specified port, default is 6379
port 6178

# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for connections.
#
# bind 127.0.0.1


是否为备机(IP:端口)
################################# REPLICATION #################################

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. Note that the configuration is local to the slave
# so for example it is possible to configure the slave to save the DB with a
# different interval, or to listen to another port, and so on.
#
slaveof 127.0.0.1 6378

认证密码
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#

最好深读官方的解决方案http://redis.io/topics/partitioning

原文地址:https://www.cnblogs.com/google4y/p/3360498.html