From 014528285c8824ae09c40569646e8ab8e7cfe32b Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 21 Jan 2020 23:40:05 -0500 Subject: [PATCH] Migrates from ribbon to spring cloud loadbalancer --- ...yGatewayFilterFactoryIntegrationTests.java | 22 ++++++++++------ .../SpringCloudCircuitBreakerTestConfig.java | 4 ++- .../ReadBodyPredicateFactoryTest.java | 25 ++++++------------- .../gateway/test/BaseWebClientTests.java | 22 ++++++++++------ .../websocket/WebSocketIntegrationTests.java | 17 ++++++++----- spring-cloud-gateway-sample/pom.xml | 5 ++++ .../sample/GatewaySampleApplicationTests.java | 18 +++++++------ 7 files changed, 65 insertions(+), 48 deletions(-) diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactoryIntegrationTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactoryIntegrationTests.java index 1744e9ca3..c699f1bfd 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactoryIntegrationTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactoryIntegrationTests.java @@ -36,13 +36,17 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.factory.RetryGatewayFilterFactory.RetryConfig; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.cloud.gateway.test.BaseWebClientTests; +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; +import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; +import org.springframework.core.env.Environment; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; @@ -173,8 +177,8 @@ public class RetryGatewayFilterFactoryIntegrationTests extends BaseWebClientTest @EnableAutoConfiguration @SpringBootConfiguration @Import(DefaultTestConfig.class) - // @RibbonClient(name = "badservice2", configuration = - // TestBadLoadBalancerConfig.class) + @LoadBalancerClient(name = "badservice2", + configuration = TestBadLoadBalancerConfig.class) public static class TestConfig { Log log = LogFactory.getLog(getClass()); @@ -271,12 +275,14 @@ public class RetryGatewayFilterFactoryIntegrationTests extends BaseWebClientTest @LocalServerPort protected int port = 0; - // @Bean - // public ServerList serverList() { - // return new StaticServerList<>( - // new Server("https", "localhost.domain.doesnot.exist", this.port), - // new Server("localhost", this.port)); - // } + @Bean + public ServiceInstanceListSupplier staticServiceInstanceListSupplier( + Environment env) { + return ServiceInstanceListSupplier.fixed(env) + .instance(new DefaultServiceInstance("doesnotexist1", "badservice2", + "localhost.domain.doesnot.exist", port, true)) + .instance(port, "badservice2").build(); + } } diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerTestConfig.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerTestConfig.java index 8f480634b..4d6e41cfc 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerTestConfig.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerTestConfig.java @@ -24,9 +24,11 @@ import reactor.core.publisher.Mono; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.gateway.filter.factory.RetryGatewayFilterFactoryIntegrationTests.TestBadLoadBalancerConfig; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.cloud.gateway.test.BaseWebClientTests; +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.web.bind.annotation.RequestMapping; @@ -47,7 +49,7 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.r @SpringBootConfiguration @Import(BaseWebClientTests.DefaultTestConfig.class) @RestController -// @RibbonClient(name = "badservice", configuration = TestBadLoadBalancerConfig.class) +@LoadBalancerClient(name = "badservice", configuration = TestBadLoadBalancerConfig.class) public class SpringCloudCircuitBreakerTestConfig { @Value("${test.uri}") diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/ReadBodyPredicateFactoryTest.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/ReadBodyPredicateFactoryTest.java index dc2a57dd8..01fd5662b 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/ReadBodyPredicateFactoryTest.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/ReadBodyPredicateFactoryTest.java @@ -25,12 +25,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.web.server.LocalServerPort; import org.springframework.cloud.gateway.handler.AsyncPredicate; import org.springframework.cloud.gateway.handler.predicate.ReadBodyPredicateFactory.Config; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; +import org.springframework.cloud.gateway.test.BaseWebClientTests.TestLoadBalancerConfig; import org.springframework.cloud.gateway.test.PermitAllSecurityConfiguration; +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.http.HttpMethod; @@ -85,10 +87,11 @@ public class ReadBodyPredicateFactoryTest { @EnableAutoConfiguration @SpringBootConfiguration - // @RibbonClients({ - // @RibbonClient(name = "message", configuration = TestLoadBalancerConfig.class), - // @RibbonClient(name = "messageChannel", - // configuration = TestLoadBalancerConfig.class) }) + @LoadBalancerClients({ + @LoadBalancerClient(name = "message", + configuration = TestLoadBalancerConfig.class), + @LoadBalancerClient(name = "messageChannel", + configuration = TestLoadBalancerConfig.class) }) @Import(PermitAllSecurityConfiguration.class) @RestController public static class TestConfig { @@ -124,18 +127,6 @@ public class ReadBodyPredicateFactoryTest { } - protected static class TestLoadBalancerConfig { - - @LocalServerPort - protected int port = 0; - - // @Bean - // public ServerList serverList() { - // return new StaticServerList<>(new Server("localhost", this.port)); - // } - - } - static class Event { private String foo; diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java index db1f77a1e..667432be0 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java @@ -29,10 +29,13 @@ import org.springframework.boot.web.server.LocalServerPort; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.route.Route; +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; +import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.annotation.Order; +import org.springframework.core.env.Environment; import org.springframework.http.client.reactive.ClientHttpConnector; import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.test.web.reactive.server.WebTestClient; @@ -53,6 +56,8 @@ public class BaseWebClientTests { protected static final Duration DURATION = Duration.ofSeconds(5); + public static final String SERVICE_ID = "testservice"; + @LocalServerPort protected int port = 0; @@ -76,9 +81,8 @@ public class BaseWebClientTests { } @Configuration(proxyBeanMethods = false) - // @RibbonClients({ - // @RibbonClient(name = "testservice", configuration = TestLoadBalancerConfig.class) - // }) + @LoadBalancerClient(name = "testservice", + configuration = TestLoadBalancerConfig.class) @Import(PermitAllSecurityConfiguration.class) public static class DefaultTestConfig { @@ -137,15 +141,17 @@ public class BaseWebClientTests { } - protected static class TestLoadBalancerConfig { + public static class TestLoadBalancerConfig { @LocalServerPort protected int port = 0; - // @Bean - // public ServerList serverList() { - // return new StaticServerList<>(new Server("localhost", this.port)); - // } + @Bean + public ServiceInstanceListSupplier staticServiceInstanceListSupplier( + Environment env) { + return ServiceInstanceListSupplier.fixed(env).instance(port, SERVICE_ID) + .build(); + } } diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/websocket/WebSocketIntegrationTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/websocket/WebSocketIntegrationTests.java index 4cd3eef75..9b2f6ce0b 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/websocket/WebSocketIntegrationTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/websocket/WebSocketIntegrationTests.java @@ -45,6 +45,8 @@ import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.cloud.gateway.test.PermitAllSecurityConfiguration; import org.springframework.cloud.gateway.test.support.HttpServer; import org.springframework.cloud.gateway.test.support.ReactorHttpServer; +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; +import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.Lifecycle; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -52,6 +54,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; import org.springframework.http.HttpHeaders; import org.springframework.http.server.reactive.HttpHandler; import org.springframework.util.StringUtils; @@ -357,8 +360,8 @@ public class WebSocketIntegrationTests { @Configuration(proxyBeanMethods = false) @EnableAutoConfiguration @Import(PermitAllSecurityConfiguration.class) - // @RibbonClient(name = "wsservice", - // configuration = LocalLoadBalancerClientConfiguration.class) + @LoadBalancerClient(name = "wsservice", + configuration = LocalLoadBalancerClientConfiguration.class) protected static class GatewayConfig { @Bean @@ -375,10 +378,12 @@ public class WebSocketIntegrationTests { @Value("${ws.server.port}") private int wsPort; - // @Bean - // public ServerList serverList() { - // return new StaticServerList<>(new Server("localhost", this.wsPort)); - // } + @Bean + public ServiceInstanceListSupplier staticServiceInstanceListSupplier( + Environment env) { + return ServiceInstanceListSupplier.fixed(env).instance(wsPort, "wsservice") + .build(); + } } diff --git a/spring-cloud-gateway-sample/pom.xml b/spring-cloud-gateway-sample/pom.xml index a28697cff..f80a6e763 100644 --- a/spring-cloud-gateway-sample/pom.xml +++ b/spring-cloud-gateway-sample/pom.xml @@ -55,6 +55,11 @@ spring-cloud-test-support test + + org.springframework.cloud + spring-cloud-loadbalancer + test + io.projectreactor reactor-test diff --git a/spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java b/spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java index 04983ceff..90d43791b 100644 --- a/spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java +++ b/spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java @@ -32,9 +32,12 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.cloud.gateway.test.HttpBinCompatibleController; +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; +import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.core.env.Environment; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.util.SocketUtils; @@ -46,8 +49,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen * @author Spencer Gibb */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = { GatewaySampleApplicationTests.TestConfig.class }, - webEnvironment = RANDOM_PORT, properties = "management.server.port=${test.port}") +@SpringBootTest(classes = { + GatewaySampleApplicationTests.TestConfig.class }, webEnvironment = RANDOM_PORT, properties = "management.server.port=${test.port}") public class GatewaySampleApplicationTests { protected static int managementPort; @@ -175,7 +178,7 @@ public class GatewaySampleApplicationTests { @Configuration(proxyBeanMethods = false) @EnableAutoConfiguration - //@RibbonClient(name = "httpbin", configuration = LoadBalancerConfig.class) + @LoadBalancerClient(name = "httpbin", configuration = LoadBalancerConfig.class) @Import(GatewaySampleApplication.class) protected static class TestConfig { @@ -191,11 +194,10 @@ public class GatewaySampleApplicationTests { @LocalServerPort int port; - //@Bean - //@Primary - //public ServerList serverList() { - // return new StaticServerList<>(new Server("localhost", port)); - //} + @Bean + public ServiceInstanceListSupplier fixedServiceInstanceListSupplier(Environment env) { + return ServiceInstanceListSupplier.fixed(env).instance("localhost", port, "httpbin").build(); + } }