feat:added polaris weighted round robin load balancer. (#1072)

pull/1078/head
veterancj 1 year ago committed by GitHub
parent cc8d72cf14
commit 98a5454eeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,4 +22,5 @@
- [fix:fix SCG report wrong service bug when using IP routing.](https://github.com/Tencent/spring-cloud-tencent/pull/1064) - [fix:fix SCG report wrong service bug when using IP routing.](https://github.com/Tencent/spring-cloud-tencent/pull/1064)
- [fix:fix gray release examples bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1067) - [fix:fix gray release examples bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1067)
- [fix:fix router label feign interceptor order.](https://github.com/Tencent/spring-cloud-tencent/pull/1070) - [fix:fix router label feign interceptor order.](https://github.com/Tencent/spring-cloud-tencent/pull/1070)
- [feat:added polaris weighted round robin load balancer.](https://github.com/Tencent/spring-cloud-tencent/pull/1072)
- [fix:add deregister judgment.](https://github.com/Tencent/spring-cloud-tencent/pull/1076) - [fix:add deregister judgment.](https://github.com/Tencent/spring-cloud-tencent/pull/1076)

@ -0,0 +1,101 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 com.tencent.cloud.polaris.loadbalancer;
import java.util.List;
import java.util.stream.Collectors;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
/**
* Abstract Loadbalancer of Polaris.
*
* @author <a href="mailto:veteranchen@tencent.com">veteranchen</a>
*/
public abstract class AbstractPolarisLoadBalancer implements ReactorServiceInstanceLoadBalancer {
private static final Logger log = LoggerFactory.getLogger(AbstractPolarisLoadBalancer.class);
private final String serviceId;
private final RouterAPI routerAPI;
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
public AbstractPolarisLoadBalancer(String serviceId, ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider, RouterAPI routerAPI) {
this.serviceId = serviceId;
this.supplierObjectProvider = supplierObjectProvider;
this.routerAPI = routerAPI;
}
private static ServiceInstances convertToPolarisServiceInstances(List<ServiceInstance> serviceInstances) {
ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceInstances.get(0).getServiceId());
List<Instance> polarisInstances = serviceInstances.stream()
.map(serviceInstance -> ((PolarisServiceInstance) serviceInstance).getPolarisInstance())
.collect(Collectors.toList());
return new DefaultServiceInstances(serviceKey, polarisInstances);
}
@Override
public Mono<Response<ServiceInstance>> choose(Request request) {
ServiceInstanceListSupplier supplier = supplierObjectProvider
.getIfAvailable(NoopServiceInstanceListSupplier::new);
return supplier.get(request).next().map(serviceInstances -> {
if (serviceInstances.isEmpty()) {
log.warn("No servers available for service: " + this.serviceId);
return new EmptyResponse();
}
ProcessLoadBalanceRequest req = new ProcessLoadBalanceRequest();
req.setDstInstances(convertToPolarisServiceInstances(serviceInstances));
req = setProcessLoadBalanceRequest(req);
try {
ProcessLoadBalanceResponse response = routerAPI.processLoadBalance(req);
return new DefaultResponse(new PolarisServiceInstance(response.getTargetInstance()));
}
catch (Exception e) {
log.warn("PolarisRoutingLoadbalancer error", e);
return new EmptyResponse();
}
});
}
protected abstract ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest req);
}

@ -95,6 +95,16 @@ public class PolarisLoadBalancerClientConfiguration {
loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), polarisSDKContextManager.getRouterAPI()); loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), polarisSDKContextManager.getRouterAPI());
} }
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.strategy", havingValue = "polarisWeightedRoundRobin")
public ReactorLoadBalancer<ServiceInstance> polarisWeightedRoundRobinLoadBalancer(Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory, PolarisSDKContextManager polarisSDKContextManager) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new PolarisWeightedRoundRobinLoadBalancer(name,
loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), polarisSDKContextManager.getRouterAPI());
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnReactiveDiscoveryEnabled @ConditionalOnReactiveDiscoveryEnabled
@Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER) @Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER)

