From ec0f827e2b4f4804d4d3c696550f17967a97a5d5 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 29 Nov 2017 11:44:29 -0500 Subject: [PATCH] Activate DiscoveryClient integration with a property fixes gh-123 --- .../main/asciidoc/spring-cloud-gateway.adoc | 6 ++ ...tewayDiscoveryClientAutoConfiguration.java | 46 +++++++++++ .../main/resources/META-INF/spring.factories | 3 +- ...DiscoveryClientAutoConfigurationTests.java | 76 +++++++++++++++++++ 4 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/GatewayDiscoveryClientAutoConfiguration.java create mode 100644 spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/discovery/GatewayDiscoveryClientAutoConfigurationTests.java diff --git a/docs/src/main/asciidoc/spring-cloud-gateway.adoc b/docs/src/main/asciidoc/spring-cloud-gateway.adoc index 41806a52..5895db46 100644 --- a/docs/src/main/asciidoc/spring-cloud-gateway.adoc +++ b/docs/src/main/asciidoc/spring-cloud-gateway.adoc @@ -681,6 +681,12 @@ public RouteLocator customRouteLocator(ThrottleGatewayFilterFactory throttle) { This style also allows for more custom predicate assertions. The predicates defined by `RouteDefinitionLocator` beans are combined using logical `and`. By using the fluent Java API, you can use the `and()`, `or()` and `negate()` operators on the `Predicate` class. +=== DiscoveryClient Route Definition Locator + +The Gateway can be configured to create routes based on services registered with a `DiscoveryClient` compatible service registry. + +To enable this, set `spring.cloud.gateway.discovery.locator.enabled=true` and make sure a `DiscoveryClient` implementation is on the classpath and enabled (such as Netflix Eureka, Consul or Zookeeper). + == Actuator API TODO: document the `/gateway` actuator endpoint diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/GatewayDiscoveryClientAutoConfiguration.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/GatewayDiscoveryClientAutoConfiguration.java new file mode 100644 index 00000000..c0d9d160 --- /dev/null +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/GatewayDiscoveryClientAutoConfiguration.java @@ -0,0 +1,46 @@ +/* + * Copyright 2013-2017 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 + * + * http://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 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.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.gateway.config.GatewayAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.DispatcherHandler; + +/** + * @author Spencer Gibb + */ +@Configuration +@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true) +@AutoConfigureBefore(GatewayAutoConfiguration.class) +@ConditionalOnClass({DispatcherHandler.class, DiscoveryClient.class}) +public class GatewayDiscoveryClientAutoConfiguration { + + @Bean + @ConditionalOnBean(DiscoveryClient.class) + @ConditionalOnProperty(name = "spring.cloud.gateway.discovery.locator.enabled") + public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient) { + return new DiscoveryClientRouteDefinitionLocator(discoveryClient); + } + +} + diff --git a/spring-cloud-gateway-core/src/main/resources/META-INF/spring.factories b/spring-cloud-gateway-core/src/main/resources/META-INF/spring.factories index f5386f12..f4d8e781 100644 --- a/spring-cloud-gateway-core/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-gateway-core/src/main/resources/META-INF/spring.factories @@ -3,4 +3,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration,\ org.springframework.cloud.gateway.config.GatewayAutoConfiguration,\ org.springframework.cloud.gateway.config.GatewayLoadBalancerClientAutoConfiguration,\ -org.springframework.cloud.gateway.config.GatewayRedisAutoConfiguration +org.springframework.cloud.gateway.config.GatewayRedisAutoConfiguration,\ +org.springframework.cloud.gateway.discovery.GatewayDiscoveryClientAutoConfiguration diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/discovery/GatewayDiscoveryClientAutoConfigurationTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/discovery/GatewayDiscoveryClientAutoConfigurationTests.java new file mode 100644 index 00000000..3c0df19a --- /dev/null +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/discovery/GatewayDiscoveryClientAutoConfigurationTests.java @@ -0,0 +1,76 @@ +/* + * Copyright 2013-2017 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 + * + * http://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 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.client.discovery.DiscoveryClient; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +@RunWith(Enclosed.class) +public class GatewayDiscoveryClientAutoConfigurationTests { + + @RunWith(SpringRunner.class) + @SpringBootTest(classes = Config.class, + properties = "spring.cloud.gateway.discovery.locator.enabled=true") + public static class EnabledByProperty { + + @Autowired(required = false) + private DiscoveryClientRouteDefinitionLocator locator; + + @Test + public void routeLocatorBeanExists() { + assertThat(locator) + .as("DiscoveryClientRouteDefinitionLocator was null") + .isNotNull(); + } + } + + @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 { + + @Bean + DiscoveryClient discoveryClient() { + return mock(DiscoveryClient.class); + } + } +}