Browse Source

Migrates from ribbon to spring cloud loadbalancer

pull/1543/head
Spencer Gibb 5 years ago
parent
commit
014528285c
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 22
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactoryIntegrationTests.java
  2. 4
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerTestConfig.java
  3. 25
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/ReadBodyPredicateFactoryTest.java
  4. 22
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java
  5. 17
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/websocket/WebSocketIntegrationTests.java
  6. 5
      spring-cloud-gateway-sample/pom.xml
  7. 18
      spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java

22
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.context.SpringBootTest;
import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.boot.test.system.OutputCaptureRule;
import org.springframework.boot.web.server.LocalServerPort; 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.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.RetryGatewayFilterFactory.RetryConfig; import org.springframework.cloud.gateway.filter.factory.RetryGatewayFilterFactory.RetryConfig;
import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.test.BaseWebClientTests; 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.Bean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -173,8 +177,8 @@ public class RetryGatewayFilterFactoryIntegrationTests extends BaseWebClientTest
@EnableAutoConfiguration @EnableAutoConfiguration
@SpringBootConfiguration @SpringBootConfiguration
@Import(DefaultTestConfig.class) @Import(DefaultTestConfig.class)
// @RibbonClient(name = "badservice2", configuration = @LoadBalancerClient(name = "badservice2",
// TestBadLoadBalancerConfig.class) configuration = TestBadLoadBalancerConfig.class)
public static class TestConfig { public static class TestConfig {
Log log = LogFactory.getLog(getClass()); Log log = LogFactory.getLog(getClass());
@ -271,12 +275,14 @@ public class RetryGatewayFilterFactoryIntegrationTests extends BaseWebClientTest
@LocalServerPort @LocalServerPort
protected int port = 0; protected int port = 0;
// @Bean @Bean
// public ServerList<Server> serverList() { public ServiceInstanceListSupplier staticServiceInstanceListSupplier(
// return new StaticServerList<>( Environment env) {
// new Server("https", "localhost.domain.doesnot.exist", this.port), return ServiceInstanceListSupplier.fixed(env)
// new Server("localhost", this.port)); .instance(new DefaultServiceInstance("doesnotexist1", "badservice2",
// } "localhost.domain.doesnot.exist", port, true))
.instance(port, "badservice2").build();
}
} }

