public void should_fix_issue_194()
        {
            var consulPort = 8503;
            var downstreamServiceOneUrl       = "http://localhost:8362";
            var downstreamServiceTwoUrl       = "http://localhost:8330";
            var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/api/user/{user}",
                        DownstreamScheme       = "http",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 8362,
                            }
                        },
                        UpstreamPathTemplate = "/api/user/{user}",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                    },
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/api/product/{product}",
                        DownstreamScheme       = "http",
                        DownstreamHostAndPorts = new List <FileHostAndPort>
                        {
                            new FileHostAndPort
                            {
                                Host = "localhost",
                                Port = 8330,
                            }
                        },
                        UpstreamPathTemplate = "/api/product/{product}",
                        UpstreamHttpMethod   = new List <string> {
                            "Get"
                        },
                    }
                },
                GlobalConfiguration = new FileGlobalConfiguration()
                {
                    ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
                    {
                        Host = "localhost",
                        Port = consulPort
                    }
                }
            };

            this.Given(x => x.GivenProductServiceOneIsRunning(downstreamServiceOneUrl, "/api/user/info", 200, "user"))
            .And(x => x.GivenProductServiceTwoIsRunning(downstreamServiceTwoUrl, "/api/product/info", 200, "product"))
            .And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/api/user/info?id=1"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("user"))
            .When(x => _steps.WhenIGetUrlOnTheApiGateway("/api/product/info?id=1"))
            .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
            .And(x => _steps.ThenTheResponseBodyShouldBe("product"))
            .BDDfy();
        }
示例#2
0
        public void should_proxy_websocket_input_to_downstream_service_and_use_service_discovery_and_load_balancer()
        {
            var downstreamPort = 5007;
            var downstreamHost = "localhost";

            var secondDownstreamPort = 5008;
            var secondDownstreamHost = "localhost";

            var serviceName = "websockets";
            var consulPort  = 8509;
            var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
            var serviceEntryOne = new ServiceEntry()
            {
                Service = new AgentService()
                {
                    Service = serviceName,
                    Address = downstreamHost,
                    Port    = downstreamPort,
                    ID      = Guid.NewGuid().ToString(),
                    Tags    = new string[0]
                },
            };
            var serviceEntryTwo = new ServiceEntry()
            {
                Service = new AgentService()
                {
                    Service = serviceName,
                    Address = secondDownstreamHost,
                    Port    = secondDownstreamPort,
                    ID      = Guid.NewGuid().ToString(),
                    Tags    = new string[0]
                },
            };

            var config = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        UpstreamPathTemplate   = "/",
                        DownstreamPathTemplate = "/ws",
                        DownstreamScheme       = "ws",
                        LoadBalancerOptions    = new FileLoadBalancerOptions {
                            Type = "RoundRobin"
                        },
                        ServiceName = serviceName,
                    }
                },
                GlobalConfiguration = new FileGlobalConfiguration
                {
                    ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
                    {
                        Host = "localhost",
                        Port = consulPort,
                        Type = "consul"
                    }
                }
            };

            this.Given(_ => _steps.GivenThereIsAConfiguration(config))
            .And(_ => _steps.StartFakeOcelotWithWebSockets())
            .And(_ => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl, serviceName))
            .And(_ => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
            .And(_ => StartFakeDownstreamService($"http://{downstreamHost}:{downstreamPort}", "/ws"))
            .And(_ => StartSecondFakeDownstreamService($"http://{secondDownstreamHost}:{secondDownstreamPort}", "/ws"))
            .When(_ => WhenIStartTheClients())
            .Then(_ => ThenBothDownstreamServicesAreCalled())
            .BDDfy();
        }
示例#3
0
        public void should_use_consul_service_discovery_and_load_balance_request()
        {
            var consulPort                    = 8502;
            var serviceName                   = "product";
            var downstreamServiceOneUrl       = "http://localhost:50881";
            var downstreamServiceTwoUrl       = "http://localhost:50882";
            var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
            var serviceEntryOne               = new ServiceEntry()
            {
                Service = new AgentService()
                {
                    Service = serviceName,
                    Address = "localhost",
                    Port    = 50881,
                    ID      = Guid.NewGuid().ToString(),
                    Tags    = new string[0]
                },
            };
            var serviceEntryTwo = new ServiceEntry()
            {
                Service = new AgentService()
                {
                    Service = serviceName,
                    Address = "localhost",
                    Port    = 50882,
                    ID      = Guid.NewGuid().ToString(),
                    Tags    = new string[0]
                },
            };

            var configuration = new FileConfiguration
            {
                ReRoutes = new List <FileReRoute>
                {
                    new FileReRoute
                    {
                        DownstreamPathTemplate = "/",
                        DownstreamScheme       = "http",
                        UpstreamPathTemplate   = "/",
                        UpstreamHttpMethod     = new List <string> {
                            "Get"
                        },
                        ServiceName         = serviceName,
                        LoadBalancerOptions = new FileLoadBalancerOptions {
                            Type = "LeastConnection"
                        },
                    }
                },
                GlobalConfiguration = new FileGlobalConfiguration()
                {
                    ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
                    {
                        Host = "localhost",
                        Port = consulPort
                    }
                }
            };

            this.Given(x => x.GivenProductServiceOneIsRunning(downstreamServiceOneUrl, 200))
            .And(x => x.GivenProductServiceTwoIsRunning(downstreamServiceTwoUrl, 200))
            .And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl, serviceName))
            .And(x => x.GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
            .And(x => _steps.GivenThereIsAConfiguration(configuration))
            .And(x => _steps.GivenOcelotIsRunning())
            .When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimes("/", 50))
            .Then(x => x.ThenTheTwoServicesShouldHaveBeenCalledTimes(50))
            .And(x => x.ThenBothServicesCalledRealisticAmountOfTimes(24, 26))
            .BDDfy();
        }