6.redis集群

来源:互联网 发布:python课程视频 编辑:程序博客网 时间:2024/06/04 18:40

    考虑到互联网使用环境极其复杂,需要高效稳定的redis环境,redis3.0增加了cluster功能,可以给redis集群部署,这样的好处是性能稳定,但是缺点是非常重,运营不方便,并且至少需要部署6个节点,而且有坑。

   下面转载了一个同学的文章来搭建集群部署环境,http://www.cnblogs.com/Xrinehart/p/3502213.html。

import java.util.HashSet;import java.util.Set;import java.util.concurrent.atomic.AtomicBoolean;import java.util.concurrent.atomic.AtomicLong;import redis.clients.jedis.HostAndPort;import redis.clients.jedis.JedisCluster;import redis.clients.jedis.JedisPoolConfig;public class redisClusterConcurrent {    private static JedisCluster jedisCluster;    void init() {        JedisPoolConfig config = new JedisPoolConfig();          config.setMaxTotal(20);          config.setMaxIdle(2);          //创建节点        HostAndPort hp0 = new HostAndPort("192.168.21.171", 7000);          HostAndPort hp1 = new HostAndPort("192.168.21.171", 7001);          HostAndPort hp2 = new HostAndPort("192.168.21.171", 7002);          HostAndPort hp3 = new HostAndPort("192.168.21.171", 7003);          HostAndPort hp4 = new HostAndPort("192.168.21.171", 7004);          HostAndPort hp5 = new HostAndPort("192.168.21.171", 7005);            Set<HostAndPort> hps = new HashSet<HostAndPort>();          hps.add(hp0);          hps.add(hp1);          hps.add(hp2);          hps.add(hp3);          hps.add(hp4);          hps.add(hp5);            // 超时,最大的转发数,最大链接数,最小链接数都会影响到集群          jedisCluster = new JedisCluster(hps, 5000, 10, config);      }    public static void main(String[] args) throws InterruptedException {        final redisClusterConcurrent redisConcurrent = new redisClusterConcurrent();        redisConcurrent.init();        final AtomicLong counter = new AtomicLong(0);        final AtomicBoolean stopFlag = new AtomicBoolean(false);        final int threadCount = 1000;                for (int i = 0; i < threadCount; i++) {            final String sss = i + "";            new Thread(new Runnable() {                @Override                public void run() {                    while (!stopFlag.get()) {                        jedisCluster.set(sss, "aaasasdasdasdasdasdasdasdasdasdasdasdasdasaaaaaaaaaaaaaaaaaa");                        counter.incrementAndGet();                    }                }            }).start();        }        Runtime.getRuntime().addShutdownHook(new Thread() {            public void run() {                System.err.println("*** stop client ***");                stopFlag.set(true);            }        });        long startTime = System.currentTimeMillis();        while (!stopFlag.get()) {            Thread.sleep(1000);            final long count = counter.get();            long currentTime = System.currentTimeMillis();            long qps = count * 1000 / (currentTime - startTime);            System.out.println("qps=" + qps);            if ((currentTime - startTime) / 1000 > 10) {                counter.set(0);                startTime = currentTime;                System.out.println("reset counter");            }        }    }}
创建一个JedisCluster,JedisCluster封装了redis的命令,这样就可以直接调用操作redis。测试的qps大概4万左右,和ShardedJedisPool差不多,存储100个数,可能节点1、5一样,2、4的一样,但是每一个节点的数据都不会超过100个,按一种算法去算,当一个节点挂了,并不会影响程序的正常使用,当剩下2个节点后,程序会报错,这样保证了高可用高并发性。

0 0
原创粉丝点击