English|中文
A zookeeper client library based on ZookeeperEx,easily use for Zookeeper.
Transaction operation is not supported at present, and will be supported in the next version
1.Connect to Zookeeper
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Microsoft", LogLevel.Debug)
.AddFilter("System", LogLevel.Information)
.AddConsole();
});
var logger = loggerFactory.CreateLogger<ZkConnection>();
//Multiple zookeeper addresses are separated by ,
var zk = new ZkConnection(new ZkConnectionOptions() { ConnectionString = "localhost:2181", SessionTimeout = 5000 }, logger);
await zk.ConnectAsync();
2.Set watch event
It is used to monitor node changes and data changes. Node changes include: node creation and node deletion
zk.OnWatch += Zk_OnWatch;
private Task Zk_OnWatch(ZkWatchEventArgs args)
{
Console.WriteLine($"OnWatch: Path {args.Path}, Type {args.EventType}, State {args.State}");
return Task.CompletedTask;
}
3.Node operation
//Create node
await zk.CreateNodeAsync("/mynode", "nodedata",
new List<Acl>() { new Acl(AclPerm.All, AclScheme.World, AclId.World()) }, NodeType.Ephemeral);
//Get child node
await zk.GetChildrenAsync("/mynode");
//Delete node
await zk.DeleteNodeAsync("/mynode");
//Check if the node exists
await zk.NodeExistsAsync("/mynode")
4.Data
//Update node data
await zk.SetDataAsync("/mynode", "111");
//Get node data
await zk.GetDataAsync("/mynode")
5.ACL
//Get ACL
await zk.GetAclAsync("/mynode");
//Set ACL
await zk.SetAclAsync("/mynode",new List<Acl>() { new Acl(AclPerm.All, AclScheme.World, AclId.World()) })