|
开源客户端,原生api的不足
连接的创建是异步的,需要开发人员自行编码实现等待
连接没有自动的超时重连机制
Zk本身没提供序列化机制,需要开发人员自行指定,从而实现数据的序列化和反序列化
Watcher注册一次只会生效一次,需要不断的重复注册
Watcher的使用方式不符合java本身的术语,如果采用监听器方式,更容易理解
不支持递归创建树形节点
开源客户端---ZkClient介绍
Github上一个开源的zk客户端,由datameer的工程师Stefan Groschupf和Peter Voss一起开发
– 解决session会话超时重连
– Watcher反复注册
– 简化开发api
– 还有.....
– https://github.com/sgroschupf/zkclient
开源客户端---Curator介绍
1. 使用CuratorFrameworkFactory工厂的两个静态方法创建客户端
a) static CuratorFramework newClient(String connectString, int sessionTimeoutMs, int connectionTimeoutMs,
RetryPolicy retryPolicy)
b) static CuratorFramework newClient(String connectString, RetryPolicy retryPolicy)
2. Start()方法启动
参数说明
connectString 分开的ip:port对
retryPolicy 重试策略,默认四种:Exponential BackoffRetry,RetryNTimes ,RetryOneTime,
RetryUntilElapsed
sessionTimeoutMs 会话超时时间,单位为毫秒,默认60000ms
connectionTimeoutMs 连接创建超时时间,单位为毫秒,默认是15000ms
重试策略
– 实现接口RetryPolicy可以自定重重试策略
boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper)
retryCount 已经重试的次数,如果第一次重试 此值为0
elapsedTimeMs 重试花费的时间,单位为毫秒
sleeper 类似于Thread.sleep,用于sleep指定时间
返回值 如果还会继续重试,则返回true
四种默认重试策略
– ExponentialBackoffRetry
ExponentialBackoffRetry(int baseSleepTimeMs, int maxRetries)
ExponentialBackoffRetry(int baseSleepTimeMs, int maxRetries, int maxSleepMs)
当前应该sleep的时间: baseSleepTimeMs * Math.max(1, random.nextInt(1 |
|
|