Commit 384fad6c by Spencer Gibb

wrap SpringLBClientFactory in a CachingLBClientFactory so LBClients are reused.

fixes gh-240
parent 4bd7c3bd
/*
* 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;
}
}
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package org.springframework.cloud.netflix.feign.ribbon; package org.springframework.cloud.netflix.feign.ribbon;
import feign.ribbon.LBClientFactory;
import feign.ribbon.RibbonClient; import feign.ribbon.RibbonClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
...@@ -47,13 +46,18 @@ public class FeignRibbonClientAutoConfiguration { ...@@ -47,13 +46,18 @@ public class FeignRibbonClientAutoConfiguration {
private SpringClientFactory factory; private SpringClientFactory factory;
@Bean @Bean
public LBClientFactory lbClientFactory() { public SpringLBClientFactory springLBClientFactory() {
return new SpringLBClientFactory(factory); return new SpringLBClientFactory(factory);
} }
@Bean @Bean
public CachingLBClientFactory cachingLBClientFactory() {
return new CachingLBClientFactory(springLBClientFactory());
}
@Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public Client feignRibbonClient() { public Client feignRibbonClient() {
return RibbonClient.builder().lbClientFactory(lbClientFactory()).build(); return RibbonClient.builder().lbClientFactory(cachingLBClientFactory()).build();
} }
} }
\ No newline at end of file
/*
* 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");
}
}
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