@ -17,95 +17,37 @@
package com.tencent.cloud.polaris.loadbalancer; package com.tencent.cloud.polaris.loadbalancer;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
import com.tencent.polaris.api.config.consumer.LoadBalanceConfig; import com.tencent.polaris.api.config.consumer.LoadBalanceConfig;
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.rpc.Criteria; import com.tencent.polaris.api.rpc.Criteria;
import com.tencent.polaris.router.api.core.RouterAPI; import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest; import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
/** /**
* PolarisRingHashLoadBalancer. * PolarisRingHashLoadBalancer.
* *
* @author sean yu * @author sean yu
* @author <a href="mailto:veteranchen@tencent.com">veteranchen</a>
*/ */
public class PolarisRingHashLoadBalancer implements ReactorServiceInstanceLoadBalancer { public class PolarisRingHashLoadBalancer extends AbstractPolarisLoadBalancer {
private static final Logger log = LoggerFactory.getLogger(PolarisWeightedRandomLoadBalancer.class);
private final String serviceId;
private final RouterAPI routerAPI;
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
public PolarisRingHashLoadBalancer(String serviceId, public PolarisRingHashLoadBalancer(String serviceId,
ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider, ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider,
RouterAPI routerAPI) { RouterAPI routerAPI) {
this.serviceId = serviceId; super(serviceId, supplierObjectProvider, routerAPI);
this.supplierObjectProvider = supplierObjectProvider;
this.routerAPI = routerAPI;
}
private static ServiceInstances convertToPolarisServiceInstances(List<ServiceInstance> serviceInstances) {
ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceInstances.get(0).getServiceId());
List<Instance> polarisInstances = serviceInstances.stream()
.map(serviceInstance -> ((PolarisServiceInstance) serviceInstance).getPolarisInstance())
.collect(Collectors.toList());
return new DefaultServiceInstances(serviceKey, polarisInstances);
} }
@Override @Override
public Mono<Response<ServiceInstance>> choose(Request request) { protected ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest req) {
ServiceInstanceListSupplier supplier = supplierObjectProvider
.getIfAvailable(NoopServiceInstanceListSupplier::new);
String hashKey = Optional.ofNullable(PolarisLoadBalancerRingHashKeyProvider.getHashKey()).orElse(""); String hashKey = Optional.ofNullable(PolarisLoadBalancerRingHashKeyProvider.getHashKey()).orElse("");
return supplier.get(request).next().map(serviceInstances -> getInstanceResponse(serviceInstances, hashKey)); req.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_RING_HASH);
}
private Response<ServiceInstance> getInstanceResponse(List<ServiceInstance> serviceInstances, String hashKey) {
if (serviceInstances.isEmpty()) {
log.warn("No servers available for service: " + this.serviceId);
return new EmptyResponse();
}
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
request.setDstInstances(convertToPolarisServiceInstances(serviceInstances));
request.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_RING_HASH);
Criteria criteria = new Criteria(); Criteria criteria = new Criteria();
criteria.setHashKey(hashKey); criteria.setHashKey(hashKey);
request.setCriteria(criteria); req.setCriteria(criteria);
return req;
try {
ProcessLoadBalanceResponse response = routerAPI.processLoadBalance(request);
return new DefaultResponse(new PolarisServiceInstance(response.getTargetInstance()));
}
catch (Exception e) {
log.warn("PolarisRoutingLoadbalancer error", e);
return new EmptyResponse();
}
} }
} }

@ -17,88 +17,30 @@
package com.tencent.cloud.polaris.loadbalancer; package com.tencent.cloud.polaris.loadbalancer;
import java.util.List;
import java.util.stream.Collectors;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
import com.tencent.polaris.api.config.consumer.LoadBalanceConfig; import com.tencent.polaris.api.config.consumer.LoadBalanceConfig;
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.rpc.Criteria; import com.tencent.polaris.api.rpc.Criteria;
import com.tencent.polaris.router.api.core.RouterAPI; import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest; import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
/** /**
* Loadbalancer of Polaris. * Loadbalancer of Polaris.
* *
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a> * @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
* @author <a href="mailto:veteranchen@tencent.com">veteranchen</a>
*/ */
public class PolarisWeightedRandomLoadBalancer implements ReactorServiceInstanceLoadBalancer { public class PolarisWeightedRandomLoadBalancer extends AbstractPolarisLoadBalancer {
private static final Logger log = LoggerFactory.getLogger(PolarisWeightedRandomLoadBalancer.class);
private final String serviceId;
private final RouterAPI routerAPI;
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
public PolarisWeightedRandomLoadBalancer(String serviceId, ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider, RouterAPI routerAPI) { public PolarisWeightedRandomLoadBalancer(String serviceId, ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider, RouterAPI routerAPI) {
this.serviceId = serviceId; super(serviceId, supplierObjectProvider, routerAPI);
this.supplierObjectProvider = supplierObjectProvider;
this.routerAPI = routerAPI;
}
private static ServiceInstances convertToPolarisServiceInstances(List<ServiceInstance> serviceInstances) {
ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceInstances.get(0).getServiceId());
List<Instance> polarisInstances = serviceInstances.stream()
.map(serviceInstance -> ((PolarisServiceInstance) serviceInstance).getPolarisInstance())
.collect(Collectors.toList());
return new DefaultServiceInstances(serviceKey, polarisInstances);
} }
@Override @Override
public Mono<Response<ServiceInstance>> choose(Request request) { protected ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest req) {
ServiceInstanceListSupplier supplier = supplierObjectProvider req.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
.getIfAvailable(NoopServiceInstanceListSupplier::new); req.setCriteria(new Criteria());
return supplier.get(request).next().map(this::getInstanceResponse); return req;
}
private Response<ServiceInstance> getInstanceResponse(List<ServiceInstance> serviceInstances) {
if (serviceInstances.isEmpty()) {
log.warn("No servers available for service: " + this.serviceId);
return new EmptyResponse();
}
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
request.setDstInstances(convertToPolarisServiceInstances(serviceInstances));
request.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
request.setCriteria(new Criteria());
try {
ProcessLoadBalanceResponse response = routerAPI.processLoadBalance(request);
return new DefaultResponse(new PolarisServiceInstance(response.getTargetInstance()));
}
catch (Exception e) {
log.warn("PolarisRoutingLoadbalancer error", e);
return new EmptyResponse();
}
} }
} }

