Skip to content

Commit

Permalink
Support blocking discovery client when reactive is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
TYsewyn authored Oct 18, 2019
1 parent 3f0364d commit 6560a9c
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientAutoConfiguration;
import org.springframework.cloud.gateway.config.GatewayAutoConfiguration;
Expand All @@ -49,7 +49,7 @@
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
@AutoConfigureBefore(GatewayAutoConfiguration.class)
@AutoConfigureAfter(CompositeDiscoveryClientAutoConfiguration.class)
@ConditionalOnClass({ DispatcherHandler.class, ReactiveDiscoveryClient.class })
@ConditionalOnClass({ DispatcherHandler.class })
@EnableConfigurationProperties
public class GatewayDiscoveryClientAutoConfiguration {

Expand Down Expand Up @@ -80,15 +80,6 @@ public static List<FilterDefinition> initFilters() {
return definitions;
}

@Bean
@ConditionalOnBean(ReactiveDiscoveryClient.class)
@ConditionalOnProperty(name = "spring.cloud.gateway.discovery.locator.enabled")
public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(
ReactiveDiscoveryClient discoveryClient,
DiscoveryLocatorProperties properties) {
return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
}

@Bean
public DiscoveryLocatorProperties discoveryLocatorProperties() {
DiscoveryLocatorProperties properties = new DiscoveryLocatorProperties();
Expand All @@ -97,4 +88,37 @@ public DiscoveryLocatorProperties discoveryLocatorProperties() {
return properties;
}

@Configuration
@ConditionalOnProperty(value = "spring.cloud.discovery.reactive.enabled",
matchIfMissing = true)
public static class ReactiveDiscoveryClientRouteDefinitionLocatorConfiguration {

@Bean
@ConditionalOnProperty(name = "spring.cloud.gateway.discovery.locator.enabled")
public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(
ReactiveDiscoveryClient discoveryClient,
DiscoveryLocatorProperties properties) {
return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
}

}

/**
* @deprecated In favor of the native reactive service discovery capability.
*/
@Configuration
@Deprecated
@ConditionalOnProperty(value = "spring.cloud.discovery.reactive.enabled",
havingValue = "false")
public static class BlockingDiscoveryClientRouteDefinitionLocatorConfiguration {

@Bean
@ConditionalOnProperty(name = "spring.cloud.gateway.discovery.locator.enabled")
public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(
DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) {
return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@

package org.springframework.cloud.gateway.discovery;

import java.util.List;

import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;

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.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(Enclosed.class)
public class GatewayDiscoveryClientAutoConfigurationTests {
public class BlockingGatewayDiscoveryClientAutoConfigurationTests {

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Config.class,
properties = { "spring.cloud.gateway.discovery.locator.enabled=true",
"spring.cloud.gateway.loadbalancer.use404=true" })
@SpringBootTest(classes = Config.class, properties = {
"spring.cloud.gateway.discovery.locator.enabled=true",
"spring.cloud.discovery.reactive.enabled=false",
"spring.cloud.gateway.loadbalancer.use404=true",
"spring.cloud.discovery.client.simple.instances.service[0].uri=https://service1:443" })
public static class EnabledByProperty {

@Autowired(required = false)
Expand All @@ -53,6 +53,9 @@ public static class EnabledByProperty {
public void routeLocatorBeanExists() {
assertThat(locator).as("DiscoveryClientRouteDefinitionLocator was null")
.isNotNull();
List<RouteDefinition> definitions = locator.getRouteDefinitions()
.collectList().block();
assertThat(definitions).hasSize(1);
}

@Test
Expand Down Expand Up @@ -81,13 +84,6 @@ public void routeLocatorBeanMissing() {
@EnableAutoConfiguration
protected static class Config {

@Bean
ReactiveDiscoveryClient discoveryClient() {
ReactiveDiscoveryClient discoveryClient = mock(ReactiveDiscoveryClient.class);
when(discoveryClient.getServices()).thenReturn(Flux.empty());
return discoveryClient;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gateway.discovery;

import java.util.List;

import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;

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.cloud.gateway.config.LoadBalancerProperties;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(Enclosed.class)
public class ReactiveGatewayDiscoveryClientAutoConfigurationTests {

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Config.class, properties = {
"spring.cloud.gateway.discovery.locator.enabled=true",
"spring.cloud.gateway.loadbalancer.use404=true",
"spring.cloud.discovery.client.simple.instances.service[0].uri=https://service1:443" })
public static class EnabledByProperty {

@Autowired(required = false)
private DiscoveryClientRouteDefinitionLocator locator;

@Autowired(required = false)
private LoadBalancerProperties properties;

@Test
public void routeLocatorBeanExists() {
assertThat(locator).as("DiscoveryClientRouteDefinitionLocator was null")
.isNotNull();
List<RouteDefinition> definitions = locator.getRouteDefinitions()
.collectList().block();
assertThat(definitions).hasSize(1);
}

@Test
public void use404() {
assertThat(properties.isUse404()).isTrue();
}

}

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Config.class)
public static class DisabledByDefault {

@Autowired(required = false)
private DiscoveryClientRouteDefinitionLocator locator;

@Test
public void routeLocatorBeanMissing() {
assertThat(locator).as("DiscoveryClientRouteDefinitionLocator exists")
.isNull();
}

}

@SpringBootConfiguration
@EnableAutoConfiguration
protected static class Config {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@
org.springframework.cloud.gateway.actuate.GatewayControllerEndpointTests.class,
org.springframework.cloud.gateway.config.GatewayAutoConfigurationTests.class,
org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocatorTests.class,
org.springframework.cloud.gateway.discovery.GatewayDiscoveryClientAutoConfigurationTests.class,
org.springframework.cloud.gateway.discovery.BlockingGatewayDiscoveryClientAutoConfigurationTests.class,
org.springframework.cloud.gateway.discovery.ReactiveGatewayDiscoveryClientAutoConfigurationTests.class,
org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocatorIntegrationTests.class,
org.springframework.cloud.gateway.support.ShortcutConfigurableTests.class,
org.springframework.cloud.gateway.support.ipresolver.XForwardedRemoteAddressResolverTest.class,
Expand Down

0 comments on commit 6560a9c

Please sign in to comment.