首页 > 代码库 > Curator源码解析(一)源码结构和测试程序
Curator源码解析(一)源码结构和测试程序
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源码解析(一)源码结构和测试程序