use of org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer in project spring-cloud-tencent by Tencent.
the class PolarisCachingSpringLoadBalanceFactoryTest method test.
@Test
public void test() {
PolarisCachingSpringLoadBalanceFactory polarisCachingSpringLoadBalanceFactory = new PolarisCachingSpringLoadBalanceFactory(factory, null);
DefaultClientConfigImpl config1 = new DefaultClientConfigImpl();
config1.loadDefaultValues();
config1.setClientName(service1);
DefaultClientConfigImpl config2 = new DefaultClientConfigImpl();
config2.loadDefaultValues();
config2.setClientName(service2);
when(factory.getClientConfig(service1)).thenReturn(config1);
when(factory.getClientConfig(service2)).thenReturn(config2);
ILoadBalancer loadBalancer = new SimpleLoadBalancer();
when(factory.getLoadBalancer(service1)).thenReturn(loadBalancer);
when(factory.getLoadBalancer(service2)).thenReturn(loadBalancer);
ServerIntrospector serverIntrospector = new DefaultServerIntrospector();
when(factory.getInstance(service1, ServerIntrospector.class)).thenReturn(serverIntrospector);
when(factory.getInstance(service2, ServerIntrospector.class)).thenReturn(serverIntrospector);
// load balancer for service1
FeignLoadBalancer feignLoadBalancer = polarisCachingSpringLoadBalanceFactory.create(service1);
Assert.assertNotNull(feignLoadBalancer);
verify(factory).getClientConfig(service1);
verify(factory, times(0)).getClientConfig(service2);
verify(factory).getLoadBalancer(service1);
verify(factory, times(0)).getLoadBalancer(service2);
verify(factory).getInstance(service1, ServerIntrospector.class);
verify(factory, times(0)).getInstance(service2, ServerIntrospector.class);
Assert.assertEquals(loadBalancer, feignLoadBalancer.getLoadBalancer());
Assert.assertEquals(service1, feignLoadBalancer.getClientName());
// load balancer for service2
FeignLoadBalancer feignLoadBalancer2 = polarisCachingSpringLoadBalanceFactory.create(service2);
// load balancer for service1 again
feignLoadBalancer = polarisCachingSpringLoadBalanceFactory.create(service1);
Assert.assertNotNull(feignLoadBalancer);
verify(factory).getClientConfig(service1);
verify(factory).getClientConfig(service2);
verify(factory).getLoadBalancer(service1);
verify(factory).getLoadBalancer(service2);
verify(factory).getInstance(service1, ServerIntrospector.class);
verify(factory).getInstance(service2, ServerIntrospector.class);
Assert.assertEquals(loadBalancer, feignLoadBalancer2.getLoadBalancer());
Assert.assertEquals(service2, feignLoadBalancer2.getClientName());
}
use of org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer in project spring-cloud-tencent by Tencent.
the class PolarisCachingSpringLoadBalanceFactory method create.
@Override
public FeignLoadBalancer create(String clientName) {
FeignLoadBalancer client = this.cache.get(clientName);
if (client != null) {
return client;
}
IClientConfig config = this.factory.getClientConfig(clientName);
ILoadBalancer lb = this.factory.getLoadBalancer(clientName);
ServerIntrospector serverIntrospector = this.factory.getInstance(clientName, ServerIntrospector.class);
FeignLoadBalancer loadBalancer = new PolarisFeignLoadBalancer(lb, config, serverIntrospector);
// There is a concurrency problem here.
// When the concurrency is high, it may cause a service to create multiple FeignLoadBalancers.
// But there is no concurrency control in CachingSpringLoadBalancerFactory,
// so no locks will be added here for the time being
cache.putIfAbsent(clientName, loadBalancer);
return loadBalancer;
}
Aggregations