找回密码
 会员注册
查看: 14|回复: 0

浅析如何基于ZooKeeper实现高可用架构

[复制链接]

2万

主题

0

回帖

7万

积分

超级版主

积分
71625
发表于 2024-10-8 13:51:48 | 显示全部楼层 |阅读模式
public class Node { public static final Node INSTANCE = new Node(); private volatile String role = "slave"; private CuratorFramework curatorFramework; private Node(){ } public void start(String connectString) throws Exception{ if(connectString == null || connectString.isEmpty()){ throw new Exception("connectString is null or empty"); } CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(connectString).sessionTimeoutMs(5000).connectionTimeoutMs(3000) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .build(); String groupNodePath = "/com/dewu/order/operate"; String masterNodePath = groupNodePath + "/master"; String slaveNodePath = groupNodePath + "/slave"; //watch master node PathChildrenCache pathChildrenCache = new PathChildrenCache(client, groupNodePath, true); pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED)){ String childPath = event.getData().getPath(); System.out.println("child removed: " + childPath); //如果是master节点删除了,则自己尝试变成master if(masterNodePath.equals(childPath)){ switchMaster(client, masterNodePath, slaveNodePath); } } else if(event.getType().equals(PathChildrenCacheEvent.Type.CONNECTION_LOST)) { System.out.println("connection lost, become slave"); role = "slave"; } else if(event.getType().equals(PathChildrenCacheEvent.Type.CONNECTION_RECONNECTED)) { System.out.println("connection connected……"); if(!becomeMaster(client, masterNodePath)){ becomeSlave(client, slaveNodePath); } } else{ System.out.println("path changed: " + event.getData().getPath()); } } }); client.start(); pathChildrenCache.start(); } public String getRole(){ return role; } private boolean becomeMaster(CuratorFramework client, String masterNodePath){ //try to become master try { client.create().creatingParentContainersIfNeeded().withMode(CreateMode.EPHEMERAL) .forPath(masterNodePath, Longs.toByteArray(System.currentTimeMillis())); System.out.println("succeeded to become master"); role = "master"; return true; } catch (Exception e) { System.out.println("failed to become master: " + e.getMessage()); return false; } } private boolean becomeSlave(CuratorFramework client, String slaveNodePath) throws Exception { //try to become slave try { client.create().creatingParentContainersIfNeeded().withMode(CreateMode.EPHEMERAL) .forPath(slaveNodePath, Longs.toByteArray(System.currentTimeMillis())); System.out.println("succeeded to become slave"); role = "slave"; return true; } catch (Exception e) { System.out.println("failed to become slave: " + e.getMessage()); throw e; } } private void switchMaster(CuratorFramework client, String masterNodePath, String slaveNodePath){ if(becomeMaster(client, masterNodePath)){ try { client.delete().forPath(slaveNodePath); } catch (Exception e) { System.out.println("failed to delete slave node when switch master: " + slaveNodePath); } } }}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-10 06:16 , Processed in 0.420260 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表