Search in sources :

Example 1 with FeignLoadBalancer

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());
}
Also used : FeignLoadBalancer(org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer) SimpleLoadBalancer(com.tencent.cloud.polaris.router.SimpleLoadBalancer) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) DefaultServerIntrospector(org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector) DefaultServerIntrospector(org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Example 2 with FeignLoadBalancer

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;
}
Also used : FeignLoadBalancer(org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) IClientConfig(com.netflix.client.config.IClientConfig) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector)

Aggregations

ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)2 ServerIntrospector (org.springframework.cloud.netflix.ribbon.ServerIntrospector)2 FeignLoadBalancer (org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer)2 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)1 IClientConfig (com.netflix.client.config.IClientConfig)1 SimpleLoadBalancer (com.tencent.cloud.polaris.router.SimpleLoadBalancer)1 Test (org.junit.Test)1 DefaultServerIntrospector (org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector)1