redis的sentinel主从切换(failover)与Jedis线程池自动重连

Posted 扎心了老铁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis的sentinel主从切换(failover)与Jedis线程池自动重连相关的知识,希望对你有一定的参考价值。

本文介绍如何通过sentinel监控redis主从集群,并通过jedis自动切换ip和端口。

1、配置redis主从实例

10.93.21.21:6379

10.93.21.21:6389

10.93.21.21:6399

主从同步关系

master:10.93.21.21:6379

slave:10.93.21.21:6389,10.93.21.21:6399

master配置如下:

# 实例ip和端口
bind 10.93.21.21
port 6379
# pid文件
pidfile redis_6379.pid
# 日志文件
logfile "/home/data_monitor/redis-3.2.9/log/6379.log"
# 持久化数据文件dir
dir /home/data_monitor/redis-3.2.9/data
# rdb 文件地址
dbfilename 6379.rdb
# aof 文件地址
appendfilename "6379.aof"

slave配置如下(以6389为例)

# 实例ip和端口
bind 10.93.21.21
port 6389
# master节点配置
slaveof 10.93.21.21 6379
# pid文件
pidfile redis_6389.pid
# 日志文件
logfile "/home/data_monitor/redis-3.2.9/log/6389.log"
# 持久化数据文件dir
dir /home/data_monitor/redis-3.2.9/data
# rdb 文件地址
dbfilename 6389.rdb
# aof 文件地址
appendfilename "6389.aof"

启动redis

bin/redis-server conf/6379.conf
bin/redis-server conf/6389.conf
bin/redis-server conf/6399.conf

 验证一下

master

$ bin/redis-cli -h 10.93.21.21 -p 6379
10.93.21.21:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.93.21.21,port=6389,state=online,offset=571888,lag=1
slave1:ip=10.93.21.21,port=6399,state=online,offset=571888,lag=1
master_repl_offset:571888
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:571887

slave

$ bin/redis-cli -h 10.93.21.21 -p 6389
10.93.21.21:6389> info replication
# Replication
role:slave
master_host:10.93.21.21
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:530211
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

 

2、配置sentinel集群

10.93.21.21:26379

10.93.21.21:26389

10.93.21.21:26399

启动所有的节点,只要监控同一个redis master,启动的话自动连接成集群

# sentinel注册的IP和端口
bind 10.93.21.21
port 26379
# working目录
dir /home/data_monitor/redis-3.2.9/sentinel
# sentinel监控哪个主节点
sentinel monitor mymaster 10.93.21.21 6379 1
# 主节点挂掉多长时间,判定为挂掉,开始failover
sentinel down-after-milliseconds mymaster 10000
# failover交由几个sentinel执行
sentinel parallel-syncs mymaster 1
# failover多长时间没完成,超时失败
sentinel failover-timeout mymaster 180000

启动sentinel集群

bin/redis-sentinel conf/s26379.conf
bin/redis-sentinel conf/s26389.conf
bin/redis-sentinel conf/s26399.conf

 

3、jedis自动切换ip和端口

先解决依赖:pom.xml

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.0.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.7.0</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>    

连接池代码

public class JedisPoolUtil {
     
    private static JedisSentinelPool pool = null;
 
    public static Properties getJedisProperties() {
 
        Properties config = new Properties();
        InputStream is = null;
        try {
            is = JedisPoolUtil.class.getClassLoader().getResourceAsStream("cacheConfig.properties");
            config.load(is);
        } catch (IOException e) {
            logger.error("", e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    logger.error("", e);
                }
            }
        }
        return config;
    }
 
    /**
     * 创建连接池
     *
     */
    private static void createJedisPool() {
        // 建立连接池配置参数
        JedisPoolConfig config = new JedisPoolConfig();
        Properties prop = getJedisProperties();
        // 设置最大连接数
        config.setMaxTotal(StringUtil.nullToInteger(prop.getProperty("MAX_ACTIVE")));
        // 设置最大阻塞时间,记住是毫秒数milliseconds
        config.setMaxWaitMillis(StringUtil.nullToInteger(prop.getProperty("MAX_WAIT")));
        // 设置空间连接
        config.setMaxIdle(StringUtil.nullToInteger(prop.getProperty("MAX_IDLE")));
        // jedis实例是否可用
        boolean borrow = prop.getProperty("TEST_ON_BORROW") == "false" ? false : true;
        config.setTestOnBorrow(borrow);
        // 创建连接池
//      pool = new JedisPool(config, prop.getProperty("ADDR"), StringUtil.nullToInteger(prop.getProperty("PORT")), StringUtil.nullToInteger(prop.getProperty("TIMEOUT")));// 线程数量限制,IP地址,端口,超时时间
        //获取redis密码
        String password = StringUtil.nullToString(prop.getProperty("PASSWORD"));
 
         String masterName = "mymaster";
        Set<String> sentinels = new HashSet<String>();
        sentinels.add("192.168.137.128:26379");
        sentinels.add("192.168.137.128:26380");
        sentinels.add("192.168.137.128:26381");
        pool = new JedisSentinelPool(masterName, sentinels, config);
    }
 
    /**
     * 在多线程环境同步初始化
     */
    private static synchronized void poolInit() {
        if (pool == null)
            createJedisPool();
    }
 
    /**
     * 获取一个jedis 对象
     *
     * @return
     */
    public static Jedis getJedis() {
        if (pool == null)
            poolInit();
        return pool.getResource();
    }
 
    /**
     * 释放一个连接
     *
     * @param jedis
     */
    public static void returnRes(Jedis jedis) {
        pool.returnResource(jedis);
    }
 
    /**
     * 销毁一个连接
     *
     * @param jedis
     */
    public static void returnBrokenRes(Jedis jedis) {
        pool.returnBrokenResource(jedis);
    }
     
     
    public static void main(String[] args){
        Jedis jedis=getJedis();
         
    }
 
}

 

以上是关于redis的sentinel主从切换(failover)与Jedis线程池自动重连的主要内容,如果未能解决你的问题,请参考以下文章

Spring整合redis,通过sentinel进行主从切换

Redis技术探索「高可用架构模式」哨兵(sentinel)模式实现主从故障互切换模式详解

Redis 安装,主从配置及Sentinel配置自动Failover

Redis 安装,主从配置及Sentinel配置自动Failover

redis sentinel 工作原理

redis主从集群 主挂掉 java怎样调用从