标签:
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-examples</artifactId>
<version>2.9.0</version>
</dependency>
client.create().forPath("/head", new byte[0]);
client.delete().inBackground().forPath("/head");
client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/head/child", new byte[0]);
client.getData().watched().inBackground().forPath("/test");
事件类型 事件返回数据
CREATE getResultCode() and getPath()
DELETE getResultCode() and getPath()
EXISTS getResultCode(), getPath() and getStat()
GET_DATA getResultCode(), getPath(), getStat() and getData()
SET_DATA getResultCode(), getPath() and getStat()
CHILDREN getResultCode(), getPath(), getStat(), getChildren()
SYNC getResultCode(), getStat()
GET_ACL getResultCode(), getACLList()
SET_ACL getResultCode()
TRANSACTION getResultCode(), getOpResults()
WATCHED getWatchedEvent()
GET_CONFIG getResultCode(), getData()
RECONFIG getResultCode(), getData()
CuratorFramework client = CuratorFrameworkFactory.builder().namespace("MyApp") ... build();
CuratorTempFramework client = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")// 连接串
.retryPolicy(new RetryNTimes(10, 5000))// 重试策略
.connectionTimeoutMs(100) // 连接超时
.sessionTimeoutMs(100) // 会话超时
.buildTemp(100, TimeUnit.MINUTES); // 临时客户端并设置连接时间
public void close();
public CuratorTransaction inTransaction() throws Exception;
public TempGetDataBuilder getData() throws Exception;
public static void main(String[] args) throws Exception
{
TestingServer server = new TestingServer();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
client.getConnectionStateListenable().addListener(new ConnectionStateListener()
{
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState)
{
System.out.println("连接状态:" + newState.name());
}
});
client.start();
System.out.println(client.getChildren().forPath("/"));
client.create().forPath("/test");
System.out.println(client.getChildren().forPath("/"));
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(server);
System.out.println("OK!");
}
public static void main(String[] args) throws Exception
{
TestingCluster cluster = new TestingCluster(3);
cluster.start();
for (TestingZooKeeperServer server : cluster.getServers())
{
System.out.println(server.getInstanceSpec());
}
cluster.stop();
CloseableUtils.closeQuietly(cluster);
System.out.println("OK!");
}
EnsurePath ensurePath = new EnsurePath(aFullPathToEnsure);
...
String nodePath = aFullPathToEnsure + "/foo";
ensurePath.ensure(zk); // first time syncs and creates if needed
zk.create(nodePath, ...);
...
ensurePath.ensure(zk); // subsequent times are NOPs
zk.create(nodePath, ...);
RetryLoop retryLoop = client.newRetryLoop();
while ( retryLoop.shouldContinue() )
{
try
{
// perform your work
...
// it‘s important to re\-get the ZK instance as there may have been an error and the instance was re\-created
ZooKeeper zk = client.getZookeeper();
retryLoop.markComplete();
}
catch ( Exception e )
{
retryLoop.takeException(e);
}
}
RetryLoop.callWithRetry(client, new Callable<Void>()
{
@Override
public Void call() throws Exception
{
// do your work here - it will get retried if needed
return null;
}
});
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4926385.html