get service instances by Flux.blockLast() to resolve concurrent problem (#762)

pull/765/head
lepdou 3 years ago committed by GitHub
parent eef527bec8
commit 03659bb684
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,2 +1,4 @@
# Change Log # Change Log
--- ---
- [Bugfix: get service instances by Flux.blockLast() to resolve concurrent problem](https://github.com/Tencent/spring-cloud-tencent/pull/762)

@ -18,10 +18,8 @@
package com.tencent.cloud.polaris.loadbalancer; package com.tencent.cloud.polaris.loadbalancer;
import java.util.Collections; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.pojo.PolarisServiceInstance; import com.tencent.cloud.common.pojo.PolarisServiceInstance;
@ -54,19 +52,22 @@ public final class LoadBalancerUtils {
* @return ServiceInstances * @return ServiceInstances
*/ */
public static ServiceInstances transferServersToServiceInstances(Flux<List<ServiceInstance>> servers) { public static ServiceInstances transferServersToServiceInstances(Flux<List<ServiceInstance>> servers) {
AtomicReference<List<Instance>> instances = new AtomicReference<>(); List<ServiceInstance> serviceInstances = servers.blockLast();
servers.subscribe(serviceInstances -> instances.set(serviceInstances.stream()
.map(LoadBalancerUtils::transferServerToServiceInstance) List<Instance> instances = new ArrayList<>();
.collect(Collectors.toList()))); if (!CollectionUtils.isEmpty(serviceInstances)) {
String serviceName = null; for (ServiceInstance serviceInstance : serviceInstances) {
if (CollectionUtils.isEmpty(instances.get())) { instances.add(transferServerToServiceInstance(serviceInstance));
instances.set(Collections.emptyList()); }
} }
else {
serviceName = instances.get().get(0).getService(); String serviceName = "";
if (!CollectionUtils.isEmpty(instances)) {
serviceName = instances.get(0).getService();
} }
ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceName); ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceName);
return new DefaultServiceInstances(serviceKey, instances.get()); return new DefaultServiceInstances(serviceKey, instances);
} }
/** /**

Loading…
Cancel
Save