public ConsulServiceDiscovery(IOptions <ServiceDiscoveryConsulOptions> options, IServiceDiscoveryCache cache,
                               IServiceChangeTokenManager manager, ILogger <ConsulServiceDiscovery> logger)
 {
     _options = options.Value;
     _cache   = cache;
     _manager = manager;
     _logger  = logger;
 }
Exemple #2
0
        internal static Uri BuildConsulUri(ServiceDiscoveryConsulOptions options)
        {
            var builder = new UriBuilder
            {
                Host   = options.Address,
                Port   = options.Port,
                Scheme = options.Scheme
            };

            return(builder.Uri);
        }
Exemple #3
0
        private static IConsulClient NewConsulClient(ServiceDiscoveryConsulOptions options)
        {
            var client = new ConsulClient(c =>
            {
                c.Address    = BuildConsulUri(options);
                c.Datacenter = options.Datacenter;
                c.WaitTime   = options.WaitTime;
            });

            return(client);
        }
Exemple #4
0
        private static string CreateOptionUniqueString(ServiceDiscoveryConsulOptions options)
        {
            var uniqueString = string.Format("{0}-{1}-{2}-{3}", options.Scheme, options.Address, options.Port,
                                             options.Datacenter);

            if (options.WaitTime.HasValue)
            {
                uniqueString = string.Concat(uniqueString, $"-{options.WaitTime.Value.ToString()}");
            }

            return(uniqueString);
        }
Exemple #5
0
        public static IConsulClient CreateConsulClient(ServiceDiscoveryConsulOptions options)
        {
            IConsulClient client;
            bool          found;
            var           uniqueString = CreateOptionUniqueString(options);

            ConsulClientCacheLock.EnterReadLock();

            try
            {
                found = ConsulClientCache.TryGetValue(uniqueString, out client);
            }
            finally
            {
                ConsulClientCacheLock.ExitReadLock();
            }

            if (found)
            {
                return(client);
            }

            ConsulClientCacheLock.EnterWriteLock();
            try
            {
                found = ConsulClientCache.TryGetValue(uniqueString, out client);
                if (found)
                {
                    return(client);
                }

                client = NewConsulClient(options);
                ConsulClientCache[uniqueString] = client;
                return(client);
            }
            finally
            {
                ConsulClientCacheLock.ExitWriteLock();
            }
        }