Zookeeper基础教程(四):C#连接使用Zookeeper
Zookeeper作为分布式的服务框架,虽然是java写的,但是强大的C#也可以连接使用。
C#要连接使用Zookeeper,需要借助第三方插件,而现在主要有两个插件可供使用,分别是ZooKeeperNetEx和Zookeeper.Net
Zookeeper.Net好像是是Apache官方提供的,但是5年没更新了,也就是说他依赖于.net framework,因此无法在.net core项目中使用
ZooKeeperNetEx是从java改过来的,因此里面的一些习惯是java风格的,但是好像有人在提供更新维护,支持最新的Zookeeper特性,而且摆脱了对.net framework的依赖,所以个人推荐使用ZooKeeperNetEx做开发,本文也已介绍ZooKeeperNetEx为主
新建一个控制台项目,在nuget中搜索ZooKeeperNetEx,并安装最新版
在Program的Main方法:
using org.apache.zookeeper;
using org.apache.zookeeper.data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace AspNetCore.ZookeeperConsole
{
class Program
{
static void Main(string[] args)
{
//Zookeeper连接字符串,采用host:port格式,多个地址之间使用逗号(,)隔开
string connectionString = "192.168.209.133:2181,192.168.209.133:2181,192.168.209.133:2181";
//会话超时时间,单位毫秒
int sessionTimeOut = 10000;
//异步监听
var watcher = new MyWatcher("ConnectWatcher");
//连接
ZooKeeper zooKeeper = new ZooKeeper(connectionString, sessionTimeOut, watcher);
Thread.Sleep(1000);//停一秒,等待连接完成
while (zooKeeper.getState() == ZooKeeper.States.CONNECTING)
{
Console.WriteLine("等待连接完成...");
Thread.Sleep(1000);
} var state = zooKeeper.getState();
if (state != ZooKeeper.States.CONNECTED && state != ZooKeeper.States.CONNECTEDREADONLY)
{
Console.WriteLine("连接失败:" + state);
Console.ReadKey();
return;
} //创建znode节点
{
var data = Encoding.UTF8.GetBytes("hello world");
List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;//创建节点时的acl权限,也可以使用下面的自定义权限
//List<ACL> acl = new List<ACL>() {
// new ACL((int)ZooDefs.Perms.READ, new Id("ip", "127.0.0.1")),
// new ACL((int)(ZooDefs.Perms.READ | ZooDefs.Perms.WRITE), new Id("auth", "id:pass"))
//};
CreateMode createMode = CreateMode.PERSISTENT;
zooKeeper.createAsync("/mynode", data, acl, createMode).Wait();
Console.WriteLine("完成创建节点");
} //节点是否存在
{
var exists = zooKeeper.existsAsync("/mynode", new MyWatcher("ExistsWatcher")).GetAwaiter().GetResult();
Console.WriteLine("节点是否存在:" + exists);
} //获取节点数据
{
var dataResult = zooKeeper.getDataAsync("/mynode", new MyWatcher("GetWatcher")).GetAwaiter().GetResult();
var value = Encoding.UTF8.GetString(dataResult.Data);
Console.WriteLine("完成读取节点:" + value);
} //设置节点数据
{
var data = Encoding.UTF8.GetBytes("hello world again");
zooKeeper.setDataAsync("/mynode", data);
Console.WriteLine("设置节点数据");
} //重新获取节点数据
{
var dataResult = zooKeeper.getDataAsync("/mynode", new MyWatcher("GetWatcher")).GetAwaiter().GetResult();
var value = Encoding.UTF8.GetString(dataResult.Data);
Console.WriteLine("重新获取节点数据:" + value);
} //移除节点
{
zooKeeper.deleteAsync("/mynode").Wait();
Console.WriteLine("移除节点");
} Console.WriteLine("完成");
Console.ReadKey();
}
} class MyWatcher : Watcher
{
public string Name { get; private set; } public MyWatcher(string name)
{
this.Name = name;
} public override Task process(WatchedEvent @event)
{
var path = @event.getPath();
var state = @event.getState(); Console.WriteLine($"{Name} recieve: Path-{path} State-{@event.getState()} Type-{@event.get_Type()}");
return Task.FromResult(0);
}
}
}
运行后显示结果:
这个简单的例子是使用ZooKeeperNetEx操作的简单例子,下面具体介绍
ZooKeeperNetEx连接Zookeeper只需要实例化ZooKeeper对象即可
//Zookeeper连接字符串,采用host:port格式,多个地址之间使用逗号(,)隔开
string connectionString = "192.168.209.133:2181,192.168.209.133:2181,192.168.209.133:2181";
//会话超时时间,单位毫秒
int sessionTimeOut = 10000;
//异步监听
var watcher = new MyWatcher("ConnectWatcher");
//连接
ZooKeeper zooKeeper = new ZooKeeper(connectionString, sessionTimeOut, watcher);
实例化过程中至少需要三个参数
连接字符串(connectstring):host:port形式,多个地址之间使用英文逗号隔开
会话超时时间(sessionTimeout):当会话中,Zookeeper超过此时间未响应,则表示会话超时
监听器(watcher):这个连接过程中可以注册一个监听器,当连接过程中出现状态改变时,会通知到监听器
ZooKeeper对象实例化过程中会异步的去连接Zookeeper,所以例子中才有一个while循环来判断状态
Thread.Sleep(1000);//停一秒,等待连接完成
while (zooKeeper.getState() == ZooKeeper.States.CONNECTING)
{
Console.WriteLine("等待连接完成...");
Thread.Sleep(1000);
}
而Zookeeper的连接状态有6种:
//ZooKeeper.States的枚举
CONNECTING = 0, //连接中
CONNECTED = 1, //已连接
CONNECTEDREADONLY = 2, //已连接,但是只能只读访问
CLOSED = 3, //已关闭连接
AUTH_FAILED = 4, //认证失败
NOT_CONNECTED = 5 //未连接
当应用连接到Zookeeper时,一般都是读取数据,所以主需要只读连接就可以满足的,不过具体还是要看需求。
当在指定的会话时间内未成功连接时,则会导致连接超时,因为这个过程是异步的,所以需要一个监听器来接收。
监听器其实是org.apache.zookeeper.Watcher的一个子类,这个需要开发者去继承实现它的process方法,比如上面的例子中我们就简单的实现
class MyWatcher : Watcher
{
public string Name { get; private set; } public MyWatcher(string name)
{
this.Name = name;
} public override Task process(WatchedEvent @event)
{
var path = @event.getPath();
var state = @event.getState(); Console.WriteLine($"{Name} recieve: Path-{path} State-{@event.getState()} Type-{@event.get_Type()}");
return Task.FromResult(0);
}
}
这里仅仅只是简单的输出节点路径、监听事件响应状态和监听事件类型
//监听事件响应状态,Watcher.Event.KeeperState的枚举
Expired = -112, //连接超时
Disconnected = 0, //连接断开
SyncConnected = 3, //已同步连接
AuthFailed = 4, //认证失败
ConnectedReadOnly = 5 //只读连接 //监听事件类型,Watcher.Event.EventType的枚举 None = -1, //非节点操作事件
NodeCreated = 1, //创建节点事件
NodeDeleted = 2, //删除节点事件
NodeDataChanged = 3, //节点数据改变
NodeChildrenChanged = 4 //子节点发生改变
为什么要有监听器?监听器就类似一个回调,当发生某个事件时,我们的应用可能需要进行相应的处理,如当连接断开时,由于监听器的存在,我们可以让我们的应用程序重新与Zookeeper建立连接。
ZooKeeperNetEx创建znode节点使用的是createAsync异步方法,传入4个参数,分别是
节点路径(path)::创建的节点路径
节点数据(data):节点数据,它是一个字节数组,可以通过编码将字符串转化为字符数组
ACL权限(acl):ACL权限,可以使用已定义好的,也可以使用自定义,如:
//已经定义好的,ZooDefs.Ids的枚举
OPEN_ACL_UNSAFE:完全开放
CREATOR_ALL_ACL:创建该znode的连接拥有所有权限
READ_ACL_UNSAFE:所有的客户端都可读
自定义方式如:
List<ACL> acl = new List<ACL>() {
new ACL((int)ZooDefs.Perms.READ, new Id("ip", "127.0.0.1")),
new ACL((int)(ZooDefs.Perms.READ | ZooDefs.Perms.WRITE), new Id("auth", "id:pass"))
};
节点类型(createMode):节点类型有4种,分别是CreateMode类的4个静态字段
PERSISTENT:持久化节点
PERSISTENT_SEQUENTIAL:持久化有序节点
EPHEMERAL:临时节点(连接断开自动删除)
EPHEMERAL_SEQUENTIAL:临时有序节点(连接断开自动删除)
createAsync异步方法会返回实际创建的znode路径,貌似没什么用(在创建顺序节点时会用到,当一个新的znode被创建为一个顺序节点时,ZooKeeper通过将10位的序列号附加到原始名称来设置znode的路径)
上面这个是ZooKeeperNetEx创建znode节点的方法,而对znode的其他操作的参数就很简单了,这里就不在重述,需要具体操作才能理解,一个简单的介绍如下:
//删除znode节点
public Task deleteAsync(string path, int version = -1);
//指定的znode节点是否存在
public Task<Stat> existsAsync(string path, Watcher watcher);
public Task<Stat> existsAsync(string path, bool watch = false);
//获取znode节点数据
public Task<DataResult> getDataAsync(string path, bool watch = false);
public Task<DataResult> getDataAsync(string path, Watcher watcher);
//设置指定znode节点的数据
public Task<Stat> setDataAsync(string path, byte[] data, int version = -1);
//获取指定znode节点的子节点,注意,监听器是注册给当前节点的,而非子节点
public Task<ChildrenResult> getChildrenAsync(string path, Watcher watcher);
public Task<ChildrenResult> getChildrenAsync(string path, bool watch = false);
可以比较一下上一节介绍的zkCli对znode节点的操作就很容易理解了。
另外,需要注意的是,existsAsync方法、getDataAsync方法和getChildrenAsync方法可以在指定的znode注册一个监听器,setDataAsync方法却没有这个注册功能,这个是因为Zookeeper注册的监听器只会响应一次,当需要再次响应时,需要重新注册,这时就可以调用existsAsync方法或者getDataAsync方法或者getChildrenAsync方法进行重新注册了!
上一节说到ACL权限不仅可以在创建是给予,在创建后也可以修改,ZookeeperNetEx操作znode的ACL权限使用的方法如下:
//获取ACL权限
public Task<ACLResult> getACLAsync(string path);
//设置ACL权限
public Task<Stat> setACLAsync(string path, List<ACL> acl, int aclVersion = -1);
说到ACL,自然就会认证存在,ZookeeperNetEx添加认证使用的是addAuthInfo方法
public void addAuthInfo(string scheme, byte[] auth);
其中scheme就是我们上一节介绍的那几种:
world:默认模式,所有客户端都拥有指定的权限。world下只有一个id选项,就是anyone,通常组合写法为world:anyone:[permissons];
auth:只有经过认证的用户才拥有指定的权限。通常组合写法为auth:user:password:[permissons],使用这种模式时,你需要先进行登录,之后采用auth模式设置权限时,user和password都将使用登录的用户名和密码;比如:
digest:只有经过认证的用户才拥有指定的权限。通常组合写法为digest:user:BASE64(SHA1(password)):[permissons],这种形式下的密码必须通过SHA1和BASE64进行双重加密;
ip:限制只有特定IP的客户端才拥有指定的权限。通常组成写法为ip:182.168.0.168:[permissions];
super:代表超级管理员,拥有所有的权限,需要修改Zookeeper启动脚本进行配置。
auth是认证数据,如果没有则可以是空的字节数组,如:
//world模式认证
zk.addAuthInfo("world",new byte[0]);
//auth模式认证
byte[] auth=Encoding.UTF8.GetBytes("id:pass")
zk.addAuthInfo("auth",new byte[0]);
//digest模式认证
byte[] auth=Encoding.UTF8.GetBytes("加密后的字符串")
zk.addAuthInfo("digest",new byte[0]);
ZookeeperNetEx关闭会话使用的是closeAsync方法,调用这个方法之后,当前连接对象ZooKeeper就不能再访问了
public Task closeAsync();
其他常用方法就不介绍了,一般时候基本上也用不上。
简单封装
真实项目中,我们连接Zookeeper多数只是为了创建znode节点,读取数据等等操作,一般不会去设置ACL等权限,甚至连认证都可能不会用到,为了更好使用ZookeeperNetEx,我做了一层简单的封装,用以满足常见的CRUD操作,同时也让它更符合我们.net开发的一些习惯。
using org.apache.zookeeper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Threading;
using System.Text;
using org.apache.zookeeper.data;
using org.apache.utils;
using System.Diagnostics; namespace AspNetCore.ZookeeperConsole
{ /// <summary>
/// Zookeeper辅助类
/// </summary>
public class ZookeeperHelper : Watcher, IDisposable
{
/// <summary>
/// Zookeeper路径分隔符
/// </summary>
string sep = "/";
/// <summary>
/// Zookeeper访问对象
/// </summary>
ZooKeeper zookeeper;
/// <summary>
/// Zookeeper集群地址
/// </summary>
string[] address;
/// <summary>
/// 路径监控节点列表
/// </summary>
ConcurrentDictionary<string, NodeWatcher> nodeWatchers = new ConcurrentDictionary<string, NodeWatcher>();
/// <summary>
/// 节点的默认权限
/// </summary>
List<ACL> defaultACL = ZooDefs.Ids.OPEN_ACL_UNSAFE;
/// <summary>
/// 默认的监听器
/// </summary>
DefaultWatcher defaultWatcher;
/// <summary>
/// 监控定时器
/// </summary>
System.Timers.Timer timer;
/// <summary>
/// 同步锁
/// </summary>
AutoResetEvent are = new AutoResetEvent(false);
/// <summary>
/// 是否正常关闭
/// </summary>
bool isClose = false; /// <summary>
/// 回话超时时间
/// </summary>
public int SessionTimeout { get; set; } = 10000;
/// <summary>
/// 当前路径
/// </summary>
public string CurrentPath { get; private set; }
/// <summary>
/// 是否已连接Zookeeper
/// </summary>
public bool Connected { get { return zookeeper != null && (zookeeper.getState() == ZooKeeper.States.CONNECTED || zookeeper.getState() == ZooKeeper.States.CONNECTEDREADONLY); } }
/// <summary>
/// Zookeeper是否有写的权限
/// </summary>
public bool CanWrite { get { return zookeeper != null && zookeeper.getState() == ZooKeeper.States.CONNECTED; } }
/// <summary>
/// 数据编码
/// </summary>
public Encoding Encoding { get; set; } = Encoding.Default;
/// <summary>
/// 释放时发生
/// </summary>
public event Action OnDisposing;
/// <summary>
/// 在重新连接时发生
/// </summary>
public event Action OnConnected; /// <summary>
/// 构造函数
/// </summary>
/// <param name="address">集群地址(host:prot)</param>
public ZookeeperHelper(params string[] address) : this(address, "")
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="address">集群地址(host:prot)</param>
/// <param name="root">初始化根路经</param>
public ZookeeperHelper(string[] address, string root)
{
this.address = address.ToArray();
CurrentPath = string.IsNullOrWhiteSpace(root) ? sep : root; SetLogger(new ZookeeperLogger()); timer = new System.Timers.Timer();
timer.Interval = 5000;
timer.Elapsed += Timer_Elapsed;
} /// <summary>
/// Zookeeper的日志设置
/// </summary>
/// <param name="log"></param>
public static void SetLogger(ZookeeperLogger log)
{
ZooKeeper.LogLevel = log.LogLevel;
ZooKeeper.LogToFile = log.LogToFile;
ZooKeeper.LogToTrace = log.LogToTrace;
ZooKeeper.CustomLogConsumer = log;
} #region 私有方法
/// <summary>
/// 定时器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Enabled = false; if (Monitor.TryEnter(timer))//每次只能一个线程进去
{
if (!isClose)
{
//Thread.Sleep(SessionTimeout);
if (!Connected)
{
try
{
zookeeper?.closeAsync();
are.Reset();
zookeeper = new ZooKeeper(string.Join(",", address), SessionTimeout, defaultWatcher);
if (are.WaitOne(SessionTimeout) && Connected)//会话未超时,表示成功连接
{
//挂载监听器
foreach (var key in nodeWatchers.Keys)
{
NodeWatcher watcher;
if (nodeWatchers.TryGetValue(key, out watcher))
{
WatchAsync(key, watcher, true).Wait();
}
}
OnConnected?.Invoke();
Monitor.Exit(timer);
return;
}
}
catch { }
timer.Enabled = true;
}
} Monitor.Exit(timer);
}
}
/// <summary>
/// 检查连接是否正常
/// </summary>
private void CheckConnection()
{
if (!Connected)
{
throw new Exception("fail to connect to the server:" + string.Join(",", address));
}
}
/// <summary>
/// 检查是否具有写的权限
/// </summary>
private void CheckWriten()
{
if (!CanWrite)
{
throw new Exception("this connection is in readonly mode");
}
}
/// <summary>
/// 连接数据成Zookeeper的路径格式
/// </summary>
/// <param name="paths">路径</param>
/// <returns>连接后的路径</returns>
private string Combine(params string[] paths)
{
List<string> list = new List<string>();
foreach (var path in paths)
{
var ps = path.Split(new string[] { "/", "\\" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var p in ps)
{
if (p == ".")//当前路径
{
continue;
}
else if (p == "..")//回退
{
if (list.Count == 0)
{
throw new ArgumentOutOfRangeException("path is out of range");
} list.RemoveAt(list.Count - 1);
}
else
{
list.Add(p);
}
}
} return sep + string.Join(sep, list.ToArray());
}
/// <summary>
/// 使用指定的分隔符连接路径
/// </summary>
/// <param name="sep">分隔符</param>
/// <param name="paths">路径</param>
/// <returns>连接后的路径</returns>
private string MakePathName(string sep, params string[] paths)
{
List<string> list = new List<string>();
foreach (var path in paths)
{
var ps = path.Split(new string[] { "/", "\\" }, StringSplitOptions.RemoveEmptyEntries);
list.AddRange(ps);
} return string.Join(sep, list.ToArray());
}
/// <summary>
/// 获取绝对路径
/// </summary>
/// <param name="path">路径</param>
/// <param name="isAbsolute">路径是否是绝对路径</param>
/// <returns>绝对路径</returns>
private string GetAbsolutePath(string path, bool isAbsolute)
{
if (!isAbsolute)
{
path = Combine(CurrentPath, path);
}
else
{
path = Combine(path);
}
return path;
}
#endregion /// <summary>
/// 连接Zookeeper
/// </summary>
/// <returns>成功连接返回true,否则返回false</returns>
public bool Connect()
{
if (Connected)
{
return true;
}
if (zookeeper == null)
{
lock (this)
{
defaultWatcher = defaultWatcher ?? new DefaultWatcher(this, are);
are.Reset();
zookeeper = new ZooKeeper(string.Join(",", address), SessionTimeout, defaultWatcher);
are.WaitOne(SessionTimeout);
}
}
if (!Connected)
{
return false;
}
OnConnected?.Invoke(); return true;
}
/// <summary>
/// 关闭连接
/// </summary>
public void Close()
{
isClose = true;
if (Connected)
{
zookeeper.closeAsync().Wait();
} }
/// <summary>
/// 监控回调
/// </summary>
/// <param name="event">回调事件</param>
/// <returns>异步</returns>
public async override Task process(WatchedEvent @event)
{
ZookeeperEvent ze = new ZookeeperEvent(@event); if (!string.IsNullOrEmpty(ze.Path))
{
NodeWatcher watcher;
if (nodeWatchers.TryGetValue(ze.Path, out watcher))
{
if (watcher != null)
{
try
{
watcher.Process(ze);
}
catch { }
await WatchAsync(ze.Path, watcher, true);//重新监控
}
}
}
}
/// <summary>
/// 修改当前目录地址
/// </summary>
/// <param name="path"></param>
public void ChangeDirectory(string path)
{
this.CurrentPath = Combine(path);
}
/// <summary>
/// 切换到相对目录下
/// </summary>
/// <param name="path"></param>
public void Goto(string path)
{
this.CurrentPath = Combine(this.CurrentPath, path);
}
/// <summary>
/// 使用认证
/// </summary>
/// <param name="scheme">认证类型</param>
/// <param name="auth">认证数据</param>
public void Authorize(AuthScheme scheme, string auth = "")
{
CheckConnection();
zookeeper.addAuthInfo(scheme.ToString().ToLower(), Encoding.GetBytes(auth));
}
#region 监听与取消
/// <summary>
/// 对当前路径添加监控
/// </summary>
/// <param name="delegate">监控</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool> WatchAsync(WatcherEvent @delegate)
{
return await WatchAsync(CurrentPath, @delegate, true);
}
/// <summary>
/// 对当前路径添加监控
/// </summary>
/// <param name="watcher">监控</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool> WatchAsync(NodeWatcher watcher)
{
return await WatchAsync(CurrentPath, watcher, true);
}
/// <summary>
/// 对指定路径添加监控
/// </summary>
/// <param name="path">节点路径</param>
/// <param name="delegate">监控</param>
/// <param name="isAbsolutePath">是否绝对路径</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool> WatchAsync(string path, WatcherEvent @delegate, bool isAbsolutePath = false)
{
var array = await WatchManyAsync(new string[] { path }, @delegate, isAbsolutePath);
return array.FirstOrDefault();
}
/// <summary>
/// 对指定路径添加监控
/// </summary>
/// <param name="path">节点路径</param>
/// <param name="watcher">监控</param>
/// <param name="isAbsolutePath">是否绝对路径</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool> WatchAsync(string path, NodeWatcher watcher, bool isAbsolutePath = false)
{
var array = await WatchManyAsync(new string[] { path }, watcher, isAbsolutePath);
return array.FirstOrDefault();
}
/// <summary>
/// 监控多个路径,但是不包括子路径
/// </summary>
/// <param name="paths">节点路径</param>
/// <param name="delegate">监控</param>
/// <param name="isAbsolutePath">是否绝对路径</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool[]> WatchManyAsync(string[] paths, WatcherEvent @delegate, bool isAbsolutePath = false)
{
var watcher = new NodeWatcher();
watcher.AllTypeChanged += @delegate;
return await WatchManyAsync(paths, watcher, isAbsolutePath);
}
/// <summary>
/// 监控多个路径,但是不包括子路径
/// </summary>
/// <param name="paths">节点路径</param>
/// <param name="watcher">监控</param>
/// <param name="isAbsolutePath">是否绝对路径</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool[]> WatchManyAsync(string[] paths, NodeWatcher watcher, bool isAbsolutePath = false)
{
CheckConnection();
List<bool> list = new List<bool>();
foreach (var path in paths)
{
try
{
var p = GetAbsolutePath(path, isAbsolutePath);
if (await zookeeper.existsAsync(p, this) != null)
{
nodeWatchers[p] = watcher;
list.Add(true);
}
else
{
nodeWatchers.TryRemove(p, out _);
list.Add(false);
}
}
catch
{
list.Add(false);
}
}
return list.ToArray();
}
/// <summary>
/// 监控当前路径,包括子路径
/// </summary>
/// <param name="delegate">监控</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool> WatchAllAsync(WatcherEvent @delegate)
{
return await WatchAllAsync(CurrentPath, @delegate, true);
}
/// <summary>
/// 监控当前路径,包括子路径
/// </summary>
/// <param name="watcher">监控</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool> WatchAllAsync(NodeWatcher watcher)
{
return await WatchAllAsync(CurrentPath, watcher, true);
}
/// <summary>
/// 监控指定路径,包括子路径
/// </summary>
/// <param name="path">节点路径</param>
/// <param name="delegate">监控</param>
/// <param name="isAbsolutePath">是否绝对路径</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool> WatchAllAsync(string path, WatcherEvent @delegate, bool isAbsolutePath = false)
{
var array = await WatchAllAsync(new string[] { path }, @delegate, isAbsolutePath);
return array.FirstOrDefault();
}
/// <summary>
/// 监控指定路径,包括子路径
/// </summary>
/// <param name="path">节点路径</param>
/// <param name="watcher">监控</param>
/// <param name="isAbsolutePath">是否绝对路径</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool> WatchAllAsync(string path, NodeWatcher watcher, bool isAbsolutePath = false)
{
var array = await WatchAllAsync(new string[] { path }, watcher, isAbsolutePath);
return array.FirstOrDefault();
}
/// <summary>
/// 监控所有路径,包括子路径
/// </summary>
/// <param name="paths">节点路径</param>
/// <param name="delegate">监控</param>
/// <param name="isAbsolutePath">是否绝对路径</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool[]> WatchAllAsync(string[] paths, WatcherEvent @delegate, bool isAbsolutePath = false)
{
var watcher = new NodeWatcher();
watcher.AllTypeChanged += @delegate;
return await WatchAllAsync(paths, watcher, isAbsolutePath);
}
/// <summary>
/// 监控所有路径,包括子路径
/// </summary>
/// <param name="paths">节点路径</param>
/// <param name="watcher">监控</param>
/// <param name="isAbsolutePath">是否绝对路径</param>
/// <returns>异步,true表示成功,false表示失败</returns>
public async Task<bool[]> WatchAllAsync(string[] paths, NodeWatcher watcher, bool isAbsolutePath = false)
{
CheckConnection();
List<bool> list = new List<bool>();
foreach (var path in paths)
{
try
{
var p = GetAbsolutePath(path, isAbsolutePath);
if (await zookeeper.existsAsync(p, this) != null)
{
nodeWatchers[p] = watcher;
list.Add(true); var result = await zookeeper.getChildrenAsync(p);
await WatchAllAsync(result.Children.Select(c => Combine(p, c)).ToArray(), watcher, true);
}
else
{
nodeWatchers.TryRemove(p, out _);
list.Add(false);
}
}
catch
{
list.Add(false);
}
} return list.ToArray();
}
/// <summary>
/// 取消多个指定路径上的监控
/// </summary>
/// <param name="path">节点路径</param>
/// <param name="isAbsolutePath">是否绝对路径</param>
/// <returns>异步</returns>
public async Task CancelAsync(string path, bool isAbsolutePath = true)
{
await CancelAsync(new string[] { path }, isAbsolutePath);
}
/// <summary>
/// 取消多个指定路径上的监控
/// </summary>
/// <param name="path">节点路径</param>
/// <param name="isAbsolutePath">是否绝对路径</param>
/// <returns>异步</returns>
public async Task CancelAsync(string[] paths, bool isAbsolutePath = true)
{
foreach (var path in paths)
{
var p = GetAbsolutePath(path, isAbsolutePath);
nodeWatchers.TryRemove(p, out _);
await Task.CompletedTask;
}
}
/// <summary>
/// 获取指定路径上的监控
/// </summary>
/// <param name="path">节点路径</param>
/// <param name="isAbsolutePath">是否绝对路径</param>
/// <returns>存在则返回监控对象,否则返回null</returns>
public NodeWatcher GetWatcher(string path, bool isAbsolutePath = true)
{
path = GetAbsolutePath(path, isAbsolutePath);
NodeWatcher watcher;
if (nodeWatchers.TryGetValue(path, out watcher))
{
return watcher;
} return null;
}
#endregion
#region 基本数据操作
/// <summary>
/// 当前节点是否存在
/// </summary>
/// <returns>存在返回true,否则返回false</returns>
public bool Exists()
{
return ExistsAsync().GetAwaiter().GetResult();
}
/// <summary>
/// 指定节点是否存在(相对当前节点)
/// </summary>
/// <param name="path">节点路径</param>
/// <returns>存在返回true,否则返回false</returns>
public bool Exists(string path)
{
return ExistsAsync(path).GetAwaiter().GetResult();
}
/// <summary>
/// 指定节点是否存在
/// </summary>
/// <param name="absolutePath">绝对路径</param>
/// <returns>存在返回true,否则返回false</returns>
public bool ExistsByAbsolutePath(string absolutePath)
{
return ExistsByAbsolutePathAsync(absolutePath).GetAwaiter().GetResult();
}
/// <summary>
/// 当前节点是否存在
/// </summary>
/// <returns>异步,存在返回true,否则返回false</returns>
public async Task<bool> ExistsAsync()
{
return await ExistsByAbsolutePathAsync(CurrentPath);
}
/// <summary>
/// 指定节点是否存在(相对当前节点)
/// </summary>
/// <param name="path">节点路径</param>
/// <returns>异步,存在返回true,否则返回false</returns>
public async Task<bool> ExistsAsync(string path)
{
path = Combine(CurrentPath, path);
return await ExistsByAbsolutePathAsync(path);
}
/// <summary>
/// 指定节点是否存在
/// </summary>
/// <param name="absolutePath">绝对路径</param>
/// <returns>异步,存在返回true,否则返回false</returns>
public async Task<bool> ExistsByAbsolutePathAsync(string absolutePath)
{
absolutePath = Combine(absolutePath);
return await zookeeper.existsAsync(absolutePath, false) != null;
}
/// <summary>
/// 添加或者修改当前路径上的数据
/// </summary>
/// <param name="value">数据</param>
/// <param name="persistent">是否持久节点</param>
/// <param name="sequential">是否顺序节点</param>
/// <returns>znode节点名,不包含父节点路径</returns>
public string SetData(string value, bool persistent = false, bool sequential = false)
{
return SetDataAsync(value, persistent, sequential).GetAwaiter().GetResult();
}
/// <summary>
/// 添加或者修改指定相对路径上的数据
/// </summary>
/// <param name="path">相对路径</param>
/// <param name="value">数据</param>
/// <param name="persistent">是否持久节点</param>
/// <param name="sequential">是否顺序节点</param>
/// <returns>znode节点名,不包含父节点路径</returns>
public string SetData(string path, string value, bool persistent = false, bool sequential = false)
{
return SetDataAsync(path, value, persistent, sequential).GetAwaiter().GetResult();
}
/// <summary>
/// 添加或者修改指定绝对路径上的数据
/// </summary>
/// <param name="absolutePath">绝对路径</param>
/// <param name="value">数据</param>
/// <param name="persistent">是否持久节点</param>
/// <param name="sequential">是否顺序节点</param>
/// <returns>znode节点名,不包含父节点路径</returns>
public string SetDataByAbsolutePath(string absolutePath, string value, bool persistent = false, bool sequential = false)
{
return SetDataByAbsolutePathAsync(absolutePath, value, persistent, sequential).GetAwaiter().GetResult();
}
/// <summary>
/// 添加或者修改当前路径上的数据
/// </summary>
/// <param name="value">数据</param>
/// <param name="persistent">是否持久节点</param>
/// <param name="sequential">是否顺序节点</param>
/// <returns>znode节点名,不包含父节点路径</returns>
public async Task<string> SetDataAsync(string value, bool persistent = false, bool sequential = false)
{
return await SetDataByAbsolutePathAsync(CurrentPath, value, persistent, sequential);
}
/// <summary>
/// 添加或者修改指定相对路径上的数据
/// </summary>
/// <param name="path">相对路径</param>
/// <param name="value">数据</param>
/// <param name="persistent">是否持久节点</param>
/// <param name="sequential">是否顺序节点</param>
/// <returns>znode节点名,不包含父节点路径</returns>
public async Task<string> SetDataAsync(string path, string value, bool persistent = false, bool sequential = false)
{
path = Combine(CurrentPath, path);
return await SetDataByAbsolutePathAsync(path, value, persistent, sequential);
}
/// <summary>
/// 添加或者修改指定绝对路径上的数据
/// </summary>
/// <param name="absolutePath">绝对路径</param>
/// <param name="value">数据</param>
/// <param name="persistent">是否持久节点</param>
/// <param name="sequential">是否顺序节点</param>
/// <returns>znode节点名,不包含父节点路径</returns>
public async Task<string> SetDataByAbsolutePathAsync(string absolutePath, string value, bool persistent = false, bool sequential = false)
{
CheckConnection();
CheckWriten(); absolutePath = Combine(absolutePath);
if (await zookeeper.existsAsync(absolutePath, false) == null)
{
absolutePath = await zookeeper.createAsync(absolutePath, Encoding.GetBytes(value), defaultACL, persistent ?
sequential ? CreateMode.PERSISTENT_SEQUENTIAL : CreateMode.PERSISTENT :
sequential ? CreateMode.EPHEMERAL_SEQUENTIAL : CreateMode.EPHEMERAL);
}
else
{
await zookeeper.setDataAsync(absolutePath, Encoding.GetBytes(value));
}
return absolutePath.Split(new string[] { sep }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
}
/// <summary>
/// 获取指定相对路径上的数据
/// </summary>
/// <param name="path">相对路径</param>
/// <returns>相对路径上的数据</returns>
public string GetData(string path)
{
return GetDataAsync(path).GetAwaiter().GetResult();
}
/// <summary>
/// 获取指定绝对路径上的数据
/// </summary>
/// <param name="absolutePath">绝对路径</param>
/// <returns>相对路径上的数据</returns>
public string GetDataByAbsolutePath(string absolutePath)
{
return GetDataByAbsolutePathAsync(absolutePath).GetAwaiter().GetResult();
}
/// <summary>
/// 获取指定相对路径上的数据
/// </summary>
/// <param name="path">相对路径</param>
/// <returns>相对路径上的数据</returns>
public async Task<string> GetDataAsync(string path)
{
path = Combine(CurrentPath, path);
return await GetDataByAbsolutePathAsync(path);
}
/// <summary>
/// 获取指定绝对路径上的数据
/// </summary>
/// <param name="absolutePath">绝对路径</param>
/// <returns>绝对路径上的数据</returns>
public async Task<string> GetDataByAbsolutePathAsync(string absolutePath)
{
CheckConnection();
absolutePath = Combine(absolutePath);
if (await zookeeper.existsAsync(absolutePath, false) == null)
{
return "";
}
var data = await zookeeper.getDataAsync(absolutePath, false);
return Encoding.GetString(data.Data);
}
/// <summary>
/// 获取指定节点及其字节点的所有值,使用路径做键返回字典型
/// </summary>
/// <param name="sep"></param>
/// <returns></returns>
public async Task<IDictionary<string, string>> GetDictionaryAsync(string sep = ":")
{
CheckConnection();
Dictionary<string, string> dict = new Dictionary<string, string>();
async Task action(string path)
{
try
{
var result = await zookeeper.getChildrenAsync(path, false);
string name = MakePathName(sep, path);
dict[name] = await GetDataByAbsolutePathAsync(path);
foreach (var child in result.Children)
{
var p = Combine(path, child);
await action(p);
}
}
catch (Exception ex)
{ }
} await action(CurrentPath);
return dict;
} /// <summary>
/// 获取子节点
/// </summary>
/// <param name="path">相对路径</param>
/// <param name="order">是否按时间排序</param>
/// <returns>子节点数组(节点路径不含父节点路径)</returns>
public async Task<string[]> GetChildrenAsync(string path, bool order = false)
{
path = Combine(CurrentPath, path);
return await GetChildrenByAbsolutePathAsync(path, order);
}
/// <summary>
/// 获取指定路径绝对路径下的子节点
/// </summary>
/// <param name="absolutePath">绝对路径</param>
/// <param name="order">是否按时间排序</param>
/// <returns>子节点数组(节点路径不含父节点路径)</returns>
public async Task<string[]> GetChildrenByAbsolutePathAsync(string absolutePath, bool order = false)
{
var result = await zookeeper.getChildrenAsync(absolutePath, false);
if (!order)
{
return result.Children.ToArray();
} List<(string, long)> list = new List<(string, long)>();
foreach (var child in result.Children)
{
var p = Combine(absolutePath, child);
var stat = await zookeeper.existsAsync(p, false);
if (stat != null)
{
list.Add((child, stat.getCtime()));
}
} return list.OrderBy(l => l.Item2).Select(l => l.Item1).ToArray();
}
/// <summary>
/// 移除当前路径节点
/// </summary>
public void Delete()
{
DeleteAsync().Wait();
}
/// <summary>
/// 移除相对当前的指定路径节点及子节点
/// </summary>
/// <param name="path">相对路径</param>
public void Delete(string path)
{
DeleteAsync(path).Wait();
}
/// <summary>
/// 移除指定绝对路径节点及子节点
/// </summary>
/// <param name="absolutePath">绝对路径</param>
public void DeleteByAbsolutePath(string absolutePath)
{
DeleteByAbsolutePathAsync(absolutePath).Wait();
}
/// <summary>
/// 移除当前路径节点
/// </summary>
public async Task DeleteAsync()
{
await DeleteByAbsolutePathAsync(CurrentPath);
}
/// <summary>
/// 移除相对当前的指定路径节点及子节点
/// </summary>
/// <param name="path">相对路径</param>
public async Task DeleteAsync(string path)
{
path = Combine(CurrentPath, path);
await DeleteByAbsolutePathAsync(path);
}
/// <summary>
/// 移除指定绝对路径节点及子节点
/// </summary>
/// <param name="absolutePath">绝对路径</param>
public async Task DeleteByAbsolutePathAsync(string absolutePath)
{
if (await ExistsByAbsolutePathAsync(absolutePath))
{
var children = await GetChildrenByAbsolutePathAsync(absolutePath);
foreach (var child in children)
{
var path = Combine(absolutePath, child);
await DeleteByAbsolutePathAsync(path);
} absolutePath = Combine(absolutePath);
await zookeeper.deleteAsync(absolutePath);
}
}
#endregion
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
OnDisposing?.Invoke();
Close();
timer?.Dispose();
nodeWatchers?.Clear();
are?.Dispose();
GC.Collect();
} /// <summary>
/// 默认的监听器,用于初始化使用
/// </summary>
public class DefaultWatcher : Watcher
{
/// <summary>
/// waithandle同步
/// </summary>
EventWaitHandle ewh;
/// <summary>
/// 辅助类
/// </summary>
ZookeeperHelper zookeeperHelper; public DefaultWatcher(ZookeeperHelper zookeeperHelper, EventWaitHandle ewh)
{
this.ewh = ewh;
this.zookeeperHelper = zookeeperHelper;
}
/// <summary>
/// 回调
/// </summary>
/// <param name="event">监听事件对象</param>
/// <returns></returns>
public override Task process(WatchedEvent @event)
{
var state = @event.getState();
if (state == Event.KeeperState.ConnectedReadOnly || state == Event.KeeperState.SyncConnected)//连接时
{
ewh.Set();
}
else if ((state == Event.KeeperState.Expired) && !zookeeperHelper.isClose)//回话过期重新建立连接
{
zookeeperHelper.timer.Enabled = true;
} return Task.FromResult(1);
}
}
}
/// <summary>
/// 认证类型
/// </summary>
public enum AuthScheme
{
/// <summary>
/// 下面只有一个id,叫anyone,world:anyone代表任何人,zookeeper中对所有人有权限的结点就是属于world:anyone类型的。创建节点的默认权限。有唯一的id是anyone授权的时候的模式为 world:anyone:rwadc 表示所有人都对这个节点有rwadc的权限
/// </summary>
World = 0,
/// <summary>
///不需要id,只要是通过authentication的user都有权限(zookeeper支持通过kerberos来进行authencation, 也支持username/password形式的authentication)
/// </summary>
Auth = 1,
/// <summary>
/// 它对应的id为username:BASE64(SHA1(password)),它需要先通过加密过的username:password形式的authentication。
/// </summary>
Digest = 2,
/// <summary>
///它对应的id为客户机的IP地址,设置的时候可以设置一个ip段,比如ip:192.168.1.0/16。
/// </summary>
Ip = 3,
/// <summary>
/// 在这种scheme情况下,对应的id拥有超级权限,可以做任何事情(cdrwa)
/// </summary>
Super = 4
}
/// <summary>
/// Zookeeper事件数据
/// </summary>
public class ZookeeperEvent
{
public ZookeeperEvent(WatchedEvent @event)
{
switch (@event.getState())
{
case Watcher.Event.KeeperState.AuthFailed: State = EventState.AuthFailed; break;
case Watcher.Event.KeeperState.ConnectedReadOnly: State = EventState.ConnectedReadOnly; break;
case Watcher.Event.KeeperState.Disconnected: State = EventState.Disconnected; break;
case Watcher.Event.KeeperState.Expired: State = EventState.Expired; break;
case Watcher.Event.KeeperState.SyncConnected: State = EventState.SyncConnected; break;
} switch (@event.get_Type())
{
case Watcher.Event.EventType.NodeChildrenChanged: Type = EventType.NodeChildrenChanged; break;
case Watcher.Event.EventType.NodeCreated: Type = EventType.NodeCreated; break;
case Watcher.Event.EventType.NodeDataChanged: Type = EventType.NodeDataChanged; break;
case Watcher.Event.EventType.NodeDeleted: Type = EventType.NodeDeleted; break;
case Watcher.Event.EventType.None: Type = EventType.None; break;
} Path = @event.getPath();
}
/// <summary>
/// 当前连接状态
/// </summary>
public EventState State { get; private set; }
/// <summary>
/// 事件类型
/// </summary>
public EventType Type { get; private set; }
/// <summary>
/// 事件路径
/// </summary>
public string Path { get; private set; } /// <summary>
/// 连接状态
/// </summary>
public enum EventState
{/// <summary>
/// 超时
/// </summary>
Expired = -112,
/// <summary>
/// 连接已断开
/// </summary>
Disconnected = 0,
/// <summary>
/// 已建立连接
/// </summary>
SyncConnected = 3,
/// <summary>
/// 认证失败
/// </summary>
AuthFailed = 4,
/// <summary>
/// 已建立连接,但是只支持只读模式
/// </summary>
ConnectedReadOnly = 5
}
/// <summary>
/// 时间类型
/// </summary>
public enum EventType
{
/// <summary>
/// 空类型,如:建立连接时
/// </summary>
None = -1,
/// <summary>
/// 节点创建时
/// </summary>
NodeCreated = 1,
/// <summary>
/// 节点删除时
/// </summary>
NodeDeleted = 2,
/// <summary>
/// 节点数据改变时
/// </summary>
NodeDataChanged = 3,
/// <summary>
/// 节点增加子节点时
/// </summary>
NodeChildrenChanged = 4
}
}
/// <summary>
/// 监控对象
/// </summary>
public class NodeWatcher
{
/// <summary>
/// 节点创建时调用事件
/// </summary>
public event WatcherEvent NodeCreated;
/// <summary>
/// 节点删除时调用事件
/// </summary>
public event WatcherEvent NodeDeleted;
/// <summary>
/// 节点数据改变时调用事件
/// </summary>
public event WatcherEvent NodeDataChanged;
/// <summary>
/// 节点增加子节点时调用事件
/// </summary>
public event WatcherEvent NodeChildrenChanged;
/// <summary>
/// 不区分类型,所有的类型都会调用
/// </summary>
public event WatcherEvent AllTypeChanged; /// <summary>
/// 触发,执行事件
/// </summary>
/// <param name="event"></param>
public void Process(ZookeeperEvent @event)
{
try
{
switch (@event.Type)
{
case ZookeeperEvent.EventType.NodeChildrenChanged:
NodeChildrenChanged?.Invoke(@event);
break;
case ZookeeperEvent.EventType.NodeCreated:
NodeCreated?.Invoke(@event);
break;
case ZookeeperEvent.EventType.NodeDeleted:
NodeDeleted?.Invoke(@event);
break;
case ZookeeperEvent.EventType.NodeDataChanged:
NodeDataChanged?.Invoke(@event);
break;
} AllTypeChanged?.Invoke(@event);
}
catch { }
}
}
/// <summary>
/// 监控事件委托
/// </summary>
/// <param name="event"></param>
public delegate void WatcherEvent(ZookeeperEvent @event);
/// <summary>
/// Zookeeper默认日志记录
/// </summary>
public class ZookeeperLogger : ILogConsumer
{
/// <summary>
/// 是否记录日志到文件
/// </summary>
public bool LogToFile { get; set; } = false;
/// <summary>
/// 是否记录堆栈信息
/// </summary>
public bool LogToTrace { get; set; } = true;
/// <summary>
/// 日志级别
/// </summary>
public TraceLevel LogLevel { get; set; } = TraceLevel.Warning; /// <summary>
/// 日志记录
/// </summary>
/// <param name="severity"></param>
/// <param name="className"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
public virtual void Log(TraceLevel severity, string className, string message, Exception exception)
{
Console.WriteLine(string.Format("Level:{0} className:{1} message:{2}", severity, className, message));
Console.WriteLine(exception.StackTrace);
}
}
}
ZookeeperHelper
简单的使用例子:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading; namespace AspNetCore.ZookeeperConsole
{
class Program
{
static void Main(string[] args)
{
//Zookeeper连接字符串,采用host:port格式,多个地址之间使用逗号(,)隔开
string[] address = new string[] { "192.168.209.133:2181", "192.168.209.133:2181", "192.168.209.133:2181" };
//会话超时时间,单位毫秒
int sessionTimeOut = 10000; ZookeeperHelper zookeeperHelper = new ZookeeperHelper(address, "/");
zookeeperHelper.SessionTimeout = sessionTimeOut;
zookeeperHelper.Connect();//发起连接
while (!zookeeperHelper.Connected)
{
Thread.Sleep(1000); //停一秒,等待连接完成
} //创建znode节点
{
zookeeperHelper.SetData("/mynode", "hello world", true, false);
Console.WriteLine("完成创建节点");
} //节点是否存在
{
var exists = zookeeperHelper.Exists("/mynode");
Console.WriteLine("节点是否存在:" + exists);
} //添加监听器
{
zookeeperHelper.WatchAsync("/mynode", (e) =>
{
Console.WriteLine($"recieve: Path-{e.Path} State-{e.State} Type-{e.Type}");
}).Wait();
} //获取节点数据
{
var value = zookeeperHelper.GetData("/mynode");
Console.WriteLine("完成读取节点:" + value);
} //设置节点数据
{
zookeeperHelper.SetData("/mynode", "hello world again");
Console.WriteLine("设置节点数据");
} //重新获取节点数据
{
var value = zookeeperHelper.GetData("/mynode");
Console.WriteLine("重新获取节点数据:" + value);
} //移除节点
{
zookeeperHelper.Delete("/mynode");
Console.WriteLine("移除节点");
} Console.WriteLine("完成");
Console.ReadKey(); }
}
}
执行结果:
Zookeeper基础教程(四):C#连接使用Zookeeper的更多相关文章
- Zookeeper基础教程(三):Zookeeper连接使用—zkCli
上一篇介绍Zookeeper的安装,并介绍了使用ZooInspector连接Zookeeper,这里主要介绍以命令行的形式介绍Zookeeper 假如我们已经安装了Zookeeper集群,集群中的安装 ...
- Zookeeper基础教程(二):Zookeeper安装
上一篇说了,一个Zookeeper集群一般认为至少需要3个节点,所以我们这里安装需要准备三台虚拟机: # 192.168.209.133 test1 # 192.168.209.134 test2 # ...
- Zookeeper基础教程(六):.net core使用Zookeeper
Demo代码已提交到gitee,感兴趣的更有可以直接克隆使用,地址:https://gitee.com/shanfeng1000/dotnetcore-demo/tree/master/Zookeep ...
- Zookeeper基础教程(五):C#实现Zookeeper分布式锁
分布式锁 互联网初期,我们系统一般都是单点部署,也就是在一台服务器完成系统的部署,后期随着用户量的增加,服务器的压力也越来越大,响应速度越来越慢,甚至出现服务器崩溃的情况. 为解决服务器压力太大,响应 ...
- Qt零基础教程(四) QWidget详解篇
在博客园里面转载我自己写的关于Qt的基础教程,没次写一篇我会在这里更新一下目录: Qt零基础教程(四) QWidget详解(1):创建一个窗口 Qt零基础教程(四) QWidget详解(2):QWid ...
- Qt零基础教程(四)QWidget详解(3):QWidget的几何结构
Qt零基础教程(四) QWidget详解(3):QWidget的几何结构 这篇文章里面分析了QWidget中常用的几种几何结构 下图是Qt提供的分析QWidget几何结构的一幅图,在帮助的 Wind ...
- zookeeper基础教程
一.关于zookeeper Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储, Zookeeper 作用 ...
- SpringCloud2.0 Ribbon 服务发现 基础教程(四)
1.启动[服务中心]集群,即 Eureka Server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) 2.启动[服务提供者]集群,即 Eureka Cli ...
- Zookeeper基础教程(一):认识Zookeeper
引用百度百科的话 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服 ...
随机推荐
- MyBatis(3):优化MyBatis配置文件
一.连接数据库的配置单独放在一个properties文件中 1,创建一个database.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql: ...
- 【编程思想】【设计模式】【结构模式Structural】装饰模式decorator
Python版 https://github.com/faif/python-patterns/blob/master/structural/decorator.py #!/usr/bin/env p ...
- js - 日期、时间 Date对象方法
Date 是 JS 内置的日期构造函数 var d = new Date(); // 这个是系统当前时间的日期实例 d.getYear(); // 返回 d 实例年份 - 1900 d.getFul ...
- 【Python】matplotlib直方图纵轴显示百分比
其实很简单,就是算了一下百分比权重,乘以了一个权重值 import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatt ...
- Pagination.js + Sqlite web系统分页
前端使用 jquery pagination.js 插件. 环境准备:jquery.js.pagination.js.pagination.css 附件下载:https://files.cnblogs ...
- C++内存管理:简易内存池的实现
什么是内存池? 在上一篇 C++内存管理:new / delete 和 cookie中谈到,频繁的调用 malloc 会影响运行效率以及产生额外的 cookie, 而内存池的思想是预先申请一大块内存, ...
- webapck搭建环境,让你知道vue中的h函数的作用和虚拟节点如何上树!
搭建环境 npm init 初始化项目 npm i -D snabbdom 安装 npm i -D webpack@5 webpack-cli@3 webpack-dev-server@3 简单介绍 ...
- 转:Memcached 线程部分源码分析
目前网上关于memcached的分析主要是内存管理部分,下面对memcached的线程模型做下简单分析 有不对的地方还请大家指正,对memcahced和libevent不熟悉的请先google之 先看 ...
- 《转》谈谈基于Kerberos的Windows Network Authentication
http://www.cnblogs.com/artech/archive/2007/07/05/807492.html 基本原理引入Key Distribution: KServer-Client从 ...
- [BUUCTF]PWN——level4
level4 附件 步骤: 例行检查,32位程序,开启了NX保护 运行一下程序,看看大概的情况 32位ida载入,首先检索程序里的字符串,根据上一步运行看到的字符串进行跳转 输入点在function里 ...