Unverified Commit 74ff6595 by Spencer Gibb

Merge branch 'master' into 2.0.x

parents 72b7ee24 9e180b8b
......@@ -26,10 +26,10 @@ import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicyFactory;
import org.springframework.cloud.client.loadbalancer.RetryableStatusCodeException;
import org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
import org.springframework.cloud.netflix.ribbon.support.RetryableStatusCodeException;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.policy.NeverRetryPolicy;
......
......@@ -16,19 +16,30 @@
package org.springframework.cloud.netflix.ribbon;
import com.netflix.loadbalancer.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.netflix.loadbalancer.Server;
/**
* @author Spencer Gibb
*/
public class DefaultServerIntrospector implements ServerIntrospector {
private ServerIntrospectorProperties serverIntrospectorProperties = new ServerIntrospectorProperties();
@Autowired(required = false)
public void setServerIntrospectorProperties(ServerIntrospectorProperties serverIntrospectorProperties){
this.serverIntrospectorProperties = serverIntrospectorProperties;
}
@Override
public boolean isSecure(Server server) {
// Can we do better?
return (""+server.getPort()).endsWith("443");
return serverIntrospectorProperties.getSecurePorts().contains(server.getPort());
}
@Override
......
......@@ -61,7 +61,7 @@ import com.netflix.ribbon.Ribbon;
@RibbonClients
@AutoConfigureAfter(name = "org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration")
@AutoConfigureBefore({LoadBalancerAutoConfiguration.class, AsyncLoadBalancerAutoConfiguration.class})
@EnableConfigurationProperties(RibbonEagerLoadProperties.class)
@EnableConfigurationProperties({RibbonEagerLoadProperties.class, ServerIntrospectorProperties.class})
public class RibbonAutoConfiguration {
@Autowired(required = false)
......
/*
* 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.netflix.ribbon;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Arrays;
import java.util.List;
/**
* @author Rico Pahlisch
*/
@Data
@ConfigurationProperties("ribbon")
public class ServerIntrospectorProperties {
private List<Integer> securePorts = Arrays.asList(443,8443);
}
......@@ -25,11 +25,11 @@ import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicyFactory;
import org.springframework.cloud.client.loadbalancer.RetryableStatusCodeException;
import org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser;
import org.springframework.cloud.netflix.feign.ribbon.FeignRetryPolicy;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
import org.springframework.cloud.netflix.ribbon.support.RetryableStatusCodeException;
import org.springframework.http.HttpRequest;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
......
......@@ -25,11 +25,11 @@ import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicyFactory;
import org.springframework.cloud.client.loadbalancer.RetryableStatusCodeException;
import org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser;
import org.springframework.cloud.netflix.feign.ribbon.FeignRetryPolicy;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
import org.springframework.cloud.netflix.ribbon.support.RetryableStatusCodeException;
import org.springframework.http.HttpRequest;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
......
......@@ -23,7 +23,10 @@ import java.io.IOException;
/**
* Exception to be thrown when the status code is deemed to be retryable.
* @author Ryan Baxter
* @deprecated Use {@link org.springframework.cloud.client.loadbalancer.RetryableStatusCodeException} instead
*/
//TODO Remove in Edgeware
@Deprecated
public class RetryableStatusCodeException extends IOException {
private static final String MESSAGE = "Service %s returned a status code of %d";
......
/*
* 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");
* you may not use this file except in compliance with the License.
......@@ -34,11 +34,12 @@ import org.springframework.context.annotation.Import;
*
* @author Spencer Gibb
* @author Dave Syer
* @author Biju Kunjummen
*/
@EnableCircuitBreaker
@EnableDiscoveryClient
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(ZuulProxyConfiguration.class)
@Import(ZuulProxyMarkerConfiguration.class)
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");
* you may not use this file except in compliance with the License.
......@@ -33,11 +33,12 @@ import org.springframework.context.annotation.Import;
* @see EnableZuulProxy to see how to get reverse proxy out of the box
*
* @author Spencer Gibb
* @author Biju Kunjummen
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ZuulConfiguration.class)
@Import(ZuulServerMarkerConfiguration.class)
public @interface EnableZuulServer {
}
......@@ -19,8 +19,8 @@ package org.springframework.cloud.netflix.zuul;
import java.lang.reflect.Field;
import java.util.Map;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.util.ReflectionUtils;
......@@ -39,7 +39,7 @@ import lombok.extern.apachecommons.CommonsLog;
*
*/
@CommonsLog
public class ZuulFilterInitializer implements ServletContextListener {
public class ZuulFilterInitializer {
private final Map<String, ZuulFilter> filters;
private final CounterFactory counterFactory;
......@@ -59,10 +59,9 @@ public class ZuulFilterInitializer implements ServletContextListener {
this.filterRegistry = filterRegistry;
}
@Override
public void contextInitialized(ServletContextEvent sce) {
log.info("Starting filter initializer context listener");
@PostConstruct
public void contextInitialized() {
log.info("Starting filter initializer");
TracerFactory.initialize(tracerFactory);
CounterFactory.initialize(counterFactory);
......@@ -72,9 +71,9 @@ public class ZuulFilterInitializer implements ServletContextListener {
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
log.info("Stopping filter initializer context listener");
@PreDestroy
public void contextDestroyed() {
log.info("Stopping filter initializer");
for (Map.Entry<String, ZuulFilter> entry : this.filters.entrySet()) {
filterRegistry.remove(entry.getKey());
}
......
......@@ -23,6 +23,7 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint;
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.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
......@@ -54,12 +55,14 @@ import org.springframework.context.annotation.Import;
/**
* @author Spencer Gibb
* @author Dave Syer
* @author Biju Kunjummen
*/
@Configuration
@Import({ RibbonCommandFactoryConfiguration.RestClientRibbonConfiguration.class,
RibbonCommandFactoryConfiguration.OkHttpRibbonConfiguration.class,
RibbonCommandFactoryConfiguration.HttpClientRibbonConfiguration.class })
public class ZuulProxyConfiguration extends ZuulConfiguration {
@ConditionalOnBean(ZuulProxyMarkerConfiguration.Marker.class)
public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {
@SuppressWarnings("rawtypes")
@Autowired(required = false)
......@@ -73,7 +76,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
@Override
public HasFeatures zuulFeature() {
return HasFeatures.namedFeature("Zuul (Discovery)", ZuulProxyConfiguration.class);
return HasFeatures.namedFeature("Zuul (Discovery)", ZuulProxyAutoConfiguration.class);
}
@Bean
......@@ -99,6 +102,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
}
@Bean
@ConditionalOnMissingBean(SimpleHostRoutingFilter.class)
public SimpleHostRoutingFilter simpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties 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 {
}
}
......@@ -73,9 +73,10 @@ import com.netflix.zuul.monitoring.TracerFactory;
@Configuration
@EnableConfigurationProperties({ ZuulProperties.class })
@ConditionalOnClass(ZuulServlet.class)
@ConditionalOnBean(ZuulServerMarkerConfiguration.Marker.class)
// Make sure to get the ServerProperties from the same place as a normal web app would
// FIXME @Import(ServerPropertiesAutoConfiguration.class)
public class ZuulConfiguration {
public class ZuulServerAutoConfiguration {
@Autowired
protected ZuulProperties zuulProperties;
......@@ -88,7 +89,7 @@ public class ZuulConfiguration {
@Bean
public HasFeatures zuulFeature() {
return HasFeatures.namedFeature("Zuul (Simple)", ZuulConfiguration.class);
return HasFeatures.namedFeature("Zuul (Simple)", ZuulServerAutoConfiguration.class);
}
@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 {
}
}
......@@ -329,6 +329,14 @@ public class ZuulProperties {
*/
private int maxPerRouteConnections = 20;
/**
* The socket timeout in millis. Defaults to 10000.
*/
private int socketTimeoutMillis = 10000;
/**
* The connection timeout in millis. Defaults to 2000.
*/
private int connectTimeoutMillis = 2000;
/**
* The lifetime for the connection pool.
*/
private long timeToLive = -1;
......
......@@ -67,18 +67,17 @@ import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.protocol.HttpContext;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.Host;
import org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException;
import org.springframework.context.event.EventListener;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import com.netflix.config.DynamicIntProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.constants.ZuulConstants;
import com.netflix.zuul.context.RequestContext;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.HTTPS_SCHEME;
......@@ -98,14 +97,6 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
private static final Log log = LogFactory.getLog(SimpleHostRoutingFilter.class);
private static final DynamicIntProperty SOCKET_TIMEOUT = DynamicPropertyFactory
.getInstance()
.getIntProperty(ZuulConstants.ZUUL_HOST_SOCKET_TIMEOUT_MILLIS, 10000);
private static final DynamicIntProperty CONNECTION_TIMEOUT = DynamicPropertyFactory
.getInstance()
.getIntProperty(ZuulConstants.ZUUL_HOST_CONNECT_TIMEOUT_MILLIS, 2000);
private final Timer connectionManagerTimer = new Timer(
"SimpleHostRoutingFilter.connectionManagerTimer", true);
......@@ -117,9 +108,18 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
private PoolingHttpClientConnectionManager connectionManager;
private CloseableHttpClient httpClient;
private final Runnable clientloader = new Runnable() {
@Override
public void run() {
@EventListener
public void onPropertyChange(EnvironmentChangeEvent event) {
boolean createNewClient = false;
for (String key : event.getKeys()) {
if (key.startsWith("zuul.host.")) {
createNewClient = true;
break;
}
}
if (createNewClient) {
try {
SimpleHostRoutingFilter.this.httpClient.close();
}
......@@ -128,7 +128,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
}
SimpleHostRoutingFilter.this.httpClient = newClient();
}
};
}
public SimpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties properties) {
this.helper = helper;
......@@ -141,8 +141,6 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
@PostConstruct
private void initialize() {
this.httpClient = newClient();
SOCKET_TIMEOUT.addCallback(this.clientloader);
CONNECTION_TIMEOUT.addCallback(this.clientloader);
this.connectionManagerTimer.schedule(new TimerTask() {
@Override
public void run() {
......@@ -251,8 +249,8 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
protected CloseableHttpClient newClient() {
final RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(SOCKET_TIMEOUT.get())
.setConnectTimeout(CONNECTION_TIMEOUT.get())
.setSocketTimeout(this.hostProperties.getSocketTimeoutMillis())
.setConnectTimeout(this.hostProperties.getConnectTimeoutMillis())
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
HttpClientBuilder httpClientBuilder = HttpClients.custom();
......
......@@ -8,7 +8,9 @@ org.springframework.cloud.netflix.hystrix.HystrixAutoConfiguration,\
org.springframework.cloud.netflix.hystrix.security.HystrixSecurityAutoConfiguration,\
org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration,\
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.netflix.hystrix.HystrixCircuitBreakerConfiguration
......
/*
* 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.netflix.ribbon;
import com.netflix.loadbalancer.Server;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Rico Pahlisch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DefaultServerIntrospectorDefaultTest.TestConfiguration.class)
public class DefaultServerIntrospectorDefaultTest {
@Autowired
private ServerIntrospector serverIntrospector;
@Test
public void testDefaultSslPorts(){
Server serverMock = mock(Server.class);
when(serverMock.getPort()).thenReturn(443);
Assert.assertTrue(serverIntrospector.isSecure(serverMock));
when(serverMock.getPort()).thenReturn(8443);
Assert.assertTrue(serverIntrospector.isSecure(serverMock));
when(serverMock.getPort()).thenReturn(16443);
Assert.assertFalse(serverIntrospector.isSecure(serverMock));
}
@Configuration
@EnableConfigurationProperties(ServerIntrospectorProperties.class)
protected static class TestConfiguration {
@Bean
public DefaultServerIntrospector defaultServerIntrospector(){
return new DefaultServerIntrospector();
}
}
}
/*
* 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.netflix.ribbon;
import com.netflix.loadbalancer.Server;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Rico Pahlisch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DefaultServerIntrospectorTest.TestConfiguration.class)
@TestPropertySource(properties = { "ribbon.securePorts=12345,556" })
public class DefaultServerIntrospectorTest {
@Autowired
private ServerIntrospector serverIntrospector;
@Test
public void testSecurePortConfiguration(){
Server serverMock = mock(Server.class);
when(serverMock.getPort()).thenReturn(12345);
Assert.assertTrue(serverIntrospector.isSecure(serverMock));
when(serverMock.getPort()).thenReturn(556);
Assert.assertTrue(serverIntrospector.isSecure(serverMock));
when(serverMock.getPort()).thenReturn(443);
Assert.assertFalse(serverIntrospector.isSecure(serverMock));
}
@Configuration
@EnableConfigurationProperties(ServerIntrospectorProperties.class)
protected static class TestConfiguration {
@Bean
public DefaultServerIntrospector defaultServerIntrospector(){
return new DefaultServerIntrospector();
}
}
}
......@@ -16,12 +16,12 @@
package org.springframework.cloud.netflix.zuul;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContextEvent;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ReflectionUtils;
......@@ -31,10 +31,6 @@ import com.netflix.zuul.filters.FilterRegistry;
import com.netflix.zuul.monitoring.CounterFactory;
import com.netflix.zuul.monitoring.TracerFactory;
import org.junit.Test;
import java.lang.reflect.Constructor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
......@@ -42,9 +38,6 @@ import static org.mockito.Mockito.mock;
public class ZuulFilterInitializerTests {
private static final ServletContextEvent DUMMY_SERVLET_CONTEXT_EVENT = mock(
ServletContextEvent.class);
private Map<String, ZuulFilter> filters = getFilters();
private CounterFactory counterFactory = mock(CounterFactory.class);
private TracerFactory tracerFactory = mock(TracerFactory.class);
......@@ -56,7 +49,7 @@ public class ZuulFilterInitializerTests {
@Test
public void shouldSetupOnContextInitializedEvent() throws Exception {
initializer.contextInitialized(DUMMY_SERVLET_CONTEXT_EVENT);
initializer.contextInitialized();
assertEquals(tracerFactory, TracerFactory.instance());
assertEquals(counterFactory, CounterFactory.instance());
......@@ -66,7 +59,7 @@ public class ZuulFilterInitializerTests {
@Test
public void shouldCleanupOnContextDestroyed() throws Exception {
initializer.contextDestroyed(DUMMY_SERVLET_CONTEXT_EVENT);
initializer.contextDestroyed();
assertEquals(null, ReflectionTestUtils.getField(TracerFactory.class, "INSTANCE"));
assertEquals(null,
......
/*
* 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");
* you may not use this file except in compliance with the License.
......@@ -36,6 +36,7 @@ import org.springframework.context.annotation.Bean;
/**
* @author Spencer Gibb
* @author Biju Kunjummen
*/
public class ZuulProxyConfigurationTests {
......@@ -60,7 +61,8 @@ public class ZuulProxyConfigurationTests {
void testClient(Class<?> clientType, String property) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(TestConfig.class, ZuulProxyConfiguration.class);
context.register(TestConfig.class, ZuulProxyMarkerConfiguration.class,
ZuulProxyAutoConfiguration.class);
if (property != null) {
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 {
}
}
......@@ -41,7 +41,6 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
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.route.SimpleHostRoutingFilter;
import org.springframework.context.annotation.Bean;
......@@ -203,10 +202,9 @@ class SampleCustomZuulProxyApplication {
@Configuration
@EnableZuulProxy
protected static class CustomZuulProxyConfig extends ZuulProxyConfiguration {
protected static class CustomZuulProxyConfig {
@Bean
@Override
public SimpleHostRoutingFilter simpleHostRoutingFilter(ProxyRequestHelper helper,
ZuulProperties zuulProperties) {
return new CustomHostRoutingFilter(helper, zuulProperties);
......
......@@ -23,6 +23,7 @@ import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;
......@@ -47,6 +48,7 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
......@@ -55,6 +57,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -77,8 +80,7 @@ import static org.springframework.util.StreamUtils.copyToString;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SampleApplication.class,
webEnvironment = RANDOM_PORT,
properties = {"server.servlet.contextPath: /app", "zuul.host.socket-timeout-millis=11000",
"zuul.host.connect-timeout-millis=2100"})
properties = {"server.servlet.contextPath: /app"})
@DirtiesContext
public class SimpleHostRoutingFilterTests {
......@@ -96,6 +98,8 @@ public class SimpleHostRoutingFilterTests {
@Test
public void timeoutPropertiesAreApplied() {
addEnvironment(this.context, "zuul.host.socket-timeout-millis=11000",
"zuul.host.connect-timeout-millis=2100");
setupContext();
CloseableHttpClient httpClient = getFilter().newClient();
Assertions.assertThat(httpClient).isInstanceOf(Configurable.class);
......@@ -189,6 +193,17 @@ public class SimpleHostRoutingFilterTests {
assertTrue("Get 1".equals(responseString));
}
@Test
public void zuulHostKeysUpdateHttpClient() {
setupContext();
SimpleHostRoutingFilter filter = getFilter();
CloseableHttpClient httpClient = (CloseableHttpClient) ReflectionTestUtils.getField(filter, "httpClient");
EnvironmentChangeEvent event = new EnvironmentChangeEvent(Collections.singleton("zuul.host.mykey"));
filter.onPropertyChange(event);
CloseableHttpClient newhttpClient = (CloseableHttpClient) ReflectionTestUtils.getField(filter, "httpClient");
Assertions.assertThat(httpClient).isNotEqualTo(newhttpClient);
}
private void setupContext() {
this.context.register(PropertyPlaceholderAutoConfiguration.class,
TestConfiguration.class);
......@@ -200,9 +215,14 @@ public class SimpleHostRoutingFilterTests {
}
@Configuration
@EnableConfigurationProperties(ZuulProperties.class)
@EnableConfigurationProperties
protected static class TestConfiguration {
@Bean
ZuulProperties zuulProperties() {
return new ZuulProperties();
}
@Bean
SimpleHostRoutingFilter simpleHostRoutingFilter(ZuulProperties zuulProperties) {
return new SimpleHostRoutingFilter(new ProxyRequestHelper(), zuulProperties);
}
......
......@@ -19,7 +19,8 @@ package org.springframework.cloud.netflix.zuul.metrics;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.test.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.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -43,7 +44,7 @@ public class ZuulEmptyMetricsApplicationTests {
public void setUp() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ZuulEmptyMetricsApplicationTestsConfiguration.class,
ZuulConfiguration.class);
ZuulServerMarkerConfiguration.class, ZuulServerAutoConfiguration.class);
context.refresh();
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