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);
        }
    }
}
运行结果如图:





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值