public LoadBalancerContext(ILoadBalancer loadBalancer, string vipAddress)
 {
     ClinetName = "default";
     MaxAutoRetriesNextServer = 1;
     MaxAutoRetries           = 0;
     RetryHandler             = new DefaultRetryHandler();
     RetryOnAllOperations     = false;
     VipAddress   = vipAddress;
     LoadBalancer = loadBalancer;
 }
Exemple #2
0
        private LoadBalancerCommand GetCommand(LoadBalanceKey key)
        {
            if (_swiftyClientOptions.Eureka == null)
            {
                return(null);
            }
            if (_balancerCommands.TryGetValue(key, out LoadBalancerCommand x))
            {
                return(x);
            }
            lock (Syncs.GetOrAdd(key, k => new object()))
            {
                if (_balancerCommands.TryGetValue(key, out LoadBalancerCommand command))
                {
                    return(command);
                }

                var version    = key.Version;
                var vipAddress = key.VipAddress;
                var rule       = new FilterableRule(new VersionAffinityFilter(version));
                var eureka     = _swiftyClientOptions.Eureka;

                var balancer = LoadBalancerBuilder.NewBuilder()
                               .WithPing(_swiftyPing)
                               .WithPingStrategy(_swiftyClientOptions.PingStrategy)
                               .WithRule(rule)
                               .Build($"{vipAddress}:{version}", eureka.RegistryFetchIntervalSeconds * 1000 / 2);

                var collector    = new DefaultServerStatusCollector();
                var retryHandler = new DefaultRetryHandler(
                    _swiftyClientOptions.RetriesSameServer,
                    _swiftyClientOptions.RetriesNextServer,
                    _swiftyClientOptions.RetryEnabled,
                    CircuitTrippingException,
                    RetriableException);
                command = new LoadBalancerCommand(balancer, collector, retryHandler, null);

                var servers = DiscoveryManager.Instance.Client
                              .GetInstancesByVipAddress(vipAddress, _swiftyClientOptions.Secure)
                              .Where(ins => ins.Status == InstanceStatus.UP)
                              .Select(ins => new DiscoveryEnabledServer(ins, _swiftyClientOptions.Secure, _swiftyClientOptions.Eureka.AddressUsage) as Ribbon.Server)
                              .ToList();

                balancer.AddServers(servers);

                _balancerCommands.Add(key, command);
                return(command);
            }
        }