首先项目结构很简单,如图:
接下来是Program.cs内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZooKeeperClient;
using ZooKeeperNet;
namespace ZkTest
{
class Program
{
static void Main(string[] args)
{
string _address = "127.0.0.1:2181";
IZooKeeperFactory _factory=ZooKeeperFactory.Instance;
var zk = _factory.Connect(_address);
if (zk.Exists("/ConnectionTest", true)==null)
{
// 创建一个目录节点,不进行ACL权限控制,节点为永久性的
zk.Create("/ConnectionTest", "testRootData".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
// 创建一个子目录节点
//临时节点:客户端会话失效或连接关闭后,该节点会被自动删除,且不能再临时节点下面创建子节点,否则报如下错:org.apache.zookeeper.KeeperException$NoChildrenForEphemeralsException;
//CreateMode.Ephemeral
// 临时顺序节点:基本特性与临时节点一致,创建节点的过程中,zookeeper会在其名字后自动追加一个单调增长的数字后缀,作为新的节点名
//CreateMode.EphemeralSequential
//持久节点:节点创建后,会一直存在,不会因客户端会话失效而删除
//CreateMode.Persistent
//持久顺序节点:基本特性与持久节点一致,创建节点的过程中,zookeeper会在其名字后自动追加一个单调增长的数字后缀,作为新的节点名
//CreateMode.PersistentSequential
zk.Create("/ConnectionTest/testChildPathOne", "testChildDataOne".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
}
var state = zk.State;
Thread.Sleep(2000);
if (state.Equals(ZooKeeper.States.CONNECTED))
{
string conTestData= Encoding.Default.GetString(zk.GetData("/ConnectionTest", true, null));
Console.WriteLine("取出根目录节点数据:"+conTestData);
// 取出子目录节点列表
zk.GetChildren("/ConnectionTest", true).ToList().ForEach(delegate(String NodeName)
{
Console.WriteLine("子目录节点名称:"+NodeName);
}
);
//取出子目录节点数据,返回byte[]
string childTestData = Encoding.Default.GetString(zk.GetData("/ConnectionTest/testChildPathOne", true, null));
Console.WriteLine("取出子目录节点数据:"+childTestData);
//修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉
zk.SetData("/ConnectionTest/testChildPathOne", "CHILDGeekModify".GetBytes(), -1);
//查询修改的数据
childTestData = Encoding.Default.GetString(zk.GetData("/ConnectionTest/testChildPathOne", true, null));
Console.WriteLine("取出子目录节点修改后数据为:" + childTestData);
//删除/ConnectionTest/testChildPathOne这个节点,第二个参数为版本,-1的话直接删除,无视版本
//zk.Delete("/ConnectionTest/testChildPathOne", -1);
}
zk.Dispose();
Console.WriteLine("---------------------------------------------------");
Console.ReadKey();
}
}
}
接下来看Watcher.cs代码:
namespace ZooKeeperClient
{
internal class Watcher : IWatcher
{
public void Process(WatchedEvent @event)
{
if (@event.Type == EventType.NodeDataChanged)
{
Console.WriteLine(@event.Path + " 此路径节点变化了!");
}
}
}
}
很重要的ZooKeeperFactory.cs和IZooKeeperFactory.cs:
using System;
using ZooKeeperNet;
namespace ZooKeeperClient
{
public interface IZooKeeperFactory
{
ZooKeeper Connect(string address);
ZooKeeper Connect(string address, TimeSpan timeoutSpan);
ZooKeeper Connect(string address, TimeSpan timeoutSpan, IWatcher watcher);
ZooKeeper Connect(string address, TimeSpan timeoutSpan, IWatcher watcher, long sessionId, byte[] password);
}
}
using System;
using ZooKeeperNet;
namespace ZooKeeperClient
{
public sealed class ZooKeeperFactory : IZooKeeperFactory
{
public static IZooKeeperFactory Instance = new ZooKeeperFactory();
private ZooKeeperFactory() { }
//创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法
public ZooKeeper Connect(string address)
{
return Connect(address, new TimeSpan(0, 0, 0, 30), new Watcher());
}
public ZooKeeper Connect(string address, TimeSpan timeoutSpan)
{
return Connect(address, timeoutSpan, new Watcher());
}
public ZooKeeper Connect(string address, TimeSpan timeoutSpan, IWatcher watcher)
{
return new ZooKeeper(address, timeoutSpan, watcher);
}
public ZooKeeper Connect(string address, TimeSpan timeoutSpan, IWatcher watcher, long sessionId, byte[] password)
{
return new ZooKeeper(address, timeoutSpan, watcher, sessionId, password);
}
}
}
最终结果如图: