Redis哨兵实现高可用
-
环境信息
系统版本:centos6.8 redis版本:redis-5.0.3.tar.gz 虚拟机地址:192.168.57.201(主)、192.168.57.202(从)、192.168.57.203(从) 哨兵:192.168.57.201、192.168.57.202、192.168.57.203
-
配置哨兵
# 201 202 203 sentinel monitor mymaster 192.168.57.201 6379 2
-
redis(主从复制)集群环境
-
201主节点
-
202、203从节点
#在202、203从服务器中添加,(主服务器不需要修改) # replicaof <masterip> <masterport> replicaof 192.168.57.201 6379
-
-
启动redis主从集群
# 201 202 203 ./redis-server ../redis.conf
-
启动redis哨兵
# 201 202 203 ./redis-server ../sentinel.conf --sentinel
-
测试redis哨兵高可用
netstat -ntlp kill -9 13502
-
java连接reids哨兵测试程序
/** * * @ClassName: SentinelTest * @Description: TODO(哨兵集群,通过哨兵获取redis连接,操作缓存) * @author: 一点课堂 * @sites: www.yidiankt.com * @date: 2019年2月24日 */ public class SentinelTest { public static void main(String[] args) throws Exception { Set<String> hosts = new HashSet<String>(); hosts.add("192.168.57.201:26379"); // 配置多个哨兵 hosts.add("192.168.57.203:26379"); JedisSentinelPool pool = new JedisSentinelPool("mymaster", hosts); Jedis jedis = null; // for (int i = 0; i < 20; i++) { // Thread.sleep(2000); try { jedis = pool.getResource(); String v = "hello yidiankt"; jedis.set("test-sentinel", v); System.out.println(v + "-->" + jedis.get("test-sentinel").equals(v)); } catch (Exception e) { System.out.println(" [ exception happened]" + e); } // } } }