CuratorFramework
(Documentation: http://curator.apache.org/curator-framework/index.html )
What is Framework?
- What is Curator?
Curator Framework is a high-level API of zookeeper;
- What can Curators do?
》Automatic link management, such as automatic reconnection, etc.;
》Simple API;
》Implementation of special functions, etc.; such as leader election, shared lock, etc.;
Two ways of Curator instance creation
A: Factory method
CuratorFrameworkFactory.newClient();
B: Use builder
CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(hosts) .sessionTimeoutMs(sessionTimeout) .connectionTimeoutMs(connectionTimeout) .canBeReadOnly(true) .retryPolicy(new ExponentialBackoffRetry(1000, 100)) .defaultData(null) .build();
After creating an instance, first execute the start() method;
//initialize the instance client.start(); //close the instance client.close();
PS: Curator instances are thread-safe, so try to reuse the same instance;
CuratorFramework API
Curator uses a fluent interface style.
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");
Method:
http://curator.apache.org/curator-framework/index.html)
Namespaces
To prevent conflicts, the same path may be added by adding the namespace
CuratorFramework client = CuratorFrameworkFactory.builder().namespace("MyApp") ... build(); ... client.create().forPath("/test", data); // node was actually written to: "/MyApp/test"