Curator源码解析(一)源码结构和测试程序
来源:互联网 发布:unity3d 开源游戏 编辑:程序博客网 时间:2024/05/22 04:41
Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情, 于是在它的基础上包装了一下, 提供了一套更好用的客户端框架. Netflix在用ZooKeeper的过程中遇到的问题, 我们也遇到了, 所以开始研究一下, 首先从他在github上的源码, wiki文档以及Netflix的技术blog入手.
看完官方的文档之后, 发现Curator主要解决了三类问题:
- 封装ZooKeeper client与ZooKeeper server之间的连接处理;
- 提供了一套Fluent风格的操作API;
- 提供ZooKeeper各种应用场景(recipe, 比如共享锁服务, 集群领导选举机制)的抽象封装.
先大概讲一下代码结构和主要的类,Curator包含了以下文件夹(组件):
Recipes Implementations of some of the common ZooKeeper"recipes". The implementations are built on top of the CuratorFramework.
构建在Curator Framework框架之上,实现了原生ZooKeeper中的分布式工具特征recipes。
Framework The Curator Framework is a high-level API thatgreatly simplifies using ZooKeeper. It adds many features that build onZooKeeper and handles the complexity of managing connections to the ZooKeepercluster and retrying operations.
提供高层抽象API简化ZooKeeper的使用,更重要的是封装了管理到ZooKeeper集群的连接以及重试机制的复杂性,是Curator对外提供的接口。
Utilities Various utilities that are useful when usingZooKeeper.
一些使用ZooKeeper过程中有用的工具。
Client A replacement for the bundled ZooKeeper class that takes care ofsome low-level housekeeping and provides some useful utilities.
替换原生ZooKeeper类,封装操作原生ZooKeeper的底层细节,主要包括前面说的实现了复杂的到ZooKeeper集群的连接以及重试机制。
Errors How Curator deals with errors, connection issues, recoverableexceptions, etc.
Curator如何处理错误,连接问题以及可恢复异常。
Extensions The curator-recipes package implements thecommon recipes that are described in the ZooKeeper documentation. To avoidbloating that package, recipes/applications that have a vertical appeal will beput in separate "extension" packages using the naming convention curator-x-name.
Curator基于ZooKeeper实现的拓展功能,包括直接操作ZooKeeper的服务的动态发现,封装后以Restful接口提供出来的服务动态发现,以及RPC调用等功能,具体以后的文章会讲到。
一个典型的Curator的例子就是:
/** * An example of the PathChildrenCache. The example "harness" is a command processor * that allows adding/updating/removed nodes in a path. A PathChildrenCache keeps a * cache of these changes and outputs when updates occurs. */public class PathCacheExample{ private static final String PATH = "/example/cache"; public static void main(String[] args) throws Exception { TestingServer server = new TestingServer(); CuratorFramework client = null; PathChildrenCache cache = null; try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); // in this example we will cache data. Notice that this is optional. cache = new PathChildrenCache(client, PATH, true); cache.start(); setValue(client, command, args); } finally { CloseableUtils.closeQuietly(cache); CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(server); }}private static void setValue(CuratorFramework client, String command, String[] args) throws Exception { if ( args.length != 2 ) { System.err.println("syntax error (expected set <path> <value>): " + command); return; } String name = args[0]; if ( name.contains("/") ) { System.err.println("Invalid node name" + name); return; } String path = ZKPaths.makePath(PATH, name); byte[] bytes = args[1].getBytes(); try { client.setData().forPath(path, bytes); } catch ( KeeperException.NoNodeException e ) { client.create().creatingParentsIfNeeded().forPath(path, bytes); } }
下一篇文章开始将会通过这个例子详细分析Curator源码。
- Curator源码解析(一)源码结构和测试程序
- Curator源码解析(二)初始化和启动分析
- Curator源码解析(五)连接和重试机制分析
- curator源码(一) 初始化、启动和关闭。
- Curator源码解析(三)访问接口分析
- underScore 源码解析(一)---- 结构
- Curator-framework源码笔记
- zookeeper curator 源码试读
- WebViewJavascriptBridge工程结构和源码解析
- geth结构解析和源码分析
- Curator源码解析(四)ZooKeeper存在的连接问题
- android源码结构解析
- STM32 IAP程序 源码 和测试代码
- WebBench测试源码解析
- 王学岗ListView和源码解析(一)
- curator-client源码阅读笔记
- curator-client源码阅读笔记
- Curator连接状态源码浅析
- game猜数字游戏
- 第三方
- 1022. Digital Library (30)
- Mac 的ParallelsDesktop PD安装操作
- Cocos Studio 2.0Beta0创建的cocos2d-x-3.3rc0项目解析方法
- Curator源码解析(一)源码结构和测试程序
- win8.1下在VMware Workstation安装ubuntu14.10
- c链表 学生成绩管理系统
- Tomcat Xms Xmx PermSize MaxPermSize 区别 及 java.lang.OutOfMemoryError: PermGen space 解决
- An SqlServer Store Procedure DataAccess Layer Class
- 迷茫、未知、奋斗、无力、重燃激情,我该如何形容这一年
- Netty代码分析
- [1]ARM-Linux S5PV210 UART驱动
- Canvas and Drawables 翻译第一集