4
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.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 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.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.test.BaseWebClientTests; 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.Bean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -47,7 +49,7 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.r
@SpringBootConfiguration @SpringBootConfiguration
@Import(BaseWebClientTests.DefaultTestConfig.class) @Import(BaseWebClientTests.DefaultTestConfig.class)
@RestController @RestController
// @RibbonClient(name = "badservice", configuration = TestBadLoadBalancerConfig.class) @LoadBalancerClient(name = "badservice", configuration = TestBadLoadBalancerConfig.class)
public class SpringCloudCircuitBreakerTestConfig { public class SpringCloudCircuitBreakerTestConfig {
@Value("${test.uri}") @Value("${test.uri}")

25
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.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest; 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.AsyncPredicate;
import org.springframework.cloud.gateway.handler.predicate.ReadBodyPredicateFactory.Config; import org.springframework.cloud.gateway.handler.predicate.ReadBodyPredicateFactory.Config;
import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; 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.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.Bean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
@ -85,10 +87,11 @@ public class ReadBodyPredicateFactoryTest {
@EnableAutoConfiguration @EnableAutoConfiguration
@SpringBootConfiguration @SpringBootConfiguration
// @RibbonClients({ @LoadBalancerClients({
// @RibbonClient(name = "message", configuration = TestLoadBalancerConfig.class), @LoadBalancerClient(name = "message",
// @RibbonClient(name = "messageChannel", configuration = TestLoadBalancerConfig.class),
// configuration = TestLoadBalancerConfig.class) }) @LoadBalancerClient(name = "messageChannel",
configuration = TestLoadBalancerConfig.class) })
@Import(PermitAllSecurityConfiguration.class) @Import(PermitAllSecurityConfiguration.class)
@RestController @RestController
public static class TestConfig { public static class TestConfig {
@ -124,18 +127,6 @@ public class ReadBodyPredicateFactoryTest {
} }
protected static class TestLoadBalancerConfig {
@LocalServerPort
protected int port = 0;
// @Bean
// public ServerList<Server> serverList() {
// return new StaticServerList<>(new Server("localhost", this.port));
// }
}
static class Event { static class Event {
private String foo; private String foo;

22
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.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.route.Route; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order; 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.ClientHttpConnector;
import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
@ -53,6 +56,8 @@ public class BaseWebClientTests {
protected static final Duration DURATION = Duration.ofSeconds(5); protected static final Duration DURATION = Duration.ofSeconds(5);
public static final String SERVICE_ID = "testservice";
@LocalServerPort @LocalServerPort
protected int port = 0; protected int port = 0;
@ -76,9 +81,8 @@ public class BaseWebClientTests {
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
// @RibbonClients({ @LoadBalancerClient(name = "testservice",
// @RibbonClient(name = "testservice", configuration = TestLoadBalancerConfig.class) configuration = TestLoadBalancerConfig.class)
// })
@Import(PermitAllSecurityConfiguration.class) @Import(PermitAllSecurityConfiguration.class)
public static class DefaultTestConfig { public static class DefaultTestConfig {
@ -137,15 +141,17 @@ public class BaseWebClientTests {
} }
protected static class TestLoadBalancerConfig { public static class TestLoadBalancerConfig {
@LocalServerPort @LocalServerPort
protected int port = 0; protected int port = 0;
// @Bean @Bean
// public ServerList<Server> serverList() { public ServiceInstanceListSupplier staticServiceInstanceListSupplier(
// return new StaticServerList<>(new Server("localhost", this.port)); Environment env) {
// } return ServiceInstanceListSupplier.fixed(env).instance(port, SERVICE_ID)
.build();
}
} }

17
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.PermitAllSecurityConfiguration;
import org.springframework.cloud.gateway.test.support.HttpServer; import org.springframework.cloud.gateway.test.support.HttpServer;
import org.springframework.cloud.gateway.test.support.ReactorHttpServer; 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.ConfigurableApplicationContext;
import org.springframework.context.Lifecycle; import org.springframework.context.Lifecycle;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 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.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -357,8 +360,8 @@ public class WebSocketIntegrationTests {
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration @EnableAutoConfiguration
@Import(PermitAllSecurityConfiguration.class) @Import(PermitAllSecurityConfiguration.class)
// @RibbonClient(name = "wsservice", @LoadBalancerClient(name = "wsservice",
// configuration = LocalLoadBalancerClientConfiguration.class) configuration = LocalLoadBalancerClientConfiguration.class)
protected static class GatewayConfig { protected static class GatewayConfig {
@Bean @Bean
@ -375,10 +378,12 @@ public class WebSocketIntegrationTests {
@Value("${ws.server.port}") @Value("${ws.server.port}")
private int wsPort; private int wsPort;
// @Bean @Bean
// public ServerList<Server> serverList() { public ServiceInstanceListSupplier staticServiceInstanceListSupplier(
// return new StaticServerList<>(new Server("localhost", this.wsPort)); Environment env) {
// } return ServiceInstanceListSupplier.fixed(env).instance(wsPort, "wsservice")
.build();
}
} }

5
spring-cloud-gateway-sample/pom.xml

@ -55,6 +55,11 @@
<artifactId>spring-cloud-test-support</artifactId> <artifactId>spring-cloud-test-support</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>io.projectreactor</groupId> <groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId> <artifactId>reactor-test</artifactId>

18
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.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.cloud.gateway.test.HttpBinCompatibleController; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.SocketUtils; import org.springframework.util.SocketUtils;
@ -46,8 +49,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
* @author Spencer Gibb * @author Spencer Gibb
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = { GatewaySampleApplicationTests.TestConfig.class }, @SpringBootTest(classes = {
webEnvironment = RANDOM_PORT, properties = "management.server.port=${test.port}") GatewaySampleApplicationTests.TestConfig.class }, webEnvironment = RANDOM_PORT, properties = "management.server.port=${test.port}")
public class GatewaySampleApplicationTests { public class GatewaySampleApplicationTests {
protected static int managementPort; protected static int managementPort;
@ -175,7 +178,7 @@ public class GatewaySampleApplicationTests {
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration @EnableAutoConfiguration
//@RibbonClient(name = "httpbin", configuration = LoadBalancerConfig.class) @LoadBalancerClient(name = "httpbin", configuration = LoadBalancerConfig.class)
@Import(GatewaySampleApplication.class) @Import(GatewaySampleApplication.class)
protected static class TestConfig { protected static class TestConfig {
@ -191,11 +194,10 @@ public class GatewaySampleApplicationTests {
@LocalServerPort @LocalServerPort
int port; int port;
//@Bean @Bean
//@Primary public ServiceInstanceListSupplier fixedServiceInstanceListSupplier(Environment env) {
//public ServerList<Server> serverList() { return ServiceInstanceListSupplier.fixed(env).instance("localhost", port, "httpbin").build();
// return new StaticServerList<>(new Server("localhost", port)); }
//}
} }

Loading…
Cancel
Save