polaris load balancer: 提取负载均衡器公共抽象基类,实现加权轮训负载均衡

add change log, unit test

--issue=968
pull/1062/head
veteranchen 2 years ago
parent e9c9fc819d
commit 114a4940ed

@ -22,3 +22,4 @@
- [fix:fix SCG report wrong service bug when using IP routing.](https://github.com/Tencent/spring-cloud-tencent/pull/1063) - [fix:fix SCG report wrong service bug when using IP routing.](https://github.com/Tencent/spring-cloud-tencent/pull/1063)
- [fix:fix gray release examples bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1066) - [fix:fix gray release examples bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1066)
- [fix:fix router label feign interceptor order.](https://github.com/Tencent/spring-cloud-tencent/pull/1069) - [fix:fix router label feign interceptor order.](https://github.com/Tencent/spring-cloud-tencent/pull/1069)
- [feat:added polaris weighted round robin load balancer.](https://github.com/Tencent/spring-cloud-tencent/pull/1062)

@ -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;
@ -117,4 +120,44 @@ public class PolarisWeightedRandomLoadBalancerTest {
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();
}
} }

Loading…
Cancel
Save