使用curator进行选举
2017-07-11 16:02
239 查看
前言
1.笔者在开发项目的过程中,碰到一种情况。只能进行一台机器的处理。类型这样的分布式问题,我们可以通过zookeeper实现选举master的功能。
2.大体思路。
选择一个根据节点,例如/master_select,多台机器同时向该节点创建一个子节点/master_select/lock,利用zookeeper的特性,最终只有一台机器能够创建成功,成功的那台就作为Master选举。
Curator也是基于这个思路,但是它将节点创建,事件监听和自动选举进行了封装,开发人员只需要简单的调用API即可实现Master选举。下面我我们通过一个实例开看看如何使用Curator实现Master选举功能。
3.实例代码
1.笔者在开发项目的过程中,碰到一种情况。只能进行一台机器的处理。类型这样的分布式问题,我们可以通过zookeeper实现选举master的功能。
2.大体思路。
选择一个根据节点,例如/master_select,多台机器同时向该节点创建一个子节点/master_select/lock,利用zookeeper的特性,最终只有一台机器能够创建成功,成功的那台就作为Master选举。
Curator也是基于这个思路,但是它将节点创建,事件监听和自动选举进行了封装,开发人员只需要简单的调用API即可实现Master选举。下面我我们通过一个实例开看看如何使用Curator实现Master选举功能。
3.实例代码
org.apache.curatorcurator-recipes2.12.0
package cn.datek.test.zookeeper; import java.util.List; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.leader.LeaderSelector; import org.apache.curator.framework.recipes.leader.LeaderSelectorListener; import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter; import org.apache.curator.framework.state.ConnectionState; import org.apache.curator.retry.ExponentialBackoffRetry; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:beans/spring-context.xml" }) public class RecipesMasterSelect { //dubbo.registry.address private String zkurl; private String master_path="/curator_recipes_master_path"; @Test public void master() throws Exception{ CuratorFramework client = CuratorFrameworkFactory.builder().connectString("10.0.250.222:2181").retryPolicy(new ExponentialBackoffRetry(1000, 3)).build(); client.start(); /** * 该实例封装了所有Master选举相关的逻辑,包括所有和Zookeeper服务器交互的过程,其中Master_select代表一个Master选举的一个 * 根节点,表明本次Master选举都是在该节点下进行的。 * 在创建LeaderSelector实例的时候,还会传入一个监听器:LeaderSelectorListenerAdapter。这需要开发人员自行实现。Curator * 会在成功获取Master权利时候回调该监听器。 */ LeaderSelector leaderSelector = new LeaderSelector(client, master_path, new LeaderSelectorListener() { @Override public void takeLeadership(CuratorFramework client) throws Exception { // TODO Auto-generated method stub System.out.println("成为Master角色"); Thread.sleep(3000); List path = client.getChildren().forPath(master_path); System.out.println(path); System.out.println("完成Master操作,释放Master权利"); } @Override public void stateChanged(CuratorFramework arg0, ConnectionState arg1) { // TODO Auto-generated method stub } }); leaderSelector.autoRequeue(); leaderSelector.start(); Thread.sleep(Integer.MAX_VALUE); } }
相关文章推荐
- Zookeeper入门之使用curator连接zookeeper并且进行节点的增删改查及ACL
- Zookeeper入门之使用curator连接zookeeper并且进行节点的增删改查及ACL
- Zookeeper系列三:Zookeeper客户端的使用(Zookeeper原生API如何进行调用、ZKClient、Curator)
- Curator典型使用场景之Master选举。
- Zookeeper入门之使用curator连接zookeeper并且进行节点的增删改查及ACL
- 「吐槽」使用VS2012 代替SQL Server Manager 进行简单操作
- 一步一步学Silverlight 2系列(27):使用Brush进行填充
- 详解Android使用Socket对大文件进行加密传输
- iOS开发使用Unwind Segue进行返回
- 生产者消费者模型(使用lock.condition进行分组唤醒)
- java使用UDP来进行客户端和服务器端通信的简单例子
- android使用ShareSDK 快速进行分享
- 【机器学习实战】第11章 使用 Apriori 算法进行关联分析
- 64位win7 使用tensorflow的classify_image进行物体识别
- 使用高德地图API进行定位
- 如何使用installshield进行程序打包发布
- Beetle使用FluorineFx和Flash进行AMF3通讯
- 不允许使用“{...}”对”std::vector<int,std::allocator<int> >“类型的对象进行初始化
- 【ORACLE】生产环境使用数据泵impd,expdp进行数据迁移
- 使用Python进行简单的验证码识别