@ -0,0 +1,45 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 com.tencent.cloud.polaris.loadbalancer;
import com.tencent.polaris.api.config.consumer.LoadBalanceConfig;
import com.tencent.polaris.api.rpc.Criteria;
import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
/**
* WeightedRoundRobin Loadbalancer of Polaris.
*
* @author <a href="mailto:veteranchen@tencent.com">veteranchen</a>
*/
public class PolarisWeightedRoundRobinLoadBalancer extends AbstractPolarisLoadBalancer {
public PolarisWeightedRoundRobinLoadBalancer(String serviceId, ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider, RouterAPI routerAPI) {
super(serviceId, supplierObjectProvider, routerAPI);
}
@Override
protected ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest req) {
req.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_ROUND_ROBIN);
req.setCriteria(new Criteria());
return req;
}
}

@ -90,6 +90,18 @@ public class PolarisWeightedRandomLoadBalancerAutoConfigurationTest {
}); });
} }
@Test
public void testPolarisWeightedRoundRobinInitialization() {
this.contextRunner.withPropertyValues("spring.cloud.polaris.loadbalancer.strategy=polarisWeightedRoundRobin")
.run(context -> {
assertThat(context).hasSingleBean(RestTemplate.class);
assertThatThrownBy(() -> {
context.getBean(RestTemplate.class).getForEntity("http://wrong.url", String.class);
}).isInstanceOf(Exception.class);
});
}
@Test @Test
public void testPolarisRingHashInitialization() { public void testPolarisRingHashInitialization() {
this.contextRunner this.contextRunner

@ -23,6 +23,8 @@ import java.util.List;
import com.tencent.cloud.common.pojo.PolarisServiceInstance; import com.tencent.cloud.common.pojo.PolarisServiceInstance;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.polaris.api.exception.ErrorCode;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.api.pojo.Instance; import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.router.api.core.RouterAPI; import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse; import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
@ -42,6 +44,7 @@ import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.Request; import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.Response; import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_NAMESPACE; import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_NAMESPACE;
@ -83,6 +86,7 @@ public class PolarisWeightedRandomLoadBalancerTest {
@Test @Test
public void chooseNormalLogicTest_thenReturnAvailablePolarisInstance() { public void chooseNormalLogicTest_thenReturnAvailablePolarisInstance() {
Request request = Mockito.mock(Request.class); Request request = Mockito.mock(Request.class);
List<ServiceInstance> mockInstanceList = new ArrayList<>(); List<ServiceInstance> mockInstanceList = new ArrayList<>();
mockInstanceList.add(new PolarisServiceInstance(testInstance)); mockInstanceList.add(new PolarisServiceInstance(testInstance));
@ -115,4 +119,45 @@ public class PolarisWeightedRandomLoadBalancerTest {
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getHost()).isEqualTo("host"); Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getHost()).isEqualTo("host");
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getPort()).isEqualTo(8090); Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getPort()).isEqualTo(8090);
} }
@Test
public void chooseExceptionTest_thenReturnEmptyInstance() {
Request request = Mockito.mock(Request.class);
List<ServiceInstance> mockInstanceList = new ArrayList<>();
mockInstanceList.add(new PolarisServiceInstance(testInstance));
ServiceInstanceListSupplier serviceInstanceListSupplier = Mockito.mock(ServiceInstanceListSupplier.class);
when(serviceInstanceListSupplier.get(request)).thenReturn(Flux.just(mockInstanceList));
when(supplierObjectProvider.getIfAvailable(any())).thenReturn(serviceInstanceListSupplier);
when(routerAPI.processLoadBalance(any())).thenThrow(new PolarisException(ErrorCode.API_TIMEOUT));
// request construct and execute invoke
PolarisWeightedRandomLoadBalancer polarisWeightedRandomLoadBalancer = new PolarisWeightedRandomLoadBalancer(LOCAL_SERVICE, supplierObjectProvider, routerAPI);
Mono<Response<ServiceInstance>> responseMono = polarisWeightedRandomLoadBalancer.choose(request);
ServiceInstance serviceInstance = responseMono.block().getServer();
// verify method has invoked
verify(supplierObjectProvider).getIfAvailable(any());
//result assert
Assertions.assertThat(serviceInstance).isNull();
}
@Test
public void chooseEmptySupplierTest_thenReturnEmptyInstance() {
ServiceInstanceListSupplier noopSupplier = new NoopServiceInstanceListSupplier();
when(supplierObjectProvider.getIfAvailable(any())).thenReturn(noopSupplier);
// request construct and execute invoke
PolarisWeightedRandomLoadBalancer polarisWeightedRandomLoadBalancer = new PolarisWeightedRandomLoadBalancer(LOCAL_SERVICE, supplierObjectProvider, routerAPI);
Mono<Response<ServiceInstance>> responseMono = polarisWeightedRandomLoadBalancer.choose();
ServiceInstance serviceInstance = responseMono.block().getServer();
//result assert
Assertions.assertThat(serviceInstance).isNull();
}
} }

@ -0,0 +1,120 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 com.tencent.cloud.polaris.loadbalancer;
import java.util.ArrayList;
import java.util.List;
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_NAMESPACE;
import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_SERVICE;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Test for {@link PolarisWeightedRandomLoadBalancer}.
*
* @author <a href="mailto:veteranchen@tencent.com">veteranchen</a>
*/
@ExtendWith(MockitoExtension.class)
public class PolarisWeightedRoundRobinLoadBalancerTest {
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
private static Instance testInstance;
@Mock
private RouterAPI routerAPI;
@Mock
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
@BeforeAll
static void beforeAll() {
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
.thenReturn("unit-test");
testInstance = Instance.createDefaultInstance("instance-id", LOCAL_NAMESPACE,
LOCAL_SERVICE, "host", 8090);
}
@AfterAll
static void afterAll() {
mockedApplicationContextAwareUtils.close();
}
@Test
public void chooseNormalLogicTest_thenReturnAvailablePolarisInstance() {
Request request = Mockito.mock(Request.class);
List<ServiceInstance> mockInstanceList = new ArrayList<>();
mockInstanceList.add(new PolarisServiceInstance(testInstance));
ServiceInstanceListSupplier serviceInstanceListSupplier = Mockito.mock(ServiceInstanceListSupplier.class);
when(serviceInstanceListSupplier.get(request)).thenReturn(Flux.just(mockInstanceList));
when(supplierObjectProvider.getIfAvailable(any())).thenReturn(serviceInstanceListSupplier);
ProcessLoadBalanceResponse mockLbRes = new ProcessLoadBalanceResponse(testInstance);
when(routerAPI.processLoadBalance(any())).thenReturn(mockLbRes);
// request construct and execute invoke
PolarisWeightedRoundRobinLoadBalancer polarisWeightedRoundRobinLoadBalancer = new PolarisWeightedRoundRobinLoadBalancer(LOCAL_SERVICE, supplierObjectProvider, routerAPI);
Mono<Response<ServiceInstance>> responseMono = polarisWeightedRoundRobinLoadBalancer.choose(request);
ServiceInstance serviceInstance = responseMono.block().getServer();
// verify method has invoked
verify(supplierObjectProvider).getIfAvailable(any());
//result assert
Assertions.assertThat(serviceInstance).isNotNull();
Assertions.assertThat(serviceInstance instanceof PolarisServiceInstance).isTrue();
PolarisServiceInstance polarisServiceInstance = (PolarisServiceInstance) serviceInstance;
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getId()).isEqualTo("instance-id");
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getNamespace()).isEqualTo(LOCAL_NAMESPACE);
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getService()).isEqualTo(LOCAL_SERVICE);
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getHost()).isEqualTo("host");
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getPort()).isEqualTo(8090);
}
}
Loading…
Cancel
Save