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
384fad6c
Commit
384fad6c
authored
Mar 05, 2015
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wrap SpringLBClientFactory in a CachingLBClientFactory so LBClients are reused.
fixes gh-240
parent
4bd7c3bd
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
133 additions
and
3 deletions
+133
-3
CachingLBClientFactory.java
...rk/cloud/netflix/feign/ribbon/CachingLBClientFactory.java
+47
-0
FeignRibbonClientAutoConfiguration.java
...flix/feign/ribbon/FeignRibbonClientAutoConfiguration.java
+8
-3
CachingLBClientFactoryTests.java
...oud/netflix/feign/ribbon/CachingLBClientFactoryTests.java
+78
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/ribbon/CachingLBClientFactory.java
0 → 100644
View file @
384fad6c
/*
* Copyright 2013-2015 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
.
feign
.
ribbon
;
import
feign.ribbon.LBClient
;
import
feign.ribbon.LBClientFactory
;
import
org.springframework.util.ConcurrentReferenceHashMap
;
import
java.util.Map
;
/**
* LBClientFactory that caches entries created.
* @author Spencer Gibb
*/
public
class
CachingLBClientFactory
implements
LBClientFactory
{
private
volatile
Map
<
String
,
LBClient
>
cache
=
new
ConcurrentReferenceHashMap
<>();
private
final
LBClientFactory
delegate
;
public
CachingLBClientFactory
(
LBClientFactory
delegate
)
{
this
.
delegate
=
delegate
;
}
@Override
public
LBClient
create
(
String
clientName
)
{
if
(
cache
.
containsKey
(
clientName
))
{
return
cache
.
get
(
clientName
);
}
LBClient
client
=
delegate
.
create
(
clientName
);
cache
.
put
(
clientName
,
client
);
return
client
;
}
}
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/ribbon/FeignRibbonClientAutoConfiguration.java
View file @
384fad6c
...
...
@@ -16,7 +16,6 @@
package
org
.
springframework
.
cloud
.
netflix
.
feign
.
ribbon
;
import
feign.ribbon.LBClientFactory
;
import
feign.ribbon.RibbonClient
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.AutoConfigureBefore
;
...
...
@@ -47,13 +46,18 @@ public class FeignRibbonClientAutoConfiguration {
private
SpringClientFactory
factory
;
@Bean
public
LBClientFactory
lb
ClientFactory
()
{
public
SpringLBClientFactory
springLB
ClientFactory
()
{
return
new
SpringLBClientFactory
(
factory
);
}
@Bean
public
CachingLBClientFactory
cachingLBClientFactory
()
{
return
new
CachingLBClientFactory
(
springLBClientFactory
());
}
@Bean
@ConditionalOnMissingBean
public
Client
feignRibbonClient
()
{
return
RibbonClient
.
builder
().
lbClientFactory
(
lb
ClientFactory
()).
build
();
return
RibbonClient
.
builder
().
lbClientFactory
(
cachingLB
ClientFactory
()).
build
();
}
}
\ No newline at end of file
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/ribbon/CachingLBClientFactoryTests.java
0 → 100644
View file @
384fad6c
/*
* Copyright 2013-2015 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
.
feign
.
ribbon
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.*;
import
com.netflix.client.config.CommonClientConfigKey
;
import
com.netflix.client.config.DefaultClientConfigImpl
;
import
com.netflix.client.config.IClientConfig
;
import
feign.ribbon.LBClient
;
import
feign.ribbon.LBClientFactory
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
/**
* @author Spencer Gibb
*/
public
class
CachingLBClientFactoryTests
{
@Mock
private
LBClientFactory
delegate
;
private
CachingLBClientFactory
factory
;
@Before
public
void
init
()
{
MockitoAnnotations
.
initMocks
(
this
);
IClientConfig
config
=
new
DefaultClientConfigImpl
();
config
.
set
(
CommonClientConfigKey
.
ConnectTimeout
,
1000
);
config
.
set
(
CommonClientConfigKey
.
ReadTimeout
,
500
);
LBClient
client1
=
LBClient
.
create
(
null
,
config
);
LBClient
client2
=
LBClient
.
create
(
null
,
config
);
when
(
delegate
.
create
(
"client1"
)).
thenReturn
(
client1
);
when
(
delegate
.
create
(
"client2"
)).
thenReturn
(
client2
);
factory
=
new
CachingLBClientFactory
(
delegate
);
}
@Test
public
void
delegateCreatesWhenMissing
()
{
LBClient
client
=
factory
.
create
(
"client1"
);
assertNotNull
(
"client was null"
,
client
);
verify
(
delegate
,
times
(
1
)).
create
(
"client1"
);
}
@Test
public
void
cacheWorks
()
{
LBClient
client
=
factory
.
create
(
"client2"
);
assertNotNull
(
"client was null"
,
client
);
client
=
factory
.
create
(
"client2"
);
assertNotNull
(
"client was null"
,
client
);
verify
(
delegate
,
times
(
1
)).
create
(
"client2"
);
}
}
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