Пример #1
0
        private void CreateMasterEnvironmentLoadBalancer()
        {
            if (_masterDeployment.Equals(_originatingEnvironmentDeployment))
            {
                return;
            }

            MasterEnvironmentLoadBalancer = _discovery.CreateLoadBalancer(_masterDeployment, _reachabilityCheck, TrafficRoutingStrategy.RandomByRequestID);
        }
        public async Task Setup()
        {
            IDiscovery discovery = Substitute.For <IDiscovery>();

            _discoveryConfig = new DiscoveryConfig {
                Services = new ServiceDiscoveryCollection(new Dictionary <string, ServiceDiscoveryConfig>(), new ServiceDiscoveryConfig(), new PortAllocationConfig())
            };

            Dictionary <string, string> configDic = new Dictionary <string, string>();

            _currentEnvironment = Prod;
            var environment = Substitute.For <IEnvironment>();

            environment.DeploymentEnvironment.Returns(_ => _currentEnvironment);
            _unitTestingKernel = new TestingKernel <ConsoleLog>(mockConfig: configDic);
            _unitTestingKernel.Rebind <IEnvironment>().ToConstant(environment);
            _unitTestingKernel.Rebind <IDiscovery>().ToConstant(discovery);
            _unitTestingKernel.Rebind <Func <DiscoveryConfig> >().ToMethod(_ => () => _discoveryConfig);

            _loadBalancerByEnvironment = new Dictionary <string, ILoadBalancer>();
            _loadBalancerByEnvironment.Add(Prod, new MasterLoadBalancer());
            _loadBalancerByEnvironment.Add(Staging, new StagingLoadBalancer());
            _loadBalancerByEnvironment.Add(Canary, new PreferredEnvironmentLoadBalancer());
            discovery.CreateLoadBalancer(Arg.Any <DeploymentIdentifier>(), Arg.Any <ReachabilityCheck>(), TrafficRoutingStrategy.RandomByRequestID)
            .ReturnsForAnyArgs(c => _loadBalancerByEnvironment[c.Arg <DeploymentIdentifier>().DeploymentEnvironment]);
            _serviceDiscovery = _unitTestingKernel.Get <Func <string, ReachabilityCheck, IMultiEnvironmentServiceDiscovery> >()(ServiceName, (x, y) => new Task(null));

            TracingContext.SetPreferredEnvironment(null);
        }
Пример #3
0
        private async Task <NodeAndLoadBalancer> GetNodeAndLoadBalancer(string environment, string preferredEnvironment)
        {
            var loadBalancer = _loadBalancers.GetOrAdd(environment, p => {
                var deploymentId = new DeploymentIdentifier(ServiceName, environment, Environment.Zone);
                return(Discovery.CreateLoadBalancer(deploymentId, ReachabilityCheck, TrafficRoutingStrategy.RandomByRequestID));
            });

            Node node = await loadBalancer.TryGetNode().ConfigureAwait(false);

            if (node == null)
            {
                return(null);
            }

            return(new NodeAndLoadBalancer {
                Node = node, LoadBalancer = loadBalancer, PreferredEnvironment = preferredEnvironment
            });
        }
Пример #4
0
 private ILoadBalancer CreateLoadBalancer()
 {
     return(_discovery.CreateLoadBalancer(_deploymentIdentifier, null, TrafficRoutingStrategy.RandomByRequestID));
 }