Optimize:optimize load balancer code and add testcase (#379)

pull/392/head
lingxiao,wu 3 years ago committed by GitHub
parent 08b3d1a474
commit 6efb0d2514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -30,3 +30,4 @@
- [docs: Update Readme.md](https://github.com/Tencent/spring-cloud-tencent/pull/381)
- [docs:optimize example](https://github.com/Tencent/spring-cloud-tencent/pull/386)
- [Feature: support spring cloud gateway routers](https://github.com/Tencent/spring-cloud-tencent/pull/388)
- [Optimize:optimize load balancer code and add testcase](https://github.com/Tencent/spring-cloud-tencent/pull/379)

@ -37,27 +37,23 @@ import org.springframework.util.CollectionUtils;
/**
* load balancer utils.
*
*@author lepdou 2022-05-17
* @author lepdou 2022-05-17
*/
public class LoadBalancerUtils {
private static final int DEFAULT_WEIGHT = 100;
/**
* transfer servers to ServiceInstances.
*
* @param servers servers
* @return ServiceInstances
*/
public static ServiceInstances transferServersToServiceInstances(Flux<List<ServiceInstance>> servers) {
AtomicReference<List<Instance>> instances = new AtomicReference<>();
servers.subscribe(serviceInstances -> {
instances.set(serviceInstances.stream().map(serviceInstance -> {
DefaultInstance instance = new DefaultInstance();
instance.setNamespace(MetadataContext.LOCAL_NAMESPACE);
instance.setService(serviceInstance.getServiceId());
instance.setProtocol(serviceInstance.getScheme());
instance.setId(serviceInstance.getInstanceId());
instance.setHost(serviceInstance.getHost());
instance.setPort(serviceInstance.getPort());
instance.setWeight(100);
instance.setMetadata(serviceInstance.getMetadata());
return instance;
}).collect(Collectors.toList()));
});
servers.subscribe(serviceInstances -> instances.set(serviceInstances.stream()
.map(LoadBalancerUtils::transferServerToServiceInstance)
.collect(Collectors.toList())));
String serviceName = null;
if (CollectionUtils.isEmpty(instances.get())) {
instances.set(Collections.emptyList());
@ -65,9 +61,27 @@ public class LoadBalancerUtils {
else {
serviceName = instances.get().get(0).getService();
}
ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceName);
return new DefaultServiceInstances(serviceKey, instances.get());
}
/**
* transfer ServiceInstance to DefaultInstance.
*
* @param serviceInstance serviceInstance
* @return defaultInstance
*/
public static DefaultInstance transferServerToServiceInstance(ServiceInstance serviceInstance) {
DefaultInstance instance = new DefaultInstance();
instance.setNamespace(MetadataContext.LOCAL_NAMESPACE);
instance.setService(serviceInstance.getServiceId());
instance.setProtocol(serviceInstance.getScheme());
instance.setId(serviceInstance.getInstanceId());
instance.setHost(serviceInstance.getHost());
instance.setPort(serviceInstance.getPort());
instance.setWeight(DEFAULT_WEIGHT);
instance.setMetadata(serviceInstance.getMetadata());
return instance;
}
}

@ -17,10 +17,9 @@
package com.tencent.cloud.polaris.loadbalancer;
import java.util.ArrayList;
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.DefaultInstance;
import org.apache.commons.lang.StringUtils;
@ -63,20 +62,10 @@ public class PolarisServiceInstanceListSupplier extends DelegatingServiceInstanc
throw new IllegalStateException(
"PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
}
List<ServiceInstance> serviceInstances = new ArrayList<>(allServers.size());
for (ServiceInstance server : allServers) {
DefaultInstance instance = new DefaultInstance();
instance.setNamespace(MetadataContext.LOCAL_NAMESPACE);
instance.setService(serviceName);
instance.setProtocol(server.getScheme());
instance.setId(server.getInstanceId());
instance.setHost(server.getHost());
instance.setPort(server.getPort());
instance.setWeight(100);
instance.setMetadata(server.getMetadata());
serviceInstances.add(new PolarisServiceInstance(instance));
}
return serviceInstances;
return allServers.stream().map(server -> {
DefaultInstance instance = LoadBalancerUtils.transferServerToServiceInstance(server);
return new PolarisServiceInstance(instance);
}).collect(Collectors.toList());
}
}

@ -18,12 +18,18 @@
package com.tencent.cloud.polaris.loadbalancer.config;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.cloud.polaris.loadbalancer.PolarisLoadBalancer;
import com.tencent.polaris.router.api.core.RouterAPI;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.PORT;
@ -43,6 +49,16 @@ public class PolarisLoadBalancerAutoConfigurationTest {
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER).withPropertyValues("server.port=" + PORT)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081");
private final ApplicationContextRunner polarisLoadbalancerContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisRibbonTest.class,
PolarisLoadBalancerAutoConfiguration.class,
PolarisContextAutoConfiguration.class,
LoadBalancerAutoConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER).withPropertyValues("server.port=" + PORT)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
.withPropertyValues("spring.cloud.polaris.loadbalancer.strategy=polarisWeighted");
@Test
public void testDefaultInitialization() {
this.contextRunner.run(context -> {
@ -51,10 +67,24 @@ public class PolarisLoadBalancerAutoConfigurationTest {
});
}
@Test
public void testPolarisLoadbalancerInitialization() {
this.polarisLoadbalancerContextRunner.run(context -> {
assertThat(context).hasSingleBean(RouterAPI.class);
assertThat(context).hasSingleBean(PolarisLoadBalancerProperties.class);
assertThat(hasPolarisLoadBalancer(context)).isTrue();
});
}
private boolean hasPolarisLoadBalancer(BeanFactory beanFactory) {
LoadBalancerClientFactory loadBalancerClientFactory = beanFactory.getBean(LoadBalancerClientFactory.class);
ReactiveLoadBalancer<ServiceInstance> instance = loadBalancerClientFactory.getInstance(SERVICE_PROVIDER);
return instance instanceof PolarisLoadBalancer;
}
@Configuration
@EnableAutoConfiguration
static class PolarisRibbonTest {
}
}

@ -30,7 +30,7 @@ import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisLoadBalancerAutoConfiguration}
* Test for {@link PolarisLoadBalancerAutoConfiguration}.
*
* @author Haotian Zhang
*/

Loading…
Cancel
Save