- 浏览: 1207238 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (353)
- java基础 (40)
- tapestry (16)
- gwt (14)
- ajax (7)
- linux (8)
- ubuntu (18)
- eclipse (6)
- dojo (2)
- javascript (7)
- maven (4)
- 日常生活 (29)
- web2.0 (2)
- jsonrpc (1)
- compass (1)
- lucene (0)
- spring (10)
- cluster (3)
- 数据结构 (2)
- sqlserver (1)
- tomcat (2)
- swing (1)
- servlet (1)
- hibernate (1)
- firefox (1)
- Java-Puzzlers (1)
- Wicket (2)
- mysql (10)
- windows (5)
- Webwork (1)
- struts2 (1)
- Seam (2)
- jboss (1)
- idea (6)
- 分布计算 (3)
- Python-Django (7)
- Hadoop (1)
- 工具 (3)
- Tokyo Tyrant (2)
- Comet (1)
- android (115)
- 音乐 (6)
- cxf (1)
- mqtt (1)
最新评论
-
hesai_vip:
学习了,感谢
gradlew wrapper使用下载到本地的gradle.zip文件安装。 -
imknown:
姚瑶大坏蛋 写道我也遇到了这个,用你这个方法,导致下拉刷新不起 ...
解决android-Ultra-Pull-To-Refresh下拉刷新组件中嵌套ViewPager的一个bug -
姚瑶大坏蛋:
我也遇到了这个,用你这个方法,导致下拉刷新不起作用了,你遇到过 ...
解决android-Ultra-Pull-To-Refresh下拉刷新组件中嵌套ViewPager的一个bug -
寐语者:
LeaderElection(String zookeeper ...
使用Zookeeper来为你的程序加上Leader Election的功能。 -
fyc0109:
博主, 安装了一部分还是报这个错误!FAILURE: Buil ...
gradlew wrapper使用下载到本地的gradle.zip文件安装。
ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护、名字服务、分布式同步、组服务等。ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
废话不多说,
附件是完整的maven项目。
Reference: http://hadoop.apache.org/zookeeper/docs/r3.3.1/recipes.html#sc_leaderElection
废话不多说,
package com.ericsson.threef.zookeeper; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.List; import java.util.concurrent.CountDownLatch; /** * Created by IntelliJ IDEA. * User: EDENYIN * Date: 1/4/11 * Time: 3:13 PM * To change this template use File | Settings | File Templates. */ public class LeaderElection implements Watcher, Runnable { private String zookeeperConnectionString; private String rootPath; private ZooKeeper zk; private byte[] hostAddress; public LeaderElection(String zookeeperConnectionString, String rootPath) { this.zookeeperConnectionString = zookeeperConnectionString; this.rootPath = rootPath; try { hostAddress = InetAddress.getLocalHost().getHostAddress().getBytes(); } catch (UnknownHostException e) { e.printStackTrace(); } buildZK(); } private void buildZK() { System.out.println("Build zk client"); try { zk = new ZooKeeper(zookeeperConnectionString, 10000, this); Stat s = zk.exists(rootPath, false); if (s == null) { zk.create(rootPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create(rootPath + "/ELECTION", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } String value = zk.create(rootPath + "/ELECTION/n_", hostAddress, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); } catch (Exception e) { e.printStackTrace(); System.err.println("Error connect to zoo keeper"); } } public void process(WatchedEvent event) { System.out.println(event); if (event.getState() == Event.KeeperState.Disconnected || event.getState() == Event.KeeperState.Expired) { System.out.println("Zookeeper connection timeout."); buildZK(); } } public void run() { while (true) { try { List<String> children = zk.getChildren(rootPath + "/ELECTION", false); String leaderPath = "Not found"; int minValue = -1; for (int i=0;i<children.size();i++) { String child = children.get(i); int index = Integer.parseInt(child.substring(2)); if (i == 0) { minValue = index; leaderPath = child; }else if (index < minValue) { minValue = index; leaderPath = child; } } LatchChildWatcher latchChildWatcher = new LatchChildWatcher(); byte[] data = zk.getData(rootPath + "/ELECTION/" + leaderPath, latchChildWatcher, null); System.out.println("find the leader on the path:" + leaderPath + " whose host address is " + new String(data)); latchChildWatcher.await(); } catch (Exception e) { e.printStackTrace(); System.err.println("Error get the leader." + e.getMessage()); } } } private class LatchChildWatcher implements Watcher { CountDownLatch latch; public LatchChildWatcher(){ latch = new CountDownLatch(1); } public void process(WatchedEvent event){ System.out.println("Watcher fired on path: " + event.getPath() + " state: " + event.getState() + " type " + event.getType()); latch.countDown(); } public void await() throws InterruptedException { latch.await(); } } }
附件是完整的maven项目。
Reference: http://hadoop.apache.org/zookeeper/docs/r3.3.1/recipes.html#sc_leaderElection
- ZooKeeperLeaderEle.zip (33.1 KB)
- 下载次数: 81
评论
2 楼
寐语者
2016-04-06
LeaderElection(String zookeeperConnectionString, String rootPath)这两个参数是什么
1 楼
fish2100
2011-10-21
非常感谢
发表评论
-
gradle项目中怎么集中管理你的依赖库
2015-08-16 11:30 679项目结构 引用root --gradleScript ... -
提高Android Studio的gradle编译速度。
2015-03-12 14:12 4364发现用Android Studio的make和直接使用comm ... -
gradlew wrapper使用下载到本地的gradle.zip文件安装。
2015-03-12 01:26 15353使用gradlew来build项目时,有时候老是需要下载gra ... -
http://java67.blogspot.com/
2014-12-27 21:04 1426http://java67.blogspot.com/ 面试 ... -
Idea color theme
2012-06-28 16:29 1022http://ideacolorschemes.com/ y ... -
javamail遍历的方式拿到邮件的正文和所有附件
2012-06-13 14:34 1619try { ... -
可以用在android的中Des 64位加密解密算法。
2012-03-26 14:54 11149package com.itaoo.utils; imp ... -
Intellij Idea JVM 最优化配置。
2011-11-11 13:40 8024你需要在/Applications/IntelliJ\ IDE ... -
Playing Flash Videos in Android applications
2011-09-01 13:53 780http://www.synesthesia.it/playi ... -
SuperMarioWorld
2011-07-21 15:37 800Super Mario World. -
SuperMarioWorld
2011-07-21 15:37 0Super Mario World. -
xxxxx
2011-07-21 15:01 0xxxxx -
Privacy Policy
2011-05-26 23:43 955This Privacy Policy governs the ... -
macosx java source jar file
2010-11-13 23:57 1048had the same problem today - I ... -
mvn archetype:generate 让你选择archetype创建maven project
2010-04-27 11:02 8273今天发现使用idea自带的创建maven项目的向导不work. ... -
PrettyTime Convert elapsed timestamps, like, “in 3 minutes!”
2010-04-17 19:42 1117Features: Generate human-reada ... -
login your site with facebook connect
2009-12-22 15:00 1214http://wiki.developers.facebook ... -
分页程序中得到page link的算法。
2009-01-16 11:18 1264话不多说。 public ArrayList<In ... -
Dbunit中需要把schema name 大写
2008-03-28 15:11 2427用dbunit做dao的测试。 数据库用的是oracle。 在 ... -
Dbunit在Oracle下需要注意的问题
2007-06-14 14:56 2485公司准备把数据库把MYSQL换成oracle。 系统有用dbu ...
相关推荐
本文详细分析了Zookeeper的源码,特别是Leader选举过程的实现。首先,介绍了阅读源码的...最后,重点分析了Zookeeper中的Leader选举机制,包括其多层队列架构和具体实现流程,帮助读者深入理解这一核心功能的底层逻辑。
ZooKeeper本身可以以Standalone模式安装运行,不过它的长处在于通过分布式ZooKeeper集群(一个Leader,多个Follower),基于一定的策略来保证ZooKeeper集群的稳定性和可用性,从而实现分布式应用的可靠性。...
ZooKeeper权限控制样例程序,配合文档:http://blog.csdn.net/nileader/article/details/43014541
已经包含zookeeper和zookeeperTest JAR包,找个资源真不容易
ZooKeeper是一个高可用的分布式数据管理与系统协调框架。基于对Paxos 算法的实现,使该框架保证了分布式环境中数据的强一致性,也正是基于这样的特性,使得zookeeper...因此,也非常欢迎你分享你在ZK使用上的奇技淫巧。
为.jar的程序,需要JDK1.7以上的版本,只需修改zookeeper的连接地址,即可查询zookeeper节点的信息。 注:本资源是对github上的一个开源项目的一个改造,如有侵权,请告知,会及时的删除资源。原项目地址:...
1. 使用前先Nuget搜索ZooKeeper.Net安装 2. 安装ZooKeeper,百度自行搜索“Windows下安装ZooKeeper” 3. 下载路径:https://mirrors.cnnic.cn/apache/zookeeper/
zookeeper中的leader的主要作用,一个zookeeper 集群 只有一个leader: 类似master/slave模式
Zookeeper是Hadoop的一个子项目,它是分布式系统中的协调系统,可提供的服务主要有:配置服务、名字服务、分布式同步、组服务等。
大部分分布式应用系统需要一个主控、协调器或者控制器来管理物理分布的子进程,目前,大多数都需要开发私有的协调程序,缺乏一个通用机制,协调程序的反复编写浪费,并且难以形成通用、伸缩性好的协调器,zookeeper...
同时,本书深入介绍了分布式一致性问题的工业解决方案——ZooKeeper,并着重向读者展示这一分布式协调框架的使用方法、内部实现及运维技巧,旨在帮助读者全面了解ZooKeeper,并更好地使用和运维ZooKeeper。...
zookeeper 使用指导
ZooKeeper 客户端的使用(二).
c++实现创建,监听,获取指定节点,获取全部子节点,监听子节点变化,监听节点变化等长用zookeeper功能
该示例代码是基于dubbo+zookeeper的微服务实战 订单模块/用户模块/API 使用ecplise开发详细说明见博客https://blog.csdn.net/weixin_39519416/article/details/88123067
dubbo使用zookeeper注册最小依赖jar包,包含dubbo核心包一个,Commons包一个,log4j包一个,netty核心包一个,slf4j包两个,spring核心包一个,zookeeper包两个,javassist包一个。本地搭建可以成功调用服务。欢迎来...
Zookeeper使用场景及详解,优劣之分
D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 ...