Commit 31a3ff61 by Biju Kunjummen Committed by Spencer Gibb

GH-1916: Spring Boot Auto-configuration based Zuul Server and Proxy (#1951)

parent 3773047a
/* /*
* Copyright 2013-2015 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -34,11 +34,12 @@ import org.springframework.context.annotation.Import; ...@@ -34,11 +34,12 @@ import org.springframework.context.annotation.Import;
* *
* @author Spencer Gibb * @author Spencer Gibb
* @author Dave Syer * @author Dave Syer
* @author Biju Kunjummen
*/ */
@EnableCircuitBreaker @EnableCircuitBreaker
@EnableDiscoveryClient @EnableDiscoveryClient
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Import(ZuulProxyConfiguration.class) @Import(ZuulProxyMarkerConfiguration.class)
public @interface EnableZuulProxy { public @interface EnableZuulProxy {
} }
/* /*
* Copyright 2013-2015 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -33,11 +33,12 @@ import org.springframework.context.annotation.Import; ...@@ -33,11 +33,12 @@ import org.springframework.context.annotation.Import;
* @see EnableZuulProxy to see how to get reverse proxy out of the box * @see EnableZuulProxy to see how to get reverse proxy out of the box
* *
* @author Spencer Gibb * @author Spencer Gibb
* @author Biju Kunjummen
*/ */
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
@Import(ZuulConfiguration.class) @Import(ZuulServerMarkerConfiguration.class)
public @interface EnableZuulServer { public @interface EnableZuulServer {
} }
/* /*
* Copyright 2013-2015 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -22,6 +22,7 @@ import java.util.List; ...@@ -22,6 +22,7 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
...@@ -53,12 +54,14 @@ import org.springframework.context.annotation.Import; ...@@ -53,12 +54,14 @@ import org.springframework.context.annotation.Import;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
* @author Dave Syer * @author Dave Syer
* @author Biju Kunjummen
*/ */
@Configuration @Configuration
@Import({ RibbonCommandFactoryConfiguration.RestClientRibbonConfiguration.class, @Import({ RibbonCommandFactoryConfiguration.RestClientRibbonConfiguration.class,
RibbonCommandFactoryConfiguration.OkHttpRibbonConfiguration.class, RibbonCommandFactoryConfiguration.OkHttpRibbonConfiguration.class,
RibbonCommandFactoryConfiguration.HttpClientRibbonConfiguration.class }) RibbonCommandFactoryConfiguration.HttpClientRibbonConfiguration.class })
public class ZuulProxyConfiguration extends ZuulConfiguration { @ConditionalOnBean(ZuulProxyMarkerConfiguration.Marker.class)
public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Autowired(required = false) @Autowired(required = false)
...@@ -72,7 +75,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -72,7 +75,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
@Override @Override
public HasFeatures zuulFeature() { public HasFeatures zuulFeature() {
return HasFeatures.namedFeature("Zuul (Discovery)", ZuulProxyConfiguration.class); return HasFeatures.namedFeature("Zuul (Discovery)", ZuulProxyAutoConfiguration.class);
} }
@Bean @Bean
...@@ -98,6 +101,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -98,6 +101,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingBean(SimpleHostRoutingFilter.class)
public SimpleHostRoutingFilter simpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties zuulProperties) { public SimpleHostRoutingFilter simpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties zuulProperties) {
return new SimpleHostRoutingFilter(helper, zuulProperties); return new SimpleHostRoutingFilter(helper, zuulProperties);
} }
......
/*
* Copyright 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.netflix.zuul;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Responsible for adding in a marker bean to trigger activation of
* {@link ZuulServerAutoConfiguration}
*
* @author Biju Kunjummen
*/
@Configuration
public class ZuulProxyMarkerConfiguration {
@Bean
public Marker zuulProxyMarkerBean() {
return new Marker();
}
class Marker {
}
}
...@@ -74,9 +74,10 @@ import com.netflix.zuul.monitoring.TracerFactory; ...@@ -74,9 +74,10 @@ import com.netflix.zuul.monitoring.TracerFactory;
@Configuration @Configuration
@EnableConfigurationProperties({ ZuulProperties.class }) @EnableConfigurationProperties({ ZuulProperties.class })
@ConditionalOnClass(ZuulServlet.class) @ConditionalOnClass(ZuulServlet.class)
@ConditionalOnBean(ZuulServerMarkerConfiguration.Marker.class)
// Make sure to get the ServerProperties from the same place as a normal web app would // Make sure to get the ServerProperties from the same place as a normal web app would
@Import(ServerPropertiesAutoConfiguration.class) @Import(ServerPropertiesAutoConfiguration.class)
public class ZuulConfiguration { public class ZuulServerAutoConfiguration {
@Autowired @Autowired
protected ZuulProperties zuulProperties; protected ZuulProperties zuulProperties;
...@@ -89,7 +90,7 @@ public class ZuulConfiguration { ...@@ -89,7 +90,7 @@ public class ZuulConfiguration {
@Bean @Bean
public HasFeatures zuulFeature() { public HasFeatures zuulFeature() {
return HasFeatures.namedFeature("Zuul (Simple)", ZuulConfiguration.class); return HasFeatures.namedFeature("Zuul (Simple)", ZuulServerAutoConfiguration.class);
} }
@Bean @Bean
......
/*
* Copyright 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.netflix.zuul;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Responsible for adding in a marker bean to trigger activation of
* {@link ZuulServerAutoConfiguration}
*
* @author Biju Kunjummen
*/
@Configuration
public class ZuulServerMarkerConfiguration {
@Bean
public Marker zuulServerMarkerBean() {
return new Marker();
}
class Marker {
}
}
...@@ -8,7 +8,9 @@ org.springframework.cloud.netflix.hystrix.HystrixAutoConfiguration,\ ...@@ -8,7 +8,9 @@ org.springframework.cloud.netflix.hystrix.HystrixAutoConfiguration,\
org.springframework.cloud.netflix.hystrix.security.HystrixSecurityAutoConfiguration,\ org.springframework.cloud.netflix.hystrix.security.HystrixSecurityAutoConfiguration,\
org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration,\ org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration,\
org.springframework.cloud.netflix.rx.RxJavaAutoConfiguration,\ org.springframework.cloud.netflix.rx.RxJavaAutoConfiguration,\
org.springframework.cloud.netflix.metrics.servo.ServoMetricsAutoConfiguration org.springframework.cloud.netflix.metrics.servo.ServoMetricsAutoConfiguration,\
org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration,\
org.springframework.cloud.netflix.zuul.ZuulProxyAutoConfiguration
org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\ org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\
org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration
......
/*
* Copyright 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.netflix.zuul;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.netflix.zuul.filters.CompositeRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* To test the auto-configuration of Zuul Proxy
*
* @author Biju Kunjummen
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ZuulProxyAutoConfigurationTests {
@Autowired
private RouteLocator routeLocator;
@Autowired(required = false)
private RibbonRoutingFilter ribbonRoutingFilter;
@Test
public void testAutoConfiguredBeans() {
assertThat(routeLocator).isInstanceOf(CompositeRouteLocator.class);
assertThat(this.ribbonRoutingFilter).isNotNull();
}
@Configuration
@EnableAutoConfiguration
@EnableZuulProxy
static class TestConfig {
}
}
/* /*
* Copyright 2013-2016 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -36,6 +36,7 @@ import org.springframework.context.annotation.Bean; ...@@ -36,6 +36,7 @@ import org.springframework.context.annotation.Bean;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
* @author Biju Kunjummen
*/ */
public class ZuulProxyConfigurationTests { public class ZuulProxyConfigurationTests {
...@@ -60,7 +61,8 @@ public class ZuulProxyConfigurationTests { ...@@ -60,7 +61,8 @@ public class ZuulProxyConfigurationTests {
void testClient(Class<?> clientType, String property) { void testClient(Class<?> clientType, String property) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(TestConfig.class, ZuulProxyConfiguration.class); context.register(TestConfig.class, ZuulProxyMarkerConfiguration.class,
ZuulProxyAutoConfiguration.class);
if (property != null) { if (property != null) {
EnvironmentTestUtils.addEnvironment(context, property); EnvironmentTestUtils.addEnvironment(context, property);
} }
......
/*
* Copyright 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.netflix.zuul;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.netflix.zuul.filters.CompositeRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* To test the auto-configuration of Zuul Proxy
*
* @author Biju Kunjummen
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ZuulServerAutoConfigurationTests {
@Autowired
private RouteLocator routeLocator;
@Autowired(required = false)
private RibbonRoutingFilter ribbonRoutingFilter;
@Test
public void testAutoConfiguredBeans() {
assertThat(routeLocator).isInstanceOf(CompositeRouteLocator.class);
assertThat(ribbonRoutingFilter).isNull();
}
@Configuration
@EnableAutoConfiguration
@EnableZuulServer
static class TestConfig {
}
}
/*
* Copyright 2016-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.netflix.zuul.filters; package org.springframework.cloud.netflix.zuul.filters;
import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertFalse;
...@@ -24,7 +40,6 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; ...@@ -24,7 +40,6 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.RoutesMvcEndpoint; import org.springframework.cloud.netflix.zuul.RoutesMvcEndpoint;
import org.springframework.cloud.netflix.zuul.ZuulProxyConfiguration;
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator; import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter; import org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -186,10 +201,9 @@ class SampleCustomZuulProxyApplication { ...@@ -186,10 +201,9 @@ class SampleCustomZuulProxyApplication {
@Configuration @Configuration
@EnableZuulProxy @EnableZuulProxy
protected static class CustomZuulProxyConfig extends ZuulProxyConfiguration { protected static class CustomZuulProxyConfig {
@Bean @Bean
@Override
public SimpleHostRoutingFilter simpleHostRoutingFilter(ProxyRequestHelper helper, public SimpleHostRoutingFilter simpleHostRoutingFilter(ProxyRequestHelper helper,
ZuulProperties zuulProperties) { ZuulProperties zuulProperties) {
return new CustomHostRoutingFilter(helper, zuulProperties); return new CustomHostRoutingFilter(helper, zuulProperties);
......
/* /*
* Copyright 2013-2015 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -18,7 +18,8 @@ package org.springframework.cloud.netflix.zuul.metrics; ...@@ -18,7 +18,8 @@ package org.springframework.cloud.netflix.zuul.metrics;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.ClassPathExclusions; import org.springframework.cloud.ClassPathExclusions;
import org.springframework.cloud.netflix.zuul.ZuulConfiguration; import org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration;
import org.springframework.cloud.netflix.zuul.ZuulServerMarkerConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -42,7 +43,7 @@ public class ZuulEmptyMetricsApplicationTests { ...@@ -42,7 +43,7 @@ public class ZuulEmptyMetricsApplicationTests {
public void setUp() throws Exception { public void setUp() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ZuulEmptyMetricsApplicationTestsConfiguration.class, context.register(ZuulEmptyMetricsApplicationTestsConfiguration.class,
ZuulConfiguration.class); ZuulServerMarkerConfiguration.class, ZuulServerAutoConfiguration.class);
context.refresh(); context.refresh();
this.context = context; this.context = context;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment