Commit 65bf3bb6 by Andreas Kluth Committed by Spencer Gibb

Add the ability to configure the max connections and max per route on…

Add the ability to configure the max connections and max per route on PoolingHttpClientConnectionManager.
parent bfd4efcd
...@@ -1033,6 +1033,8 @@ http://www.slideshare.net/MikeyCohen1/edge-architecture-ieee-international-confe ...@@ -1033,6 +1033,8 @@ http://www.slideshare.net/MikeyCohen1/edge-architecture-ieee-international-confe
Zuul's rule engine allows rules and filters to be written in essentially any JVM language, with built in support for Java and Groovy. Zuul's rule engine allows rules and filters to be written in essentially any JVM language, with built in support for Java and Groovy.
NOTE: The configuration property `zuul.max.host.connections` has been replaced by two new properties, `zuul.host.maxTotalConnections` and `zuul.host.maxPerRouteConnections` which default to 200 and 20 respectively.
[[netflix-zuul-reverse-proxy]] [[netflix-zuul-reverse-proxy]]
=== Embedded Zuul Reverse Proxy === Embedded Zuul Reverse Proxy
......
...@@ -30,6 +30,7 @@ import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent; ...@@ -30,6 +30,7 @@ import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper; import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator; import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
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.discovery.ServiceRouteMapper; import org.springframework.cloud.netflix.zuul.filters.discovery.ServiceRouteMapper;
import org.springframework.cloud.netflix.zuul.filters.discovery.SimpleServiceRouteMapper; import org.springframework.cloud.netflix.zuul.filters.discovery.SimpleServiceRouteMapper;
...@@ -105,8 +106,8 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -105,8 +106,8 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
} }
@Bean @Bean
public SimpleHostRoutingFilter simpleHostRoutingFilter(ProxyRequestHelper helper) { public SimpleHostRoutingFilter simpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties zuulProperties) {
return new SimpleHostRoutingFilter(helper); return new SimpleHostRoutingFilter(helper, zuulProperties);
} }
@Bean @Bean
......
...@@ -70,6 +70,8 @@ public class ZuulProperties { ...@@ -70,6 +70,8 @@ public class ZuulProperties {
private boolean ignoreLocalService = true; private boolean ignoreLocalService = true;
private Host host = new Host();
public Set<String> getIgnoredHeaders() { public Set<String> getIgnoredHeaders() {
Set<String> ignoredHeaders = new LinkedHashSet<>(this.ignoredHeaders); Set<String> ignoredHeaders = new LinkedHashSet<>(this.ignoredHeaders);
if (ClassUtils.isPresent( if (ClassUtils.isPresent(
...@@ -170,6 +172,14 @@ public class ZuulProperties { ...@@ -170,6 +172,14 @@ public class ZuulProperties {
} }
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Host {
private int maxTotalConnections = 200;
private int maxPerRouteConnections = 20;
}
public String getServletPattern() { public String getServletPattern() {
String path = this.servletPath; String path = this.servletPath;
if (!path.startsWith("/")) { if (!path.startsWith("/")) {
......
...@@ -64,6 +64,8 @@ import org.apache.http.message.BasicHeader; ...@@ -64,6 +64,8 @@ import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpRequest; import org.apache.http.message.BasicHttpRequest;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper; 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.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -91,6 +93,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter { ...@@ -91,6 +93,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
"SimpleHostRoutingFilter.connectionManagerTimer", true); "SimpleHostRoutingFilter.connectionManagerTimer", true);
private ProxyRequestHelper helper; private ProxyRequestHelper helper;
private Host hostProperties;
private PoolingHttpClientConnectionManager connectionManager; private PoolingHttpClientConnectionManager connectionManager;
private CloseableHttpClient httpClient; private CloseableHttpClient httpClient;
...@@ -107,12 +110,9 @@ public class SimpleHostRoutingFilter extends ZuulFilter { ...@@ -107,12 +110,9 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
} }
}; };
public SimpleHostRoutingFilter() { public SimpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties properties) {
this(new ProxyRequestHelper());
}
public SimpleHostRoutingFilter(ProxyRequestHelper helper) {
this.helper = helper; this.helper = helper;
this.hostProperties = properties.getHost();
} }
@PostConstruct @PostConstruct
...@@ -209,10 +209,9 @@ public class SimpleHostRoutingFilter extends ZuulFilter { ...@@ -209,10 +209,9 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
.build(); .build();
this.connectionManager = new PoolingHttpClientConnectionManager(registry); this.connectionManager = new PoolingHttpClientConnectionManager(registry);
this.connectionManager.setMaxTotal(Integer this.connectionManager.setMaxTotal(hostProperties.getMaxTotalConnections());
.parseInt(System.getProperty("zuul.max.host.connections", "200"))); this.connectionManager
this.connectionManager.setDefaultMaxPerRoute(Integer .setDefaultMaxPerRoute(hostProperties.getMaxPerRouteConnections());
.parseInt(System.getProperty("zuul.max.host.connections", "20")));
return this.connectionManager; return this.connectionManager;
} }
catch (Exception ex) { catch (Exception ex) {
......
...@@ -189,14 +189,18 @@ class SampleCustomZuulProxyApplication { ...@@ -189,14 +189,18 @@ class SampleCustomZuulProxyApplication {
@Configuration @Configuration
@EnableZuulProxy @EnableZuulProxy
protected static class CustomZuulProxyConfig extends ZuulProxyConfiguration { protected static class CustomZuulProxyConfig extends ZuulProxyConfiguration {
@Bean @Bean
@Override @Override
public SimpleHostRoutingFilter simpleHostRoutingFilter( public SimpleHostRoutingFilter simpleHostRoutingFilter(
ProxyRequestHelper helper) { ProxyRequestHelper helper, ZuulProperties zuulProperties) {
return new CustomHostRoutingFilter(); return new CustomHostRoutingFilter(helper, zuulProperties);
} }
private class CustomHostRoutingFilter extends SimpleHostRoutingFilter { private class CustomHostRoutingFilter extends SimpleHostRoutingFilter {
public CustomHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties zuulProperties) {
super(helper, zuulProperties);
}
@Override @Override
public Object run() { public Object run() {
......
/*
* Copyright 2013-2016 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.route;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertEquals;
import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment;
/**
* @author Andreas Kluth
* @author Spencer Gibb
*/
public class SimpleHostRoutingFilterTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@After
public void clear() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void connectionPropertiesAreApplied() {
addEnvironment(this.context, "zuul.host.maxTotalConnections=100", "zuul.host.maxPerRouteConnections=10");
setupContext();
PoolingHttpClientConnectionManager connMgr = getFilter().newConnectionManager();
assertEquals(100, connMgr.getMaxTotal());
assertEquals(10, connMgr.getDefaultMaxPerRoute());
}
@Test
public void defaultPropertiesAreApplied() {
setupContext();
PoolingHttpClientConnectionManager connMgr = getFilter().newConnectionManager();
assertEquals(200, connMgr.getMaxTotal());
assertEquals(20, connMgr.getDefaultMaxPerRoute());
}
private void setupContext() {
this.context.register(PropertyPlaceholderAutoConfiguration.class,
TestConfiguration.class);
this.context.refresh();
}
private SimpleHostRoutingFilter getFilter() {
return this.context.getBean(SimpleHostRoutingFilter.class);
}
@Configuration
@EnableConfigurationProperties(ZuulProperties.class)
protected static class TestConfiguration {
@Bean
SimpleHostRoutingFilter simpleHostRoutingFilter(ZuulProperties zuulProperties) {
return new SimpleHostRoutingFilter(new ProxyRequestHelper(), zuulProperties);
}
}
}
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