Search in sources :

Example 1 with DefaultServerIntrospector

use of org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector 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 DefaultServerIntrospector

use of org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector in project spring-cloud-tencent by Tencent.

the class PolarisFeignLoadBalancerTest method testHasRouterContext.

@Test
public void testHasRouterContext() {
    DefaultClientConfigImpl config = new DefaultClientConfigImpl();
    config.loadDefaultValues();
    ILoadBalancer loadBalancer = new SimpleLoadBalancer();
    ServerIntrospector serverIntrospector = new DefaultServerIntrospector();
    PolarisFeignLoadBalancer polarisFeignLoadBalancer = new PolarisFeignLoadBalancer(loadBalancer, config, serverIntrospector);
    Map<String, String> labels = new HashMap<>();
    labels.put("k1", "v1");
    labels.put("k2", "v2");
    List<String> headerValues = new ArrayList<>();
    headerValues.add(JacksonUtils.serialize2Json(labels));
    Map<String, Collection<String>> headers = new HashMap<>();
    headers.put(RouterConstants.ROUTER_LABEL_HEADER, headerValues);
    // mock ApplicationContextAwareUtils#getProperties
    try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
        mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn("unit-test");
        MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
        // mock MetadataContextHolder#get
        try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {
            mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
            PolarisRouterContext routerContext = polarisFeignLoadBalancer.buildRouterContext(headers);
            Assert.assertNotNull(routerContext);
            Map<String, String> routerLabels = routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS);
            Assert.assertNotNull(routerLabels);
            Assert.assertEquals("v1", routerLabels.get("k1"));
            Assert.assertEquals("v2", routerLabels.get("k2"));
            Assert.assertNull(routerLabels.get("k3"));
        }
    }
}
Also used : SimpleLoadBalancer(com.tencent.cloud.polaris.router.SimpleLoadBalancer) HashMap(java.util.HashMap) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) DefaultServerIntrospector(org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector) ArrayList(java.util.ArrayList) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) DefaultServerIntrospector(org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector) Mockito.anyString(org.mockito.Mockito.anyString) PolarisRouterContext(com.tencent.cloud.polaris.router.PolarisRouterContext) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) MetadataContextHolder(com.tencent.cloud.common.metadata.MetadataContextHolder) Collection(java.util.Collection) MetadataContext(com.tencent.cloud.common.metadata.MetadataContext) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Example 3 with DefaultServerIntrospector

use of org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector in project spring-cloud-tencent by Tencent.

the class PolarisFeignLoadBalancerTest method testHasNoneRouterContext.

@Test
public void testHasNoneRouterContext() {
    DefaultClientConfigImpl config = new DefaultClientConfigImpl();
    config.loadDefaultValues();
    ILoadBalancer loadBalancer = new SimpleLoadBalancer();
    ServerIntrospector serverIntrospector = new DefaultServerIntrospector();
    PolarisFeignLoadBalancer polarisFeignLoadBalancer = new PolarisFeignLoadBalancer(loadBalancer, config, serverIntrospector);
    Map<String, Collection<String>> headers = new HashMap<>();
    // mock ApplicationContextAwareUtils#getProperties
    try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
        mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn("unit-test");
        MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
        // mock MetadataContextHolder#get
        try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {
            mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
            PolarisRouterContext routerContext = polarisFeignLoadBalancer.buildRouterContext(headers);
            Assert.assertNull(routerContext);
        }
    }
}
Also used : SimpleLoadBalancer(com.tencent.cloud.polaris.router.SimpleLoadBalancer) HashMap(java.util.HashMap) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) DefaultServerIntrospector(org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) DefaultServerIntrospector(org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector) Mockito.anyString(org.mockito.Mockito.anyString) PolarisRouterContext(com.tencent.cloud.polaris.router.PolarisRouterContext) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) MetadataContextHolder(com.tencent.cloud.common.metadata.MetadataContextHolder) Collection(java.util.Collection) MetadataContext(com.tencent.cloud.common.metadata.MetadataContext) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Aggregations

DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)3 ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)3 SimpleLoadBalancer (com.tencent.cloud.polaris.router.SimpleLoadBalancer)3 Test (org.junit.Test)3 DefaultServerIntrospector (org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector)3 ServerIntrospector (org.springframework.cloud.netflix.ribbon.ServerIntrospector)3 MetadataContext (com.tencent.cloud.common.metadata.MetadataContext)2 MetadataContextHolder (com.tencent.cloud.common.metadata.MetadataContextHolder)2 ApplicationContextAwareUtils (com.tencent.cloud.common.util.ApplicationContextAwareUtils)2 PolarisRouterContext (com.tencent.cloud.polaris.router.PolarisRouterContext)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Mockito.anyString (org.mockito.Mockito.anyString)2 ArrayList (java.util.ArrayList)1 FeignLoadBalancer (org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer)1