curator实现zookeeper的领导选举
来源:互联网 发布:仿生 知乎 编辑:程序博客网 时间:2024/06/07 07:15
通过curator来实现zookeeper的领导选举,具体看代码
public class ExampleClient extends LeaderSelectorListenerAdapter implements Closeable{
private String name;private LeaderSelector leaderSelector;private AtomicInteger leaderCount = new AtomicInteger();public ExampleClient(CuratorFramework client,String path,String name){ this.name=name; leaderSelector=new LeaderSelector(client, path, this); leaderSelector.autoRequeue();}public void takeLeadership(CuratorFramework client) throws Exception{ // we are now the leader. This method should not return until we want to relinquish leadership final int waitSeconds = (int)(5 * Math.random()) + 1; System.out.println(name + " is now the leader. Waiting " + waitSeconds + " seconds..."); System.out.println(name + " has been leader " + leaderCount.getAndIncrement() + " time(s) before."); try { Thread.sleep(TimeUnit.SECONDS.toMillis(waitSeconds)); } catch ( InterruptedException e ) { System.err.println(name + " was interrupted."); Thread.currentThread().interrupt(); } finally { System.out.println(name + " relinquishing leadership.\n"); }}public void start() throws IOException{ leaderSelector.start();}public void close() throws IOException{ leaderSelector.close();}
}
程序入口
public class LeaderSelectorExample {
private static String PATH="/example/cache";private final static String zkStr="master:2181,worker1:2181,worker2:2181";private static List<CuratorFramework> clients= Lists.newArrayList();private static List<ExampleClient> examples= Lists.newArrayList();public static void main(String []args)throws Exception{ try { for ( int i = 0; i < 100; ++i ) { CuratorFramework client = CuratorFrameworkFactory.newClient(zkStr, new ExponentialBackoffRetry(1000, 3)); clients.add(client); ExampleClient example = new ExampleClient(client, PATH, "Client #" + i); examples.add(example); client.start(); example.start(); } System.out.println("Press enter/return to quit\n"); //new BufferedReader(new InputStreamReader(System.in)).readLine(); } finally { System.out.println("Shutting down..."); for ( ExampleClient exampleClient : examples ) { CloseableUtils.closeQuietly(exampleClient); } for ( CuratorFramework client : clients ) { CloseableUtils.closeQuietly(client); } }
}
}
0 0
- curator实现zookeeper的领导选举
- curator实现master选举
- zookeeper之Curator框架(CRUD/事务/选举/锁)的使用
- curator选举实现之一,二
- Zookeeper实例Curator API-Master选举
- zookeeper——leader选举(curator)
- Zookeeper系列(十四)Zookeeper开源客户端之Curator的Master/Leader选举
- curator实现zookeeper的分布式锁
- 关于Zookeeper框架Curator中的主从选举框架LeaderLatch的坑
- 用zookeeper实现简单的master选举
- java实现zookeeper的领袖选举
- Zookeeper 实现 master 选举
- Zookeeper 实现 master 选举
- zookeeper实现主从选举
- Zookeeper开源客户端Curator之Master/Leader选举
- Zookeeper的选举
- Zookeeper的Leader选举
- Zookeeper的Leader选举
- boost-库编译和使用-001
- cmake安装、用法
- oracle system表空间的管理
- android模拟器安装教程
- AOJ 862 平面上最近点对
- curator实现zookeeper的领导选举
- 爬取Aliexpress网站的商品数据,保存至excel表格
- Integer类中reverse方法探究
- STL中set用法详解
- int *p=new int; int *p=new int[10]; int *p=new int(10);这三个有什么区别
- Spring boot 多表关联查询
- RecyclerView 超简单item点击实现
- 京东一面
- 高效程序员的七个习惯