private string GetServiceUrl(ILoadBalancerRequestContext _requestContext) { if (_requestContext == null) { return(null); } var server = _requestContext.Servers; if (server == null || server.Length < 1) { throw new Exception("Got null or empty service url."); } if (!server.Any(r => r.ServiceName.Equals(this.ServiceFullName))) { Dictionary <string, string> addtionalInfo = GetClientInfo(); log.Error(ArtemisConstants.MetadataIsNullOrUrlNotExisted, server.ToString(), addtionalInfo); return(null); } if (server.Length == 1) { return(server.First().ServiceAddress); } return(server[ThreadLocalRandom.Current.Next(0, server.Length)].ServiceAddress); }
// serviceName和serviceNamespace这2个parameter已经过时,只作为placeholder存在,值没有意义 protected ServiceClientBase(string serviceName, string serviceNamespace, string subEnv) : this(ConnectionMode.Indirect) { string settingKey = GetServiceSettingKey(SERVICE_REGISTRY_SUBENV_KEY, ServiceName, ServiceNamespace); TestSubEnv = ConfigUtils.GetNullableAppSetting(settingKey); if (string.IsNullOrWhiteSpace(TestSubEnv)) { TestSubEnv = ServiceRegistryTestSubEnv; } else { TestSubEnv = TestSubEnv.Trim().ToLower(); } int count = -1; requestContext = DynamicRequestContextProvider.LoadSignalRRequestContext(ServiceFullName); while (count++ < initUrlRetryTimesProperty && (requestContext == null || requestContext.Servers == null || requestContext.Servers.Length < 1)) { log.Info("Service url is null or empty, will retry after 200 ms", GetClientInfo()); Thread.Sleep(200); } if (count >= initUrlRetryTimesProperty) { log.Error(string.Format("Service is null after retried {0} times", count), GetClientInfo()); } else { string serviceUrl = GetServiceUrl(requestContext); if (string.IsNullOrWhiteSpace(serviceUrl)) { log.Error("Got null or empty service url.", GetClientInfo()); throw new Exception("Got null or empty service url."); } else { _threadLocalBaseUri.Value = serviceUrl; string message; if (count > 0) { message = string.Format("Got service url {0} after retried {1} times", serviceUrl, count); } else { message = string.Format("Initialized client instance with indirect service url {0}", serviceUrl); } log.Info(message, GetClientInfo()); } } }
public void CanReplaceRouteTest() { int repeat = 2; int serverCount = 10; IConfiguration configuration = ObjectFactory.CreateAppSettingConfiguration(); var configurationSource = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration); var configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource); List <Server> servers = new List <Server>(); for (int i = 0; i < serverCount; i++) { var server = new Server("Server_" + i, null); servers.Add(server); } LoadBalancerRouteConfig routeConfig = new LoadBalancerRouteConfig("default", 1, false); LoadBalancerRequestConfig requestConfig = new LoadBalancerRequestConfig(new List <LoadBalancerRouteConfig>() { routeConfig }); var groups = new List <ServerGroup>() { new ServerGroup("default", 1, servers) }; var routes = new List <LoadBalancerRoute>() { new LoadBalancerRoute("default", groups) }; var newRoute = new LoadBalancerRoute("newRoutes", new List <ServerGroup>() { new ServerGroup("newGroup", 1, servers.Take(1)) }); EventHandler <SeekRouteEventArgs> seekRouteEventHandler = (o, e) => e.Route = newRoute; var serverSource = new DefaultDynamicServerSource(routes); var factory = LoadBalancerManager.GetManager("soa", new LoadBalancerManagerConfig(configurationManager)); var loadBalancerConfig = new LoadBalancerConfig(new TruePing(), serverSource) { SeekRouteEventHandler = seekRouteEventHandler }; var loadBalancer = factory.GetLoadBalancer(MethodInfo.GetCurrentMethod().Name, loadBalancerConfig); var loadBalancerContext = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext; Assert.AreEqual(serverCount, loadBalancerContext.ServerSourceFilter.LoadBalancerRoutes[0].Servers.Length); Assert.AreEqual(serverCount, loadBalancerContext.ServerSourceFilter.LoadBalancerRoutes[0].AvailableServers.Length); Dictionary <Server, int> serverCountMap = new Dictionary <Server, int>(); for (int i = 0; i < serverCount * repeat; i++) { ILoadBalancerRequestContext requestContext = loadBalancer.GetRequestContext(requestConfig); if (serverCountMap.ContainsKey(requestContext.Server)) { serverCountMap[requestContext.Server] += 1; } else { serverCountMap[requestContext.Server] = 1; } } Assert.AreEqual(1, serverCountMap.Count); foreach (int count in serverCountMap.Values) { Assert.AreEqual(serverCount * repeat, count); } }