internal static void AddCondenserRouter(this IServiceCollection self,
                                                string agentAddress, int agentPort,
                                                IHealthConfig health, IRoutingConfig routingConfig,
                                                IHttpClientConfig httpClientConfig)
        {
            var config = new CondenserConfiguration
            {
                AgentPort    = agentPort,
                AgentAddress = agentAddress
            };

            self.AddRouting();
            self.AddSingleton(health);
            self.AddSingleton(routingConfig);
            self.AddSingleton(httpClientConfig);

            self.AddSingleton <RoutingData>();
            self.AddSingleton(config);
            self.AddSingleton <IService, HealthRouter>();
            self.AddSingleton <IService, RouteSummary>();
            self.AddSingleton <IService, TreeRouter>();
            self.AddSingleton <IService, ChangeRoutingStrategy>();
            self.AddSingleton <IService, ServerStatsRoute>();
            self.AddTransient <IRoutingStrategy <IService>, RandomRoutingStrategy <IService> >();
            self.AddTransient <IRoutingStrategy <IService>, RoundRobinRoutingStrategy <IService> >();
            self.AddSingleton <IDefaultRouting <IService>,
                               DefaultRouting <IService> >();

            self.AddTransient <ChildContainer <IService> >();
            self.AddSingleton <CurrentState>();
            self.AddSingleton <CustomRouter>();
            self.AddSingleton <RoutingHost>();
            self.AddSingleton <RadixTree <IService> >();

            self.AddTransient <Service>();
            self.AddSingleton <Func <IConsulService> >(x => x.GetService <Service>);
            self.AddSingleton <Func <ChildContainer <IService> > >(x => x.GetService <ChildContainer <IService> >);
        }
示例#2
0
        public async Task CanRegisterRoutes()
        {
            var wait = new AsyncManualResetEvent <bool>();

            var agentAddress = "localhost";
            var config       = new CondenserConfiguration
            {
                AgentAddress = agentAddress,
                AgentPort    = 8500
            };
            var router = BuildRouter();

            var host = new RoutingHost(router, config, null, RoutingData.BuildDefault(),
                                       new IService[0], () => new Service(new CurrentState(), null))
            {
                OnRouteBuilt = servers =>
                {
                    {
                        if (servers.ContainsKey("Google"))
                        {
                            wait.Set(true);
                        }
                    }
                }
            };

            var google = new ServiceManager("Google",
                                            agentAddress, 8500)
            {
                ServiceAddress = "www.google.com",
                ServicePort    = 80
            };

            await google.AddApiUrl("/search")
            .RegisterServiceAsync();

            await wait.WaitAsync();

            var routeContext = await RouteRequest(router, "/search");

            Assert.Equal(200, routeContext.HttpContext.Response.StatusCode);

            google = new ServiceManager("Google", "Google2", agentAddress, 8500)
            {
                ServiceAddress = "www.google.com",
                ServicePort    = 80
            };

            wait.Reset();

            await google.AddApiUrl("/gmail")
            .AddHttpHealthCheck("/gmail", 10)
            .RegisterServiceAsync();

            await wait.WaitAsync();

            routeContext = await RouteRequest(router, "/gmail");

            Assert.Equal(200, routeContext.HttpContext.Response.StatusCode);

            routeContext = await RouteRequest(router, "/search");

            Assert.Null(routeContext);
        }