コード例 #1
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");
            }
        }
コード例 #2
0
        private async Task ConnectToZkAsync()
        {
            if (_zk == null || !_zk.Connected)
            {
                if (_zk != null)
                {
                    await _zk.DisposeAsync();
                }

                _zk = new ZkConnection(
                    new ZkConnectionOptions()
                {
                    ConnectionString = _options.ZkAddress, SessionTimeout = _options.ZkSessionTimeout
                },
                    _loggerFactory.CreateLogger <ZkConnection>());
                _zk.OnWatch += Zk_OnWatch;
                await _zk.ConnectAsync();
            }

            var times = 0;

            while (times < 60)
            {
                if (await _zk.NodeExistsAsync(ZK_SERVER_RUNNING_NODE))
                {
                    return;
                }

                await Task.Delay(1000);

                times++;

                _logger.LogWarning($"Can not find node {ZK_SERVER_RUNNING_NODE} on Zookeeper. Retrying... {times}");
            }

            throw new CanalConnectionException($"Can not find node {ZK_SERVER_RUNNING_NODE} on Zookeeper.");
        }