The project uses the Apache ZooKeeper .NET async Client component, in addition to providing the basic zk operation, but also additional encapsulation of the commonly used functions in order to allow. Net developers to better use zookeeper.
- session expired
- Permanent watcher
- Recursively delete nodes
- Recursively create nodes
- Cross-platform (support. Net core)
IZookeeperClient client = new ZookeeperClient(new ZookeeperClientOptions("172.18.20.132:2181")
{
BasePath = "/", //default value
ConnectionTimeout = TimeSpan.FromSeconds(10), //default value
SessionTimeout = TimeSpan.FromSeconds(20), //default value
OperatingTimeout = TimeSpan.FromSeconds(60), //default value
ReadOnly = false, //default value
SessionId = 0, //default value
SessionPasswd = null //default value
EnableEphemeralNodeRestore = true //default value
});
var data = Encoding.UTF8.GetBytes("2016");
//Fast create temporary nodes
await client.CreateEphemeralAsync("/year", data);
await client.CreateEphemeralAsync("/year", data, ZooDefs.Ids.OPEN_ACL_UNSAFE);
//Fast create permanent nodes
await client.CreatePersistentAsync("/year", data);
await client.CreatePersistentAsync("/year", data, ZooDefs.Ids.OPEN_ACL_UNSAFE);
//Full call
await client.CreateAsync("/year", data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
//Recursively created
await client.CreateRecursiveAsync("/microsoft/netcore/aspnet", data);
IEnumerable<byte> data = await client.GetDataAsync("/year");
Encoding.UTF8.GetString(data.ToArray());
IEnumerable<string> children= await client.GetChildrenAsync("/microsoft");
bool exists = await client.ExistsAsync("/year");
await client.DeleteAsync("/year");
//Recursively deleted
bool success = await client.DeleteRecursiveAsync("/microsoft");
Stat stat = await client.SetDataAsync("/year", Encoding.UTF8.GetBytes("2017"));
await client.SubscribeDataChange("/year", (ct, args) =>
{
IEnumerable<byte> currentData = args.CurrentData;
string path = args.Path;
Watcher.Event.EventType eventType = args.Type;
return Task.CompletedTask;
});
await client.SubscribeChildrenChange("/microsoft", (ct, args) =>
{
IEnumerable<string> currentChildrens = args.CurrentChildrens;
string path = args.Path;
Watcher.Event.EventType eventType = args.Type;
return Task.CompletedTask;
});
The event subscribed by the "SubscribeDataChange" method is triggered in the following cases:
- The node is created
- The node is deleted
- Node data changes
- zk connection re-successful
The event subscribed by the "SubscribeChildrenChange" method is triggered in the following cases:
- The node is created
- The node is deleted
- Node node changes
- zk connection re-successful
In the event trigger parameter will have a type "EventType" attribute "Type", through this attribute can clearly distinguish the reasons for node changes.
Officially provided components, only provide the basic api, in the normal use of the zk needs to do very complicated things, breed a lot of additional code and can not guarantee the correctness of its implementation.
In the java language also has the official zk package package ZKClient, the current component is also a reference to this project. What are the specific packages that are available? Please refer to the section "Features Provided".