curator-client源码阅读笔记
来源:互联网 发布:html5 书籍 知乎 编辑:程序博客网 时间:2024/06/04 19:46
Zookeeper官方client使用起来有很多不便,比如session expire之后需要使用一个新的ZooKeeper对象,提供的接口过于底层等等
Curator是对ZooKeeper的一个封装,其中curator-client是最底层的一个封装,主要是提供自动重连的功能
入口类 CuratorZookeeperClient本身是一个很简单的封装,只保存了retryPolicy和ensembleProvider,真正的连接管理都交给了ConnectionState来处理
ConnectionState
Curator是对ZooKeeper的一个封装,其中curator-client是最底层的一个封装,主要是提供自动重连的功能
入口类 CuratorZookeeperClient本身是一个很简单的封装,只保存了retryPolicy和ensembleProvider,真正的连接管理都交给了ConnectionState来处理
- public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider,int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy,boolean canBeReadOnly)
- {
- retryPolicy = Preconditions.checkNotNull(retryPolicy, "retryPolicy cannot be null");
- ensembleProvider = Preconditions.checkNotNull(ensembleProvider, "ensembleProvider cannot be null");
- this.connectionTimeoutMs = connectionTimeoutMs;
- state = new ConnectionState(zookeeperFactory, ensembleProvider, sessionTimeoutMs, connectionTimeoutMs, watcher, tracer, canBeReadOnly);
- setRetryPolicy(retryPolicy);
- }
public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly) { retryPolicy = Preconditions.checkNotNull(retryPolicy, "retryPolicy cannot be null"); ensembleProvider = Preconditions.checkNotNull(ensembleProvider, "ensembleProvider cannot be null"); this.connectionTimeoutMs = connectionTimeoutMs; state = new ConnectionState(zookeeperFactory, ensembleProvider, sessionTimeoutMs, connectionTimeoutMs, watcher, tracer, canBeReadOnly); setRetryPolicy(retryPolicy); }
ConnectionState
- class ConnectionState implements Watcher, Closeable
- {
- private volatile long connectionStartMs = 0;
- private final Logger log = LoggerFactory.getLogger(getClass());
- //负责管理Zookeeper连接
- private final HandleHolder zooKeeper;
- private final AtomicBoolean isConnected =new AtomicBoolean(false);
- //zookeeper连接地址的provider
- private final EnsembleProvider ensembleProvider;
- private final int connectionTimeoutMs;
- private final AtomicReference<TracerDriver> tracer;
- private final Queue<Exception> backgroundExceptions =new ConcurrentLinkedQueue<Exception>();
- 用户自定义的watcher
- private final Queue<Watcher> parentWatchers =new ConcurrentLinkedQueue<Watcher>();
- ConnectionState(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider,int sessionTimeoutMs, int connectionTimeoutMs, Watcher parentWatcher, AtomicReference<TracerDriver> tracer,boolean canBeReadOnly)
- {
- this.ensembleProvider = ensembleProvider;
- this.connectionTimeoutMs = connectionTimeoutMs;
- this.tracer = tracer;
- if ( parentWatcher != null )
- {
- parentWatchers.offer(parentWatcher);
- }
- //ZooKeeper真正的连接还是由HandleHolder来管理,注意到第二个参数watcher,使用的是this
- zooKeeper = new HandleHolder(zookeeperFactory, this, ensembleProvider, sessionTimeoutMs, canBeReadOnly);
- }
- }
class ConnectionState implements Watcher, Closeable{ private volatile long connectionStartMs = 0; private final Logger log = LoggerFactory.getLogger(getClass()); //负责管理Zookeeper连接 private final HandleHolder zooKeeper; private final AtomicBoolean isConnected = new AtomicBoolean(false); //zookeeper连接地址的provider private final EnsembleProvider ensembleProvider; private final int connectionTimeoutMs; private final AtomicReference<TracerDriver> tracer; private final Queue<Exception> backgroundExceptions = new ConcurrentLinkedQueue<Exception>(); 用户自定义的watcher private final Queue<Watcher> parentWatchers = new ConcurrentLinkedQueue<Watcher>(); ConnectionState(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher parentWatcher, AtomicReference<TracerDriver> tracer, boolean canBeReadOnly) { this.ensembleProvider = ensembleProvider; this.connectionTimeoutMs = connectionTimeoutMs; this.tracer = tracer; if ( parentWatcher != null ) { parentWatchers.offer(parentWatcher); } //ZooKeeper真正的连接还是由HandleHolder来管理,注意到第二个参数watcher,使用的是this zooKeeper = new HandleHolder(zookeeperFactory, this, ensembleProvider, sessionTimeoutMs, canBeReadOnly); }}
- curator-client源码阅读笔记
- curator-client源码阅读笔记
- curator client 2.7.1源码笔记
- Curator-framework源码笔记
- Apache curator-client详解
- Go net/PRC源码阅读client.go
- LZ77源码阅读笔记
- Boa 源码阅读笔记
- VNC源码阅读笔记
- osqa源码阅读笔记
- VNC源码阅读笔记
- MPlayer 源码阅读笔记
- PyEmu源码阅读笔记
- JSONModel源码阅读笔记
- JSONModel源码阅读笔记
- HSF源码阅读笔记
- memcached 源码阅读笔记
- ALLJOYN源码阅读笔记
- PHP开发学习-Apache+PHP+MySQL环境搭建
- WCF实现文件的上传与下载
- 解决WEB集群session同步的方案,Redis内存缓存
- poj - 3468 - A Simple Problem with Integers(线段树(区间更新))
- arm-linux下usb驱动的结构和相关函数
- curator-client源码阅读笔记
- 创建自定义view
- 如何让ECSHOP后台添加完商品后默认不上架 ecshop实现供应商上传产品需要审核
- [算法] 输出 字符串的全部子组合 [dfs - 递归神技]
- 去除win8未激活水印
- c/c++中出现“undefined reference to”的解决
- 项目管理 草案
- 开发环境中的环境变量
- spring MongoDB 集成(分页)