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

时间:2022-02-20 21:17:40

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

我本机zookeeper环境配置如下:

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

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

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

然后执行cmd命令:

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

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

首先项目解决方案如图:

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

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

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