1、在项目中我们用redis,如果主节点宕机,,这是正在操作redis的服务就会立即暂停,之前操作的数据可能出现异常。
因此在Java客户端实现单点故障时很有必要的。
2、首先我们用到Jedis包中JedisSentinelPool监控连接池,我们通过连接redis监控集群来动态去解析redis集群主节点的信息,然后创建redis连接。
private static JedisSentinelPool jedisPool = null;private static String master = "redis_master";public static String host = "192.168.100.24";public static int port = 26379;public static int database = 0;public static int timeout = 0;public static void main(String[] args) {Set<String> sentinels = new HashSet<String>();sentinels.add(new HostAndPort(host,port).toString());jedisPool = new JedisSentinelPool(master, sentinels,new JedisPoolConfig(),timeout,null,database);try {test();} catch (InterruptedException e) {e.printStackTrace();}}上面代码是连接redis监控集群的方法。
只要有信心,人永远不会挫败