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; @@ -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 @@ -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 @@ -271,12 +275,14 @@ public class RetryGatewayFilterFactoryIntegrationTests extends BaseWebClientTest
@LocalServerPort
protected int port = 0;
// @Bean
// public ServerList<Server> 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();
}
}

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; @@ -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 @@ -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}")

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; @@ -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 { @@ -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 { @@ -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 {
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; @@ -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 { @@ -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 { @@ -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 { @@ -137,15 +141,17 @@ public class BaseWebClientTests {
}
protected static class TestLoadBalancerConfig {
public static class TestLoadBalancerConfig {
@LocalServerPort
protected int port = 0;
// @Bean
// public ServerList<Server> serverList() {
// return new StaticServerList<>(new Server("localhost", this.port));
// }
@Bean
public ServiceInstanceListSupplier staticServiceInstanceListSupplier(
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; @@ -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; @@ -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 { @@ -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 { @@ -375,10 +378,12 @@ public class WebSocketIntegrationTests {
@Value("${ws.server.port}")
private int wsPort;
// @Bean
// public ServerList<Server> serverList() {
// return new StaticServerList<>(new Server("localhost", this.wsPort));
// }
@Bean
public ServiceInstanceListSupplier staticServiceInstanceListSupplier(
Environment env) {
return ServiceInstanceListSupplier.fixed(env).instance(wsPort, "wsservice")
.build();
}
}

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

@ -55,6 +55,11 @@ @@ -55,6 +55,11 @@
<artifactId>spring-cloud-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<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; @@ -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 @@ -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 { @@ -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 { @@ -191,11 +194,10 @@ public class GatewaySampleApplicationTests {
@LocalServerPort
int port;
//@Bean
//@Primary
//public ServerList<Server> serverList() {
// return new StaticServerList<>(new Server("localhost", port));
//}
@Bean
public ServiceInstanceListSupplier fixedServiceInstanceListSupplier(Environment env) {
return ServiceInstanceListSupplier.fixed(env).instance("localhost", port, "httpbin").build();
}
}

Loading…
Cancel
Save