ZooKeeper客户端Curator使用一 创建连接
如何创建一个ZK连接
工厂方法newClient()
public static CuratorFramework createSimple(String connectionString) {
// these are reasonable arguments for the ExponentialBackOffRetry.
// The first retry will wait 1 second - the second will wait up to 2
// seconds - the third will wait up to 4 seconds.
ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000,3);
// The simplest way to get a CuratorFramework instance. This will use default values.
// The only required arguments are the connection string and the retry policy
return CuratorFrameworkFactory.newClient(connectionString,retryPolicy);
}
首先,对于ZooKeeper的连接就是创建一个CuratorFramework实例的过程.一般会把CuratorFramework实例的创建交给工厂类CuratorFrameworkFactory
,使用工厂方法newClient()方法实例化.
- connectString参数是ZooKeeper服务的地址和端口号,对于集群情况下的多个ZooKeeper示例,之间使用逗号分隔.比如
String connectString = "127.0.0.1:2181,127.0.0.2:2181,127.0.0.3:2181";
- retryPolicy参数是指在连接ZK服务过程中重新连接测策略.在它的实现类
ExponentialBackoffRetry(int baseSleepTimeMs, int maxRetries)
中,baseSleepTimeMs参数代表两次连接的等待时间,maxRetries参数表示最大的尝试连接次数 - CuratorFramework示例创建完成,代表ZooKeeper已经连接成功,调用start()方法打开连接,在使用完毕后调用close()方法关闭连接
- newClient()方法还存在一个重载方法,上面的代码中使用的是
newClient(String connectString, RetryPolicy retryPolicy)
,除该方法外,它还可以指定会话(session)的过期时间以及连接的超时时间.
public static void main(String[] args) {
final String connectString = "127.0.0.1:2181";
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, 3000, 1000, retryPolicy);
client.start();
System.out.println("zk operation");
client.close();
}
Builder()方法
相比于使用newClient()方法创建连接外,还可以使用builder()方法来控制更多的参数,代码如下:
public static CuratorFramework createWithOptions(String connectionString,
RetryPolicy retryPolicy, int connectionTimeoutMs,
int sessionTimeoutMs) {
// using the CuratorFrameworkFactory.builder() gives fine grained control
// over creation options. See the CuratorFrameworkFactory.Builder
// javadoc details
return CuratorFrameworkFactory.builder()
.connectString(connectionString)
.retryPolicy(retryPolicy)
.connectionTimeoutMs(sessionTimeoutMs)
// etc.etc.
.build();
}
最后的代码
package com.netflix.curator.client;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.CloseableUtils;
/**
* @Auther: viagra
* @Date: 2019/11/8 15:39
* @Description:
*/
public class CreateClientExample {
private static final String PATH = "/example/basic";
public static void main(String[] args) throws Exception {
String zkConnString = "127.0.0.1:2181";
CuratorFramework client = null;
try {
client = createSimple(zkConnString);
client.start();
client.create().creatingParentsIfNeeded()
.forPath(PATH,"test".getBytes());
CloseableUtils.closeQuietly(client);
client = createWithOptions(zkConnString,
new ExponentialBackoffRetry(1000, 3), 1000, 1000);
client.start();
System.out.println(new String(client.getData().forPath(PATH)));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
CloseableUtils.closeQuietly(client);
}
}
public static CuratorFramework createSimple(String connectionString) {
// these are reasonable arguments for the ExponentialBackOffRetry.
// The first retry will wait 1 second - the second will wait up to 2
// seconds - the third will wait up to 4 seconds.
ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000,3);
// The simplest way to get a CuratorFramework instance. This will use default values.
// The only required arguments are the connection string and the retry policy
return CuratorFrameworkFactory.newClient(connectionString,retryPolicy);
}
public static CuratorFramework createWithOptions(String connectionString,
RetryPolicy retryPolicy, int connectionTimeoutMs,
int sessionTimeoutMs) {
// using the CuratorFrameworkFactory.builder() gives fine grained control
// over creation options. See the CuratorFrameworkFactory.Builder
// javadoc details
return CuratorFrameworkFactory.builder()
.connectString(connectionString)
.retryPolicy(retryPolicy)
.connectionTimeoutMs(sessionTimeoutMs)
// etc.etc.
.build();
}
}
运行的结果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6Dmzpx2v-1573436159632)(C:\Users\HASEE\AppData\Roaming\Typora\typora-user-images\image-20191108163301392.png)]