Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
9eed2490
Unverified
Commit
9eed2490
authored
Sep 22, 2016
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow customization of HttpClient.
Uses ribbon settings to set MaxConnTotal and MaxConnPerRoute. Also allows user to create a custom HttpClient as a constructor argument. fixes gh-1149
parent
bbff2b24
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
4 deletions
+54
-4
RibbonLoadBalancingHttpClient.java
.../netflix/ribbon/apache/RibbonLoadBalancingHttpClient.java
+27
-4
RibbonLoadBalancingHttpClientTests.java
...lix/ribbon/apache/RibbonLoadBalancingHttpClientTests.java
+27
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/apache/RibbonLoadBalancingHttpClient.java
View file @
9eed2490
...
...
@@ -43,30 +43,53 @@ import static org.springframework.cloud.netflix.ribbon.RibbonUtils.updateToHttps
public
class
RibbonLoadBalancingHttpClient
extends
AbstractLoadBalancingClient
<
RibbonApacheHttpRequest
,
RibbonApacheHttpResponse
>
{
private
final
HttpClient
delegate
=
HttpClientBuilder
.
create
().
disableCookieManagement
().
build
()
;
private
final
HttpClient
delegate
;
private
final
IClientConfig
config
;
private
final
ServerIntrospector
serverIntrospector
;
@Deprecated
public
RibbonLoadBalancingHttpClient
()
{
super
();
this
.
config
=
new
DefaultClientConfigImpl
();
this
.
serverIntrospector
=
new
DefaultServerIntrospector
();
this
(
new
DefaultClientConfigImpl
(),
new
DefaultServerIntrospector
());
}
@Deprecated
public
RibbonLoadBalancingHttpClient
(
final
ILoadBalancer
lb
)
{
super
(
lb
);
this
.
config
=
new
DefaultClientConfigImpl
();
this
.
delegate
=
createHttpClient
(
this
.
config
);
this
.
serverIntrospector
=
new
DefaultServerIntrospector
();
initWithNiwsConfig
(
config
);
}
public
RibbonLoadBalancingHttpClient
(
IClientConfig
config
,
ServerIntrospector
serverIntrospector
)
{
this
.
delegate
=
createHttpClient
(
config
);
this
.
config
=
config
;
this
.
serverIntrospector
=
serverIntrospector
;
initWithNiwsConfig
(
config
);
}
public
RibbonLoadBalancingHttpClient
(
HttpClient
delegate
,
IClientConfig
config
,
ServerIntrospector
serverIntrospector
)
{
this
.
delegate
=
delegate
;
this
.
config
=
config
;
this
.
serverIntrospector
=
serverIntrospector
;
initWithNiwsConfig
(
config
);
}
protected
HttpClient
createHttpClient
(
IClientConfig
config
)
{
return
HttpClientBuilder
.
create
()
// already defaults to 0 in builder, so resetting to 0 won't hurt
.
setMaxConnTotal
(
config
.
getPropertyAsInteger
(
CommonClientConfigKey
.
MaxTotalConnections
,
0
))
// already defaults to 0 in builder, so resetting to 0 won't hurt
.
setMaxConnPerRoute
(
config
.
getPropertyAsInteger
(
CommonClientConfigKey
.
MaxConnectionsPerHost
,
0
))
.
disableCookieManagement
()
.
useSystemProperties
()
// for proxy
.
build
();
}
protected
HttpClient
getDelegate
()
{
return
this
.
delegate
;
}
@Override
public
RibbonApacheHttpResponse
execute
(
RibbonApacheHttpRequest
request
,
final
IClientConfig
configOverride
)
throws
Exception
{
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/apache/RibbonLoadBalancingHttpClientTests.java
View file @
9eed2490
...
...
@@ -20,6 +20,7 @@ import org.apache.http.HttpResponse;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.client.methods.HttpUriRequest
;
import
org.apache.http.impl.conn.PoolingHttpClientConnectionManager
;
import
org.junit.Test
;
import
org.mockito.ArgumentCaptor
;
import
org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration
;
...
...
@@ -74,6 +75,20 @@ public class RibbonLoadBalancingHttpClientTests {
}
@Test
public
void
testConnections
()
throws
Exception
{
SpringClientFactory
factory
=
new
SpringClientFactory
();
factory
.
setApplicationContext
(
new
AnnotationConfigApplicationContext
(
RibbonAutoConfiguration
.
class
,
Connections
.
class
));
RibbonLoadBalancingHttpClient
client
=
factory
.
getClient
(
"service"
,
RibbonLoadBalancingHttpClient
.
class
);
HttpClient
delegate
=
client
.
getDelegate
();
PoolingHttpClientConnectionManager
connManager
=
(
PoolingHttpClientConnectionManager
)
ReflectionTestUtils
.
getField
(
delegate
,
"connManager"
);
assertThat
(
connManager
.
getMaxTotal
(),
is
(
101
));
assertThat
(
connManager
.
getDefaultMaxPerRoute
(),
is
(
201
));
}
@Test
public
void
testRequestConfigDoNotFollowRedirectsOverrideWithFollowRedirects
()
throws
Exception
{
...
...
@@ -135,6 +150,18 @@ public class RibbonLoadBalancingHttpClientTests {
}
}
@Configuration
protected
static
class
Connections
{
@Bean
public
IClientConfig
clientConfig
()
{
DefaultClientConfigImpl
config
=
new
DefaultClientConfigImpl
();
config
.
set
(
CommonClientConfigKey
.
MaxTotalConnections
,
101
);
config
.
set
(
CommonClientConfigKey
.
MaxConnectionsPerHost
,
201
);
return
config
;
}
}
private
RequestConfig
getBuiltRequestConfig
(
Class
<?>
defaultConfigurationClass
,
IClientConfig
configOverride
)
throws
Exception
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment