コード例 #1
0
        private async Task GetZkLockAsync(CanalClientRunningInfo runningInfo, bool waiting = false)
        {
            var times = 0;

            while (waiting && times < 60)
            {
                await Task.Delay(1000);

                times++;
                _logger.LogWarning($"Waiting for get lock {times}-60...");
            }

            if (await _zk.NodeExistsAsync(ZK_CLIENT_RUNNING_NODE))
            {
                _logger.LogInformation($"Node {ZK_CLIENT_RUNNING_NODE} exits, get Zookeeper lock failed. Other instances are running.");
                _logger.LogWarning("Waiting...");
            }
            else
            {
                try
                {
                    var clientNodeData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(runningInfo));

                    var parentNode = ZK_CLIENT_RUNNING_NODE.Replace("/running", "");
                    if (!await _zk.NodeExistsAsync(parentNode))
                    {
                        await _zk.CreateNodeAsync(parentNode, null,
                                                  new List <Acl>() { new Acl(AclPerm.All, AclScheme.World, AclId.World()) }, NodeType.Persistent);
                    }

                    await _zk.CreateNodeAsync(ZK_CLIENT_RUNNING_NODE, clientNodeData,
                                              new List <Acl>() { new Acl(AclPerm.All, AclScheme.World, AclId.World()) }, NodeType.Ephemeral);

                    await _zk.CreateNodeAsync(parentNode + "/" + runningInfo.Address, null,
                                              new List <Acl>() { new Acl(AclPerm.All, AclScheme.World, AclId.World()) }, NodeType.Ephemeral);

                    await _zk.NodeExistsAsync(ZK_CLIENT_RUNNING_NODE);

                    _completionSource.SetResult(0);
                    _logger.LogInformation("Get zookeeper lock success.");
                }
                catch (KeeperException.NodeExistsException e)
                {
                    if (e.getPath() != ZK_CLIENT_RUNNING_NODE)
                    {
                        _logger.LogError(e, "Error during get lock.");
                        Environment.Exit(-1);
                    }

                    _logger.LogInformation(
                        $"Node {ZK_CLIENT_RUNNING_NODE} exits, get Zookeeper lock failed. Other instances are running.");
                    _logger.LogWarning("Waiting...");
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Exception in lock");
                }
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: stulzq/NZookeeper
        static async Task Main(string[] args)
        {
            using var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .AddFilter("Microsoft", LogLevel.Debug)
                .AddFilter("System", LogLevel.Information)
                .AddConsole();
            });
            var logger = loggerFactory.CreateLogger <ZkConnection>();
            var zk     = new ZkConnection(new ZkConnectionOptions()
            {
                ConnectionString = "localhost:2181", SessionTimeout = 5000
            }, logger);

            zk.OnWatch += Zk_OnWatch;
            await zk.ConnectAsync();

            while (true)
            {
                await zk.CreateNodeAsync("/mynode", "ab",
                                         new List <Acl>() { new Acl(AclPerm.All, AclScheme.World, AclId.World()) }, NodeType.Ephemeral);

                await zk.SetDataAsync("/mynode", "111");

                await zk.GetChildrenAsync("/mynode");

                await Task.Delay(1000);

                await zk.DeleteNodeAsync("/mynode");
            }
        }