示例#1
0
        private RemoteHostPool CreatePool(
            DeploymentIdentifier deploymentIdentifier,
            List <IDisposable> blockLinks,
            IServiceDiscoverySource discoverySource)
        {
            var result = _remoteHostPoolFactory.Create(deploymentIdentifier, discoverySource, _reachabilityChecker);

            var dispose = result.EndPointsChanged.LinkTo(new ActionBlock <EndPointsResult>(m =>
            {
                if (result == MasterEnvironmentPool || result == OriginatingEnvironmentPool)
                {
                    FireEndPointChange();
                }
            }));

            blockLinks.Add(dispose);

            dispose = result.ReachabilitySource.LinkTo(new ActionBlock <ServiceReachabilityStatus>(x =>
            {
                if (result == _activePool && !_suppressNotifications)
                {
                    _reachabilityChanged.Post(x);
                }
            }));
            blockLinks.Add(dispose);



            return(result);
        }
示例#2
0
        private async Task <IServiceDiscoverySource> GetDiscoverySource(DeploymentIdentifier deploymentIdentifier, ServiceDiscoveryConfig config)
        {
            var source = _serviceDiscoveryLoader.GetDiscoverySource(deploymentIdentifier, config);

            await source.Init().ConfigureAwait(false);

            return(source);
        }
示例#3
0
 public LocalDiscoverySource(DeploymentIdentifier deploymentIdentifier) : base($"{CurrentApplicationInfo.HostName}-{deploymentIdentifier.ServiceName}")
 {
     Result = new EndPointsResult {
         EndPoints = new[] { new EndPoint {
                                 HostName = CurrentApplicationInfo.HostName
                             } }
     };
 }
示例#4
0
 public ConfigDiscoverySource(DeploymentIdentifier deployment, Func <DiscoveryConfig> getConfig, ILog log) : base(deployment.ServiceName)
 {
     _serviceDiscoveryConfig = getConfig().Services[deployment.ServiceName];
     Log    = log;
     Result = new EndPointsResult {
         EndPoints = GetEndPointsInitialValue()
     };
 }
示例#5
0
        public static string GetDeploymentName(DeploymentIdentifier deploymentIdentifier, ServiceDiscoveryConfig serviceDiscoverySettings)
        {
            if (serviceDiscoverySettings.Scope == ServiceScope.Zone)
            {
                return(deploymentIdentifier.ServiceName);
            }

            return(deploymentIdentifier.GetConsulServiceName());
        }
示例#6
0
        public IServiceDiscoverySource GetDiscoverySource(DeploymentIdentifier deploymentIdentifier, ServiceDiscoveryConfig serviceDiscoveryConfig)
        {
            var source = _getSources(deploymentIdentifier).FirstOrDefault(f => f.SourceName.Equals(serviceDiscoveryConfig.Source, StringComparison.InvariantCultureIgnoreCase));

            if (source == null)
            {
                throw new ConfigurationException($"Discovery Source '{serviceDiscoveryConfig.Source}' is not supported.");
            }

            return(source);
        }
示例#7
0
        private async Task ReloadRemoteHost(DiscoveryConfig newConfig)
        {
            var newServiceConfig = newConfig.Services[_serviceName];

            lock (_locker)
            {
                if (newServiceConfig.Equals(LastServiceConfig) &&
                    newConfig.EnvironmentFallbackEnabled == LastConfig.EnvironmentFallbackEnabled &&
                    newConfig.EnvironmentFallbackTarget == LastConfig.EnvironmentFallbackTarget)
                {
                    return;
                }
            }

            var originatingSource = await GetDiscoverySource(_originatingDeployment, newServiceConfig).ConfigureAwait(false);

            var fallbackTarget   = newConfig.EnvironmentFallbackTarget ?? DefaultEnvironmentFallbackTarget;
            var masterDeployment = new DeploymentIdentifier(_serviceName, fallbackTarget, _environment);

            var shouldCreateMasterPool = newConfig.EnvironmentFallbackEnabled &&
                                         newServiceConfig.Scope == ServiceScope.Environment &&
                                         originatingSource.SupportsFallback &&
                                         _originatingDeployment.Equals(masterDeployment) == false;

            IServiceDiscoverySource masterSource = null;

            if (shouldCreateMasterPool)
            {
                masterSource = await GetDiscoverySource(masterDeployment, newServiceConfig).ConfigureAwait(false);
            }

            lock (_locker)
            {
                _suppressNotifications = true;

                LastConfig        = newConfig;
                LastServiceConfig = newServiceConfig;

                RemoveOriginatingPool();
                OriginatingEnvironmentPool = CreatePool(_originatingDeployment, _originatingEnvironmentLinks, originatingSource);

                RemoveMasterPool();

                if (masterSource != null)
                {
                    MasterEnvironmentPool = CreatePool(masterDeployment, _masterEnvironmentLinks, masterSource);
                }

                _suppressNotifications = false;

                GetRelevantPool();
            }
        }
示例#8
0
        public ConsulDiscoverySource(DeploymentIdentifier deploymentIdentifier,
                                     IDateTime dateTime,
                                     Func <DiscoveryConfig> getConfig,
                                     Func <string, IConsulClient> getConsulClient, ILog log)
            : base(GetDeploymentName(deploymentIdentifier, getConfig().Services[deploymentIdentifier.ServiceName]))

        {
            DateTime         = dateTime;
            _getConsulClient = getConsulClient;
            _log             = log;

            _firstResultInitialized = new TaskCompletionSource <bool>();
        }
示例#9
0
        public ServiceDiscovery(string serviceName,
                                ReachabilityChecker reachabilityChecker,
                                IRemoteHostPoolFactory remoteHostPoolFactory,
                                IDiscoverySourceLoader serviceDiscoveryLoader,
                                IEnvironment environment,
                                ISourceBlock <DiscoveryConfig> configListener,
                                Func <DiscoveryConfig> discoveryConfigFactory,
                                ILog log)
        {
            Log                    = log;
            _serviceName           = serviceName;
            _originatingDeployment = new DeploymentIdentifier(serviceName, environment.DeploymentEnvironment, environment);
            _masterDeployment      = new DeploymentIdentifier(serviceName, MASTER_ENVIRONMENT, environment);

            _reachabilityChecker    = reachabilityChecker;
            _remoteHostPoolFactory  = remoteHostPoolFactory;
            _serviceDiscoveryLoader = serviceDiscoveryLoader;

            // Must be run in Task.Run() because of incorrect Orleans scheduling
            _initTask        = Task.Run(() => ReloadRemoteHost(discoveryConfigFactory()));
            _configBlockLink = configListener.LinkTo(new ActionBlock <DiscoveryConfig>(ReloadRemoteHost));
        }