protected override void doRegister(RegisterMeta meta) { string directory = $"/meou/provider/{meta.getGroup()}/{meta.getName()}/{meta.getVersion()}"; try { var result = configClient.ExistsAsync(directory).ConfigureAwait(false).GetAwaiter().GetResult(); if (!result) { configClient.CreateRecursiveAsync(directory, null, org.apache.zookeeper.CreateMode.PERSISTENT).ConfigureAwait(false).GetAwaiter().GetResult(); } } catch (Exception e) { if (logger.IsEnabled(LogLevel.Warning)) { logger.LogWarning($"Create parent path failed, directory: {directory}, {e}."); } } try { string tempPath = $"{directory}/{meta.getHost()}:{meta.getPort()}:{meta.getWeight()}:{meta.getConnCount()}"; // The znode will be deleted upon the client's disconnect. configClient.CreateEphemeralAsync(tempPath).Wait(); } catch (Exception e) { if (logger.IsEnabled(LogLevel.Warning)) { logger.LogWarning($"Create register meta: {meta} path failed, {e}."); } } }
public async Task Exists() { using (ZKClient zkClient = ZKClientBuilder.NewZKClient(TestUtil.zkServers).Build()) { bool e = await zkClient.ExistsAsync("/testUserNode"); //返回 true表示节点存在 ,false表示不存在 Console.WriteLine(e); } }
public static async Task ReSetPathUnCreate(ZKClient _zkClient, string path) { var children = await _zkClient.GetChildrenAsync("/"); children.Where(x => x != "zookeeper").ToList().ForEach(async x => { await _zkClient.DeleteRecursiveAsync("/" + x); }); if (await _zkClient.ExistsAsync(path)) { await _zkClient.DeleteRecursiveAsync(path); } }
public static async Task ReSetPathCreate(ZKClient _zkClient, string path) { if (!(await _zkClient.ExistsAsync(path))) { await _zkClient.CreatePersistentAsync(path); } else { await _zkClient.DeleteRecursiveAsync(path); await _zkClient.CreatePersistentAsync(path); } }
public ZKDistributedLock(ZKClient zkClient, string lockPach) { if (!(zkClient.ExistsAsync(lockPach).GetAwaiter().GetResult())) { throw new NoNodeException($"The lockPath is not exists!,please create the node.[path:{_lockPath}]"); } this._zkClient = zkClient; this._lockPath = lockPach; this.countListener = new ZKChildListener(); this.countListener.ChildChangeHandler = async(parentPath, currentChilds) => { var check = await Task.Run(() => Check(currentSeq, currentChilds)); if (check) { semaphore.Release(); } }; this._zkClient.SubscribeChildChanges(_lockPath, countListener); }
public async Task TestAuth() { zkClient.AddAuthInfo(digest, Encoding.Default.GetBytes(allAuth)); if (await zkClient.ExistsAsync(testNode)) { await zkClient.DeleteAsync(testNode); } List <ACL> acls = new List <ACL>(); acls.Add(new ACL((int)Perms.ALL, new Id(digest, allAuth))); acls.Add(new ACL((int)Perms.READ, new Id(digest, readAuth))); acls.Add(new ACL((int)Perms.WRITE, new Id(digest, writeAuth))); acls.Add(new ACL((int)Perms.DELETE, new Id(digest, deleteAuth))); acls.Add(new ACL((int)Perms.ADMIN, new Id(digest, adminAuth))); await zkClient.CreatePersistentAsync(testNode, "test-data", acls); try { await zkClient.GetDataAsync <string>(testNode);//没有认证信息,读取会出错 } catch (Exception e) { } try { zkClient.AddAuthInfo(digest, Encoding.Default.GetBytes(adminAuth)); await zkClient.GetDataAsync <string>(testNode);//admin权限与read权限不匹配,读取也会出错 } catch (Exception e) { } try { zkClient.AddAuthInfo(digest, Encoding.Default.GetBytes(readAuth)); await zkClient.GetDataAsync <string>(testNode);//只有read权限的认证信息,才能正常读取 } catch (Exception e) { } try { await zkClient.SetDataAsync(testNode, "new-data");//没有认证信息,写入会失败 } catch (Exception e) { } try { zkClient.AddAuthInfo(digest, Encoding.Default.GetBytes(writeAuth)); await zkClient.SetDataAsync(testNode, "new-data");//加入认证信息后,写入正常 } catch (Exception e) { } try { zkClient.AddAuthInfo(digest, Encoding.Default.GetBytes(readAuth)); await zkClient.GetDataAsync <string>(testNode);//读取新值验证 } catch (Exception e) { } try { zkClient.AddAuthInfo(digest, Encoding.Default.GetBytes(deleteAuth)); await zkClient.DeleteAsync(testNode); } catch (Exception e) { } //注:zkClient.setAcl方法查看源码可以发现,调用了readData、setAcl二个方法 //所以要修改节点的ACL属性,必须同时具备read、admin二种权限 try { zkClient.AddAuthInfo(digest, Encoding.Default.GetBytes(adminAuth)); zkClient.AddAuthInfo(digest, Encoding.Default.GetBytes(readAuth)); List <ACL> acls1 = new List <ACL>(); acls1.Add(new ACL((int)Perms.ALL, new Id(digest, adminAuth))); await zkClient.SetACLAsync(testNode, acls1); ACLResult aclResult = await zkClient.GetACLAsync(testNode); } catch (Exception e) { } }