Zookeeper.NET Client (一)【自己编写客户端 连接实例】

首先zookeeper 压缩包 解压 并配置好!

我本机zookeeper环境配置如下:

D:\Worksoftware\ApacheZookeeper3\conf\zoo.cfg


我已经加了path环境变量,没加的话需要到zookeeper对应bin目录下执行zkServer

然后执行cmd命令:

启动成功后,我们开始C#编程:

首先项目解决方案如图:


接下来看Program.cs代码:

namespace ZKTEST
{
    class Program
    {
        static void Main(string[] args)
        {

            Client client = Zookeeper.CreateClient("zookeeper.local:2181");
            //输出相关服务配置的详细信息。
            string responseConf = client.FourCharacterCommand("conf").Result;
            //列出所有连接到服务器的客户端的完全的连接 / 会话的详细信息。包括“接受 / 发送”的包数量、会话 id 、操作延迟、最后的操作执行等等信息。
            string responseCons = client.FourCharacterCommand("cons").Result;
            //列出未经处理的会话和临时节点。
            string responseDump = client.FourCharacterCommand("dump").Result;
            //输出关于服务环境的详细信息(区别于 conf 命令)。
            string responseEnvi = client.FourCharacterCommand("envi").Result;
            //列出未经处理的请求
            string responseReqs = client.FourCharacterCommand("reqs").Result;
            //测试服务是否处于正确状态。如果确实如此,那么服务返回“imok”,否则不做任何相应。
            string responseRuok = client.FourCharacterCommand("ruok").Result;
            //输出关于性能和连接的客户端的列表。
            string responseStat = client.FourCharacterCommand("stat").Result;
            //列出服务器 watch 的详细信息。
            string responseWchs = client.FourCharacterCommand("wchs").Result;
            //通过 session 列出服务器 watch 的详细信息,它的输出是一个与 watch 相关的会话的列表。
            string responseWchc = client.FourCharacterCommand("wchc").Result;
            //通过路径列出服务器 watch 的详细信息。它输出一个与 session 相关的路径。
            string responseWchp = client.FourCharacterCommand("wchp").Result;

            Console.WriteLine(responseConf);
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine(responseCons);
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine(responseDump);
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine(responseEnvi);
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine(responseReqs);
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine(responseRuok);
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine(responseStat);
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine(responseWchs);
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine(responseWchc);
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine(responseWchp);
            Console.WriteLine("--------------------------------------------------");

            Console.ReadKey();
        }
    }
}

Client.cs代码如下:

namespace ZookeeperClient
{
    public class Client
    {
        readonly string connectionString;

        public Client(string connectionString)
        {
            this.connectionString = connectionString;
        }

        public Task<string> FourCharacterCommand(string cmd)
        {
            //四字命令
            var command = Encoding.ASCII.GetBytes(cmd);
           
            //打开原生客户端
            var tcp = OpenNativeClient(connectionString);
            var stream = tcp.GetStream();
            stream.Write(command, 0, command.Length);

            var data = new byte[1024];
            var reading = Task<int>.Factory.FromAsync(stream.BeginRead, stream.EndRead, data, 0, data.Length, null);

            return reading.ContinueWith(length =>
            {
                tcp.Close();
                if (length.Result < data.Length)
                {
                    Array.Resize(ref data, length.Result);
                }
                return new UTF8Encoding().GetString(data);
            });
        }


        static TcpClient OpenNativeClient(string connectionString)
        {
            var values = connectionString.Split(':');
            var hostname = values[0];
            var portnumber = values[1];
            return new TcpClient(hostname, int.Parse(portnumber));
        }
    }
}
Zookeeper.cs内容如下:

namespace ZookeeperClient
{
    public class Zookeeper
    {
        public static Client CreateClient(string connectionString)
        {
            return new Client(connectionString);
        }
    }
}
运行结果如图:





什么是ZooKeeperZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。 ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。 Rabbit ZooKeeper Extensions 该项目使用了 Apache ZooKeeper .NET async Client 组件,除提供了基本的zk操作,还额外封装了常用的功能以便让.net开发者更好的使用zookeeper。 提供的功能 session过期重连 永久watcher 递归删除节点 递归创建节点 跨平台(支持.net core) 使用说明 创建连接 IZookeeperClient client = new ZookeeperClient(new ZookeeperClientOptions         {             ConnectionString = "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         }); 创建节点 var data = Encoding.UTF8.GetBytes("2016"); //快速创建临时节点 await client.CreateEphemeralAsync("/year", data); await client.CreateEphemeralAsync("/year", data, ZooDefs.Ids.OPEN_ACL_UNSAFE); //快速创建永久节点 await client.CreatePersistentAsync("/year", data); await client.CreatePersistentAsync("/year", data, ZooDefs.Ids.OPEN_ACL_UNSAFE); //完整调用 await client.CreateAsync("/year", data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); //递归创建 await client.CreateRecursiveAsync("/microsoft/netcore/aspnet", data, CreateMode.PERSISTENT); 获取节点数据 IEnumerable data = await client.GetDataAsync("/year"); Encoding.UTF8.GetString(data.ToArray()); 获取子节点 IEnumerable children= await client.GetChildrenAsync("/microsoft"); 判断节点是否存在 bool exists = await client.ExistsAsync("/year"); 删除节点 await client.DeleteAsync("/year"); //递归删除 bool success = await client.DeleteRecursiveAsync("/microsoft"); 更新数据 Stat stat = await client.SetDataAsync("/year", Encoding.UTF8.GetBytes("2017")); 订阅数据变化 await client.Subscrib
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值