Example #1
0
        /// <summary>
        /// 获取
        /// </summary>
        /// <returns></returns>
        public EtcdClient GetClient()
        {
            if (ClusterUseType == ClusterUse.Master_Slave)
            {
                try
                {
                    _client.Put("TestCon", "EtcdCluster");
                }
                catch
                {
                    _client.Dispose();
                    _client = null;
                    var c = GetEtcdClients(1);
                    if (c != null && c[0] != null)
                    {
                        _client.Dispose();
                        _client = c[0];
                    }
                }

                return(_client);
            }
            else
            {
                if (readerWriter.TryEnterReadLock(100))
                {
                    try
                    {
                        var c = dic.Values.Skip(_Lindex++ / dic.Count).First();
                        if (c != null)
                        {
                            c.lastUse = DateTime.Now.Ticks;
                            c.UseNum++;
                            return(c.Client);
                        }
                        return(null);
                    }
                    finally
                    {
                        readerWriter.ExitReadLock();
                    }
                }
                return(null);
            }
        }
Example #2
0
 public async Task CloseConnections()
 {
     foreach (var brokerSocket in _brokerSockets)
     {
         if (brokerSocket != null)
         {
             await brokerSocket.DeleteConnection();
         }
     }
     _client.Dispose();
 }
Example #3
0
 public void Dispose()
 {
     Interlocked.Exchange(ref isTaked, 1);
     if (watcher != null)
     {
         watcher.Dispose();
     }
     if (etcdClient != null)
     {
         etcdClient.Dispose();
     }
     GC.SuppressFinalize(true);
 }
        protected void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                _etcdClient.Dispose();
            }

            _disposed = true;
        }
Example #5
0
        public static async Task Main(string[] args)
        {
            var me = Guid.NewGuid().ToString();

            Console.WriteLine("Hello World!");

            var client = new EtcdClient("http://localhost");

            await WatchExample(client);

            //await LeaderElectionExample(client, me);

            client.Dispose();
        }
Example #6
0
        /// <summary>
        /// 下线退出
        /// </summary>
        /// <returns></returns>
        private static async Task ExitCommand()
        {
            if (m_client != null)
            {
                string path = $"Client/{m_ClientName}";

                await m_client.DeleteAsync(path);

                await m_client.PutAsync(path, "OffLine");

                await OutputOnlineState();

                m_client.Dispose();
                m_client = null;

                Console.WriteLine($"{m_ClientName}已经关闭");
            }
        }
Example #7
0
        public string EtcdSet(string url)
        {
            try
            {
                etcdUri = url;
                if (etcdClient != null)
                {
                    etcdClient.Dispose();
                }
                etcdClient = new EtcdClient(new Uri(etcdUri));

                return("ok");
            }
            catch (Exception ex)
            {
                return("error:" + ex.Message);
            }
        }
Example #8
0
 public void Dispose()
 {
     client.Dispose();
 }
Example #9
0
        static async Task Main(string[] args)
        {
            //Console.WriteLine("Hello World!");



            if (args != null && args.Length > 0)
            {
                m_ClientName = args[0];
            }

            Console.WriteLine($"当前运行{m_ClientName}");

            m_client = NewClient();

            try
            {
                if (m_client != null)
                {
                    Console.WriteLine($"{m_ClientName}连接成功");
                    //await CreateClientTree();
                    //await CreateMessageTree();


                    await ClientWatcher();
                    await UpdateOnlineState();

                    //await GroupMsgWatcher();
                    //await PrivateMsgWatcher();
                }



                //CreateClientTree();
                //CreateMessageTree();


                bool isExit = false;


                while (!isExit)
                {
                    var command = Console.ReadLine();

                    if (command == "exit")
                    {
                        isExit = true;
                    }
                    else
                    {
                        await ExcuteCommand(command);
                    }
                }

                await ExitCommand();


                if (m_client != null)
                {
                    m_client.Dispose();
                }
            }
            catch (RpcException ex) when(ex.StatusCode == StatusCode.Unavailable)
            {
                // Do something with the exception.
            }


            Console.ReadLine();
        }
Example #10
0
 /// <inheritdoc/>
 public void Dispose()
 {
     _etcdClient.Dispose();
 }
Example #11
0
        /// <summary>
        /// Initialise etcd client
        /// </summary>
        /// <param name="options"></param>
        private async Task <EtcdClient> CreateClient(EtcdConfigurationRepositoryOptions options)
        {
            EtcdClient etcdClient = null;

            var lookup = new LookupClient();

            if (options.UseEtcdClientDiscovery)
            {
                var dnsHostName = string.IsNullOrEmpty(options.DnsHostOverride)
                    ? $"_etcd-client${(options.UseTLS ? "-ssl" : "")}.${options.EtcdClientDiscoveryDomain}"
                    : options.DnsHostOverride;

                _logger.LogInformation("Attempting service discovery using hostname {dnsHostName}", dnsHostName);

                var dnsResponse = await lookup.QueryAsync(dnsHostName, QueryType.SRV);

                if (dnsResponse.Answers.Count > 0)
                {
                    foreach (var srvRecord in dnsResponse.Answers.SrvRecords())
                    {
                        _logger.LogInformation($"Connecting to etcd host {srvRecord.Target} using port {options.PortOverride ?? srvRecord.Port}.");
                        var tmpEtcdClient = new EtcdClient(srvRecord.Target, options.PortOverride ?? srvRecord.Port, options.Username, options.Password);
                        try
                        {
                            await tmpEtcdClient.StatusASync(new Etcdserverpb.StatusRequest());

                            etcdClient = tmpEtcdClient;
                            break;
                        }
                        catch (Exception ex)
                        {
                            _logger.LogWarning(ex, "Failed to connect to etcd cluster.");
                            tmpEtcdClient.Dispose();
                        }
                    }
                }

                if (etcdClient == null)
                {
                    if (!options.UseTLS) //Try again with TLS
                    {
                        _logger.LogInformation("Retrying discovery etcd cluster using TLS.");
                        options.UseTLS = true;
                        etcdClient     = await CreateClient(options);
                    }
                    else
                    {
                        throw new InvalidOperationException("Unable to connect to a etcd cluster because service discovery operations failed.");
                    }
                }
            }
            else if (!string.IsNullOrEmpty(options.DnsHostOverride))
            {
                _logger.LogInformation($"Connecting to etcd host {options.DnsHostOverride} using port {options.PortOverride ?? 2379}.");
                etcdClient = new EtcdClient(options.DnsHostOverride, options.PortOverride ?? 2379, options.Username, options.Password);
            }
            else
            {
                throw new ArgumentException("Must specify 'DnsHostOverride option' if 'UseEtcdDiscovery' option is set to false.");
            }

            return(etcdClient);
        }