1.官网网站
http://curator.apache.org/
2.curator-创建会话
static CuratorFramework newClient(String connectString, RetryPolicy retryPolicy);
static CuratorFramework newClient(String connectString, int sessionTimeoutMs, int connectionTimeoutMs, RetryPolicy retryPolicy);
解释
1.connectString:英文逗号分隔的ip:port对
2.sessionTimeoutMs:会话超时时间,单位毫秒,默认60000ms
3.connectionTimeoutMs:连接创建超时时间,单位为毫秒,默认15000ms
4.retryPolicy:重试策略
默认四种:
Exponential BackoffRetry
RetryNTimes
RetryOneTime
RetryUntilElapsed
样例
private CuratorFramework client = null;
public CuratorClientTest() {
client=CuratorFrameworkFactory.newClient("localhost:2181,localhost:2182", retryPolicy);
client.start();
}
3.重试策略
3.1.概念描述
1. 所有的重试测试都可以自定义重试策略
2. 都继承 extends SleepingRetry,其中重试策略继承的父类SleepingRetry有一个方法allowRetry
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/685ba2b8f5af1c33e665e3baa5911121.png)
public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper)
参数 | 说明 |
---|
retryCount | 经重试的次数,如果第一次重试,此值为0 |
elapsedTimeMs | 重试花费的时间,单位为毫秒 |
sleeper | 类似于Thread.sleep,用于sleep指定时间 |
返回值 | 如果还会继续重试,则返回true |
3.2.ExponentialBackoffRetry
1.ExponentialBackoffRetry(int baseSleepTimeMs, int maxRetries)
2.ExponentialBackoffRetry(int baseSleepTimeMs, int maxRetries, int maxSleepMs)
3.前应该sleep的时间
baseSleepTimeMs * Math.max(1, random.nextInt(1 << (retryCount + 1)))
参数 | 说明 |
---|
baseSleepTimeMs | 初始sleep时间 |
maxRetries | 最大重试次数 |
maxSleepMs | 最大重试时间 |
返回值 | 如果还会继续重试,则返回true |
3.3.RetryNTimes
RetryNTimes(int n, int sleepMsBetweenRetries)
参数 | 说明 |
---|
n | 最大重试次数 |
sleepMsBetweenRetries | 每次重试的间隔时间 |
3.4.RetryOneTime
只重试一次
RetryOneTime(int sleepMsBetweenRetry), sleepMsBetweenRetry为重试间隔的时间
3.5.RetryUntilElapsed
RetryUntilElapsed(int maxElapsedTimeMs, int sleepMsBetweenRetries)
重试的时间超过最大时间后,就不在重试
参数 | 说明 |
---|
maxElapsedTimeMs 最大重试时间 | |
sleepMsBetweenRetries | 每次重试的间隔时间 |
4.Fluent Interface的API
Fluent Interface 概念可以参考
http://www.raychase.net/263
4.1. 概念
定义:一种面向对象的开发方式,目的是提高代码的可读性
实现方式:通过方法的级联或者方法链的方式实现
4.2.样例
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
client = CuratorFrameworkFactory.builder()
.connectString("localhost:2181,localhost:2182")
.sessionTimeoutMs(10000).retryPolicy(retryPolicy)
.namespace("base").build();
client.start();
5.节点操作
5.1.创建节点
构建操作包装类CreateBuilder:CreateBuilder create()---- CuratorFramework
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/60b23e2d3cad65ce8c66d1e23bc3d145.png)
1.creatingParentsIfNeeded()
2.withMode(CreateMode mode)
创建模式为临时节点,则只有ՙ子节点是临时节点,非ՙ子节点都为持久节点
3.withACL(List aclList)
4.forPath(String path)