Redis Sentinel 实战-故障自动修复

整体配置

redis 
    master-7000 
    slave-7001 
    slave-7002

sentinel
    sentinel-26379
    sentinel-26380
    sentinel-26381

 

1、redis配置

master-7000 conf配置

port 7000
daemonize yes
pidfile /Users/liujinjie/app/redis-4.0.7/pid/redis-7000.pid
logfile "7000.log"
dir "/Users/liujinjie/app/redis-4.0.7/data"

slave-7001 conf配置

port 7001
daemonize yes
pidfile /Users/liujinjie/app/redis-4.0.7/pid/redis-7001.pid
logfile "7001.log"
dir "/Users/liujinjie/app/redis-4.0.7/data"
slaveof 127.0.0.1 7000

slave-7002 conf配置

port 7002
daemonize yes
pidfile /Users/liujinjie/app/redis-4.0.7/pid/redis-7002.pid
logfile "7002.log"
dir "/Users/liujinjie/app/redis-4.0.7/data"
slaveof 127.0.0.1 7000

 

2、sentinel配置

(1) sentinel-26379配置

port 26379
daemonize yes
dir /Users/liujinjie/app/redis-4.0.7/data
logfile "sentinel-26379.log"

# 主节点127.0.0.1 7000;2 是两个sentinel认为有问题就有问题
sentinel monitor mymaster 127.0.0.1 7000 2  
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

(2) sentinel-26380配置

port 26380
daemonize yes
dir /Users/liujinjie/app/redis-4.0.7/data
logfile "sentinel-26380.log"
sentinel monitor mymaster 127.0.0.1 7000 2  
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

(3) sentinel-26381配置

port 26381
daemonize yes
dir /Users/liujinjie/app/redis-4.0.7/data
logfile "sentinel-26381.log"
sentinel monitor mymaster 127.0.0.1 7000 2  
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000


3、启动redis

redis-server conf/redis-7000.conf

redis-server .conf/redis-7001.conf

redis-server conf/redis-7002.conf

看启动状态
./redis-cli -p 7000 ping
redis-cli -p 7000 info replication


# 将#注释的行和空行去掉

cat sentinel.conf | grep -v "#" | grep -v "^$"

#将redis-7000.conf中的7000替换为7001然后写到文件redis-7001.conf

sed "s/7000/7001/g" redis-7000.conf > redis-7001.conf

#将slaveof 127.0.0.1 7000追加到文件redis-7001.conf后面

echo "slaveof 127.0.0.1 7000" >> redis-7001.conf

#查询redis-server端口号26开头的进程号,然后kill

ps -ef grep redis-server | grep 26 | awk '{print $2}' | xargs kill


4、启动sentinel

redis-sentinel redis-sentinel-26379.conf

redis-server conf/redis-sentinel-26379.conf --sentinel


redis-server conf/redis-sentinel-26379.conf --sentinel --protected-mode no
redis-server conf/redis-sentinel-26380.conf --sentinel --protected-mode no
redis-server conf/redis-sentinel-26381.conf --sentinel --protected-mode no


redis-cli -p 26379
127.0.0.1:26379> info

127.0.0.1:26379> info sentinel

 

5、java

(1) 导入依赖

<dependencies>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.1</version>
    </dependency>
</dependencies>

(2) java客户端代码

package com.moon.demo.redis.sentinel.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class RedisSentinelFailoverTest {

    private static Logger logger = LoggerFactory.getLogger(RedisSentinelFailoverTest.class);

    public static void main(String[] args) {
        String masterName = "mymaster";
        Set<String> sentinel = new HashSet<String>();
        sentinel.add("127.0.0.1:26379");
        sentinel.add("127.0.0.1:26380");
        sentinel.add("127.0.0.1:26381");

        JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinel);

        while (true) {
            Jedis jedis = null;
            try {
                jedis = jedisSentinelPool.getResource();
                int index = new Random().nextInt(100000);
                String key = "k-" + index;
                String value = "v-" + index;
                jedis.set(key, value);
                logger.info("{} value is {}", key, jedis.get(key));
                TimeUnit.MILLISECONDS.sleep(1000);
            } catch (Exception e) {
                logger.error("", e);
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
        }
    }
}

 

转载于:https://my.oschina.net/u/3777515/blog/1629041

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值