redis 主从备份自动切换+java代码实现类

目录(?)[-]

  1. redis-0sentinel实例之间的通讯端口
  2. master2 可以添加多组主从的redis监听
  3. Replication
  4. Sentinel
 

阅读本文您会学会: 
1.Redis 主从备份并自动切换(master slaver)模式的搭建 
2.Java 中 配合redis主从备份的方法 
【实现目标】: 
master redis 正常运行 
slaver redis 自动备份 master 的所有数据 
当master redis 冗掉 slaver 自动升级为master 接替原来master 的工作 
当原master 重启后 自动加入主从备份,原master 会变成新的slaver。 
【环境配置】: 
此类集群配置需要至少三台机器(当然,可以是虚拟机),三台机器分别安装同样的redis 环境,并配置好ip,及port 
ip分别为:

* 192.168.9.17(redis sentinel 集群监控)
* 192.168.9.18(redis 主)
* 192.168.9.19(redis 从)

chmod 
【redis 配置】 
主:

* 主的redis 配置文件,使用默认的配置文件就可以,当然配置一下更好
* 
    * 此处主要配置的有端口号   port 6379
    * 后台启动   daemonize yes
    * 日志位置,注意要自己新建.log类型文件     logfile"home\oracle\redis\text.log"

从:

* 从的redis 配置文件
* 
    * 同主的三个配置属性
    * 增加master 配置     slaveof 192.168.9.18 6379

sentinel:

* 从安装文件目录下复制 sentinel.conf 文件到软件目录
* 编辑该文件,可将原来的文件中属性文字等清空,再加上下列文字

redis-0##sentinel实例之间的通讯端口

port 26379#master1 
sentinel monitor master1 192.168.9.18 6379 1 
sentinel down-after-milliseconds master1 5000 
sentinel failover-timeout master1 900000 
sentinel parallel-syncs master1 2

master2 可以添加多组主从的redis监听…….

… 
.. 
..

【启动】

* 一定要按顺序启动redis,先启动主,再启动从,最后启动sentinel
* 
    * ./redis-server ./redis.conf
    * ./redis-sentinel ./sentinel.conf --sentinel 
    * ps:如果提示-bash 权限不够 则使用chmod 777 ./redis.conf 这个命令 执行所有需要运行的文件  然后再正常启动就可以了。

* 启动后 可以查看redis 的信息
* ./redis-cli -h 192.168.9.18 -p 6379 info 查看redis信息

Replication

role:slave #代表192.168.9.18:6379 这台redis是主 
master_host:192.168.9.18 
master_port:6379 
master_link_status:up 
master_last_io_seconds_ago:4 
master_sync_in_progress:0 
slave_priority:100 
slave_read_only:1 
connected_slaves:0

* ./redis-cli -h 192.168.9.17 -p 26379 info  查看sentinel 集群信息,如下则表示配置成功

Sentinel

sentinel_masters:1 
sentinel_tilt:0 
sentinel_running_scripts:0 
sentinel_scripts_queue_length:0 
master0:name=master1,status=ok,address=192.168.9.18:6379,slaves=1,sentinels=1 
【故障演示】

* 主redis 冗掉后    ./redis-cli -h 192.168.9.18 -p 6379 shutdown 
* 则发现redis 自动把slaver机器升级为master ,而重新启动原来的master服务,则原master服务会变成新的slaver。
* 注意:如果master 发生迁移后,需要重新启动所有redis 的情况下,必须最先启动“新的”master节点,否则sentinel 会一直找不到master。如果想要停止sentinel,可输入./redis-cli -p 26379 shutdown

【代码处配合自动切换】 
上述部分是服务器部分自动切换,还需要程序处修改redis配置,此处我们使用jedis 进行演示。 
使用以下配置类即可 
核心代码在于 
Set sentinels = new HashSet(); 
// 此处放置ip及端口为 sentinel 
// 服务地址,如果有多个sentinel 则逐一add即可 
sentinels.add(“80.27.112.118:26379”); 
jedisPool = new JedisSentinelPool(“master1”,sentinels); 
以下代码为工具类 通过配置后可以直接使用

package com.aisino.redis;

import java.util.HashSet; 
import java.util.List; 
import java.util.Set;

import com.aisino.spring.util.SystemConfig;

import redis.clients.jedis.Jedis; 
import redis.clients.jedis.JedisPool; 
import redis.clients.jedis.JedisPoolConfig; 
import redis.clients.jedis.JedisSentinelPool;

public class JedisClusterUtil {

static {
    // redis 属性配置 start
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(5000);
    config.setMaxIdle(256);
    config.setMaxWaitMillis(5000L);
    config.setTestOnBorrow(true);
    config.setTestOnReturn(true);
    config.setTestWhileIdle(true);
    config.setMinEvictableIdleTimeMillis(60000L);
    config.setTimeBetweenEvictionRunsMillis(3000L);
    config.setNumTestsPerEvictionRun(-1);
    // redis 属性配置 end

    Set sentinels = new HashSet();
    sentinels.add("80.27.112.118:26379"); // 此处放置ip及端口为 sentinel
                                            // 服务地址,如果有多个sentinel 则逐一add即可
    jedisPool = new JedisSentinelPool("master1", sentinels, config);
}

private static JedisSentinelPool jedisPool;

public static String get(String key) {
    String value = null;
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        value = jedis.get(key);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
    return value;
}

public static void close(Jedis jedis) {
    try {
        jedisPool.returnResource(jedis);
    } catch (Exception e) {
        if (jedis.isConnected()) {
            jedis.quit();
            jedis.disconnect();
        }
    }
}

public static byte[] get(byte[] key) {
    byte[] value = null;
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        value = jedis.get(key);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }

    return value;
}

public static void set(String key, String value) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.set(key, value);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
}

public static void set(String key, String value, int time) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.set(key, value);
        jedis.expire(key, time);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
}

public static void set(byte[] key, byte[] value, int time) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.set(key, value);
        jedis.expire(key, time);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
}

public static void hset(byte[] key, byte[] field, byte[] value) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.hset(key, field, value);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
}

public static void hset(String key, String field, String value) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.hset(key, field, value);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
}

public static String hget(String key, String field) {
    String value = null;
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        value = jedis.hget(key, field);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }

    return value;
}

public static byte[] hget(byte[] key, byte[] field) {
    byte[] value = null;
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        value = jedis.hget(key, field);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }

    return value;
}

public static void hdel(byte[] key, byte[] field) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.hdel(key, new byte[][] { field });
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
}

public static void lpush(String key, String value) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.lpush(key, new String[] { value });
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
}

public static void rpush(byte[] key, byte[] value) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.rpush(key, new byte[][] { value });
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
}

public static void rpoplpush(byte[] key, byte[] destination) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.rpoplpush(key, destination);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
}

public static List lpopList(byte[] key) {
    List list = null;
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        list = jedis.lrange(key, 0L, -1L);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }

    return list;
}

public static String rpop(String key) {
    String bytes = null;
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        bytes = jedis.rpop(key);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }

    return bytes;
}

public static List lrange(byte[] key, int from, int to) {
    List result = null;
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        result = jedis.lrange(key, from, to);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }

    return result;
}

public static void del(String key) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.del(key);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
}

public static long llen(byte[] key) {
    long len = 0L;
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.llen(key);
    } catch (Exception e) {
        jedisPool.returnBrokenResource(jedis);
        e.printStackTrace();
    } finally {
        close(jedis);
    }
    return len;
}

}

转载于:https://www.cnblogs.com/stupidMartian/p/6650773.html

你可能感兴趣的:(redis 主从备份自动切换+java代码实现类)