Ejemplo n.º 1
0
 /// <summary>
 /// 使用指定的路径名称设置锁
 /// </summary>
 /// <param name="path">锁名,不能包含路径分隔符(/)</param>
 /// <returns>返回锁名</returns>
 public string CreateLock(string path)
 {
     if (path.Contains("/"))
     {
         throw new ArgumentException("invalid path");
     }
     return(zookeeperHelper.SetData(path, "", false, true));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="lockerPath">分布式锁的根路径</param>
        /// <param name="sessionTimeout">回话过期时间</param>
        /// <param name="address">集群地址</param>
        public ZookeeperLocker(string lockerPath, int sessionTimeout, params string[] address)
        {
            this.address = address.ToArray();

            zookeeperHelper = new ZookeeperHelper(address, lockerPath);
            if (sessionTimeout > 0)
            {
                zookeeperHelper.SessionTimeout = sessionTimeout;
            }
            if (!zookeeperHelper.Connect())
            {
                throw new Exception("connect failed:" + string.Join(",", address));
            }
            lock (locker)
            {
                if (!zookeeperHelper.Exists())//根节点不存在则创建
                {
                    zookeeperHelper.SetData("", "", true);
                }
            }
        }