Beispiel #1
0
        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");
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            ZkConnection zkConnection        = new ZkConnection("192.168.60.6:2181");
            ZkClient     zkClient            = new ZkClient(ZkConnect, ZkSessionTimeout, ZkConnectionTimeout, new ZkStringSerializer());
            var          dataLock            = new ReentrantLock();
            var          dataExistsOrChanged = dataLock.NewCondition();

            zkClient.SubscribeDataChanges("/testyy", new DataChange(dataLock, dataExistsOrChanged, zkClient));
            //dataExistsOrChanged.Await(TimeSpan.FromMilliseconds(1000));

            Console.Read();
        }
        /// <summary>
        /// Close connection
        /// </summary>
        public async Task DisConnectAsync()
        {
            if (_currentConn != null)
            {
                await _currentConn.DisposeAsync();

                _currentConn = null;
            }

            if (_zk != null)
            {
                await _zk.DisposeAsync();

                _zk = null;
            }

            _serverRunningNodeReCreated = false;
            _logger.LogInformation("Disconnect success.");
        }
        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.");
        }