4.Redis分布式连接和主从复制

一.Redis的分布式

Redis本身不支持分布式,通过Jedis客户端可以实现使用Redis分布式。

package com.gqshao.redis.connection;


import com.google.common.collect.Lists;
import com.gqshao.redis.utils.JedisUtils;
import org.junit.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.*;

import java.util.Arrays;
import java.util.List;


/**
 * Jedis 独有的分布式
 * 根据List<JedisShardInfo>中Redis顺序和key,指定固定的Redis
 * 扩容时使用 Pre-Sharding
 */
public class ShardedJedisTest {

    protected static Logger logger = LoggerFactory.getLogger(ShardedJedisTest.class);

    private static ShardedJedisPool pool = null;
    protected ShardedJedis jedis = null;


    @BeforeClass
    public static void createPool() {
        JedisPoolConfig poolConfig = JedisUtils.getMyDefaultJedisPoolConfig();
//        List<JedisShardInfo> shards = Arrays.asList(new JedisShardInfo("192.168.3.98", 6379)
//                , new JedisShardInfo("192.168.3.98", 6380));
        List<JedisShardInfo> shards = Lists.newArrayList();
        JedisShardInfo info1 = new JedisShardInfo("192.168.3.98", 6379);
        JedisShardInfo info2 = new JedisShardInfo("192.168.3.98", 6380);
        // 添加的顺序有意义
        shards.add(info1);
        shards.add(info2);
        pool = new ShardedJedisPool(poolConfig, shards);
    }

    @Before
    public void getResource() {
        jedis = pool.getResource();
    }

    @After
    public void destroyJedis() {
        pool.returnResource(jedis);
    }

    @AfterClass
    public static void destroyPool() {
        pool.destroy();
    }

    /**
     * 不同的key分配根据List<JedisShardInfo>中的顺序,分配不同的Redis连接
     */
    @Test
    public void testSet() {
        for (int i = 0; i < 20; i++) {
            try {
                logger.info(i + " - " + jedis.getShard("" + i).getClient().getHost()
                        + " : " + jedis.getShard(i + "").getClient().getPort());
                logger.info(jedis.set("" + i, "true"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 修改List<JedisShardInfo>中的顺序,无法得到结果,确定该顺序是有意义的
     */
    @Test
    public void testGet() {
        for (int i = 0; i < 20; i++) {
            try {
                logger.info(i + " - " + jedis.getShard(i + "").getClient().getHost()
                        + ":" + jedis.getShard(i + "").getClient().getPort() + " ? " + jedis.get("" + i));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

 

二.Redis主从复制

redis的主重复制有两种方式,第一种是写在.conf配置文件中,第二种是通过命令slaveof ip port 或 slaveof no one

slaveof no one 之后,当前Redis节点提升为Master但之前数据不会丢失,所以通过程序动态的设置主从复制可以解决Redis单点故障的问题

package com.gqshao.redis.connection;


import com.gqshao.redis.utils.JedisUtils;
import org.junit.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


/**
 * slaveof 和 slaveofNoOne方法可以在运行中动态设置服务器为Master或Slave
 */
public class SlaveofTest {

    protected static Logger logger = LoggerFactory.getLogger(SlaveofTest.class);

    private static JedisPool pool1 = null;
    protected Jedis jedis1 = null;

    private static JedisPool pool2 = null;
    protected Jedis jedis2 = null;


    @BeforeClass
    public static void init() {
        JedisPoolConfig poolConfig = JedisUtils.getMyDefaultJedisPoolConfig();
        // timeout设置为0,解决JedisConnectionException
        pool1 = new JedisPool(poolConfig, "192.168.3.98", 6379, 0);
        pool2 = new JedisPool(poolConfig, "192.168.3.98", 6380, 0);
        logger.info("----------------------create resource----------------------");
    }

    @AfterClass
    public static void destroy() {
        pool1.destroy();
        pool2.destroy();
    }

    @Before
    public void initJedis() {
        jedis1 = pool1.getResource();
        jedis2 = pool2.getResource();
    }

    @After
    public void destroyJedis() {
        pool1.returnResource(jedis1);
        pool2.returnResource(jedis2);
    }

    /**
     * 测试主从复制 注意设置Slave之后需要时间复制数据
     */
    @Test
    public void test() {
        // 设置 Redis2为Redis1的slave
        logger.info("jedis1 slaveof no one");
        jedis1.slaveofNoOne();
        logger.info("jedis2 slaveof 192.168.3.98 6379");
        jedis2.slaveof("192.168.3.98", 6379);

        try {
            jedis2.set("key", "value");
        } catch (Exception e) {
            logger.warn("由于设置的原因slave为read only");
        }

        // 主从复制 Redis1中保存数据Redis2中可以读取
        logger.info("jedis1.set mykey1 myvalue1 : " + jedis1.set("mykey1", "myvalue1"));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("jedis2.get mykey1: " + jedis2.get("mykey1"));

        // 单点问题解决思路(思路启用Redis2为Master,Redis1为SlaveOf Redis1)
        // 提升Redis2为Master,读取刚才的数据,重启Redis1,将Redis2设置为Redis1的Master
        jedis2.slaveofNoOne();
        // 测试值仍然存在
        logger.info("jedis2.get again : " + jedis2.get("mykey1"));
        // Redis2中存入数据
        logger.info("jedis2.set mykey2 myvalue2 : "+jedis2.set("mykey2", "myvalue2"));
        // Redis1设置为Redis2的slave
        jedis1.slaveof("192.168.3.98", 6380);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 测试jedis1中读取数据
        logger.info("jedis1.get mykey2 : " + jedis1.get("mykey2"));

        logger.info("jedis1 slaveof no one");
        jedis1.slaveofNoOne();
        jedis1.del("mykey1", "mykey2");
        jedis2.del("mykey1", "mykey2");
    }

}

 

 

 

 

你可能感兴趣的:(redis,jedis,slave,master)