示例#1
0
        public async void CommandTest()
        {
            var balancer = LoadBalancerBuilder.NewBuilder()
                           .WithPing(Ping)
                           .WithRule(new Thrifty.MicroServices.Ribbon.Rules.RoundRobinRule()).Build("demo", 1 * 1000);

            balancer.AddServers(Servers);
            var command = new LoadBalancerCommand(balancer, null, null, null);
            var x       = await command.Submit(s => Task.FromResult(Ping.IsAlive(s)));

            Assert.True(x);
        }
示例#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);
            }
        }
示例#3
0
        public void Test()
        {
            //使用ribbon规则和基于http的ping的一个负载
            var balancer = LoadBalancerBuilder.NewBuilder()
                           .WithPing(Ping)
                           .WithLoggerFactory(LoggerFactory)
                           .WithRule(new Thrifty.MicroServices.Ribbon.Rules.RoundRobinRule()).Build("demo", 1 * 1000);

            //添加需要被负载的服务器
            balancer.AddServers(Servers);
            //按照负载规则选择一台服务器
            var server = balancer.Choose();

            Assert.NotNull(server);
            Assert.True(server.Port % 2 == 0);
        }