整体配置
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();
}
}
}
}
}