private static ZooKeeper CreateClient(ZkOption option) { if (_option == null) { throw new Exception("it is not initial"); } _option = option; return(option.SessionId == long.MinValue ? new ZooKeeper(option.ConnectionsString, option.SessionTimeout, ZkWatcher.Default, option.CanBeReadOnly) : new ZooKeeper(option.ConnectionsString, option.SessionTimeout, ZkWatcher.Default, option.SessionId, option.SessionPassword, option.CanBeReadOnly)); }
public static void Configure(ZkOption option) { lock (_lockObj) { if (Client == null) { _option = option; Client = CreateClient(option); Client.existsAsync("/locks").Wait(); } } }
public ZkLocker(string lockName, ZkOption option, int lockTimeout = 5000) { _client = option.SessionId == long.MinValue ? new ZooKeeper(option.ConnectionsString, option.SessionTimeout, this, option.CanBeReadOnly) : new ZooKeeper(option.ConnectionsString, option.SessionTimeout, this, option.SessionId, option.SessionPassword, option.CanBeReadOnly); if (_client.existsAsync("/locks").Result == null) { _client.createAsync("/locks", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).Wait(); } if (_client.existsAsync("/locks/" + lockName).Result == null) { _client.createAsync("/locks/" + lockName, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).Wait(); } _lockName = lockName; _lockTimeout = lockTimeout; }
/// <summary> /// 全局默认锁defaultLock /// </summary> /// <param name="option">配置选项</param> /// <param name="lockTimeout">设置锁超时时间</param> public ZkLocker(ZkOption option, int lockTimeout) : this("defaultLock", option, lockTimeout) { }
/// <summary> /// 全局默认锁defaultLock /// </summary> /// <param name="option">配置选项</param> public ZkLocker(ZkOption option) : this("defaultLock", option) { }