Пример #1
0
 /// <summary>
 /// 更新该节点上线状态
 /// </summary>
 /// <returns></returns>
 private static async Task UpdateOnlineState()
 {
     if (m_client != null)
     {
         string path = $"/Client/{m_ClientName}";
         await m_client.SetNodeAsync(path, "OnLine");
     }
 }
Пример #2
0
        public void OnStart(IEnumerable <ServerPort> serverPorts, IEnumerable <IGrpcServerService> servicesStarted)
        {
            // Register each service that started with etcd using the host IP setting
            var registerTasks = new List <Task>();

            foreach (IGrpcServerService service in servicesStarted)
            {
                var t = _client.SetNodeAsync(GetServiceKey(service), $"{_broadcastOptions.IP}:{_broadcastOptions.Port}");
                registerTasks.Add(t);
            }

            Task.WaitAll(registerTasks.ToArray());
        }
Пример #3
0
        private async Task <Task> PhoneHome(CancellationToken ct)
        {
            // Register
            try
            {
                await etcdClient.SetNodeAsync("/_etcd/registry/github", "{\"name\": \"GitHub\", \"type\": \"Issue Tracker\"}");

                Log.Information("ETCD Registration Complete!");
            }
            catch (EtcdGenericException ex)
            {
                Log.Error("Error registering service with ETCD registry: ", ex.Message);
            }

            // Heartbeat
            return(Task.Factory.StartNew(async() =>
            {
                const string KEY = "/heartbeat/github";
                const string VAL = "1";
                const int TTL = 10;

                while (!ct.IsCancellationRequested)
                {
                    Log.Information("Heartbeat ...");

                    try
                    {
                        await etcdClient.SetNodeAsync(KEY, VAL, ttl: TTL);
                    }
                    catch (EtcdGenericException ex)
                    {
                        Log.Error("Error sending heartbeat to ETCD registry: ", ex.Message);
                    }

                    await Task.Delay(TTL / 2 * 1000);
                }
            }, ct));
        }
Пример #4
0
        static async Task DoSample()
        {
            EtcdClientOpitions options = new EtcdClientOpitions()
            {
#if NET45 // TODO: This is optional, having HTTPS setup could make this work in .NET Core as well, but it's not tested for now
                Urls = new string[] { "https://etcd0.em", "https://etcd1.em", "https://etcd2.em" },
#else
                Urls = new string[] { "http://*****:*****@"client.p12"),  // client cerificate
                JsonDeserializer = new NewtonsoftJsonDeserializer(),
            };
            EtcdClient etcdClient = new EtcdClient(options);



            string key = "/my/key";
            string value;

            // query the value of the node using GetNodeValueAsync
            try {
                value = await etcdClient.GetNodeValueAsync(key);

                Console.WriteLine("The value of `{0}` is `{1}`", key, value);
            }
            catch (EtcdCommonException.KeyNotFound) {
                Console.WriteLine("Key `{0}` does not exist", key);
            }


            // update the value using SetNodeAsync
            EtcdResponse resp = await etcdClient.SetNodeAsync(key, "some value");

            Console.WriteLine("Key `{0}` is changed, modifiedIndex={1}", key, resp.Node.ModifiedIndex);

            // query the node using GetNodeAsync
            resp = await etcdClient.GetNodeAsync(key, ignoreKeyNotFoundException : true);

            if (resp == null || resp.Node == null)
            {
                Console.WriteLine("Key `{0}` does not exist", key);
            }
            else
            {
                Console.WriteLine("The value of `{0}` is `{1}`", key, resp.Node.Value);
            }

            //////////////////////////////////////////////////////////

            List <Task <EtcdResponse> > tasks = new List <Task <EtcdResponse> >();

            key = "/in-order-queue";

            // start monitoring the expire event
            WatchChanges(etcdClient, key);

            // create 5 in-order nodes, TTL = 3 second
            for (int i = 0; i < 5; i++)
            {
                tasks.Add(etcdClient.CreateInOrderNodeAsync(key, i.ToString(), ttl: 3));
            }

            await Task.WhenAll(tasks);

            // list the in-order nodes
            resp = await etcdClient.GetNodeAsync(key, false, recursive : true, sorted : true);

            if (resp.Node.Nodes != null)
            {
                foreach (var node in resp.Node.Nodes)
                {
                    Console.WriteLine("`{0}` = {1}", node.Key, node.Value);
                }
            }


            /////////////////////////////////////////////////////////////
            key   = "/my/cas-test";
            value = Guid.NewGuid().ToString();
            try {
                resp = await etcdClient.CreateNodeAsync(key, value, null, dir : false);

                Console.WriteLine("Key `{0}` is created with value {1}", key, value);
            }
            catch (EtcdCommonException.NodeExist) {
                Console.WriteLine("Key `{0}` already exists", key);
            }

            long prevIndex = 1;

            try {
                resp = await etcdClient.CompareAndSwapNodeAsync(key, value, "new value");

                Console.WriteLine("Key `{0}` is updated to `{1}`", key, resp.Node.Value);

                prevIndex = resp.Node.ModifiedIndex;
            }
            catch (EtcdCommonException.KeyNotFound) {
                Console.WriteLine("Key `{0}` does not exists", key);
            }
            catch (EtcdCommonException.TestFailed) {
                Console.WriteLine("Key `{0}` can not be updated because the supplied previous value is incorrect", key);
            }

            try {
                resp = await etcdClient.CompareAndSwapNodeAsync(key, prevIndex, "new value2");

                Console.WriteLine("Key `{0}` is updated to `{1}`", key, resp.Node.Value);
            }
            catch (EtcdCommonException.KeyNotFound) {
                Console.WriteLine("Key `{0}` does not exists", key);
            }
            catch (EtcdCommonException.TestFailed) {
                Console.WriteLine("Key `{0}` can not be updated because the supplied previous index is incorrect", key);
            }



            try {
                resp = await etcdClient.CompareAndDeleteNodeAsync(key, prevIndex + 1);

                Console.WriteLine("Key `{0}` is deleted", key);
            }
            catch (EtcdCommonException.KeyNotFound) {
                Console.WriteLine("Key `{0}` does not exists", key);
            }
            catch (EtcdCommonException.TestFailed) {
                Console.WriteLine("Key `{0}` can not be deleted because the supplied previous index is incorrect", key);
            }

            if (prevIndex == 1) // the previous CAS failed
            {
                try {
                    resp = await etcdClient.CompareAndDeleteNodeAsync(key, "new value2");

                    Console.WriteLine("Key `{0}` is deleted", key);
                }
                catch (EtcdCommonException.KeyNotFound) {
                    Console.WriteLine("Key `{0}` does not exists", key);
                }
                catch (EtcdCommonException.TestFailed) {
                    Console.WriteLine("Key `{0}` can not be deleted because the supplied previous value is incorrect", key);
                    etcdClient.DeleteNodeAsync(key, ignoreKeyNotFoundException: true).Wait();
                }
            }
        }
Пример #5
0
 public void Put(string id, T value)
 {
     etcdClient.SetNodeAsync(CreateKey(id), ToDataRecord(value)).Wait();
 }