總體配置java
redis master-7000 slave-7001 slave-7002 sentinel sentinel-26379 sentinel-26380 sentinel-26381
一、redis配置redis
master-7000 conf配置api
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配置app
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配置dom
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
二、sentinel配置code
(1) sentinel-26379配置server
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配置ci
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
三、啓動redisget
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
四、啓動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
五、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(); } } } } }