Skip to content

Commit

Permalink
spring-boot 3.0 upgrade, spring cloud debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Aias00 committed Jul 4, 2024
1 parent bd2b48f commit efce72f
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.convert.selector.SpringCloudSelectorHandle;
import org.apache.shenyu.common.utils.JsonUtils;
import org.apache.shenyu.loadbalancer.cache.UpstreamCacheManager;
import org.apache.shenyu.loadbalancer.entity.Upstream;
import org.apache.shenyu.loadbalancer.factory.LoadBalancerFactory;
import org.apache.shenyu.plugin.springcloud.cache.ServiceInstanceCache;
import org.apache.shenyu.plugin.springcloud.handler.SpringCloudPluginDataHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

Expand All @@ -39,6 +42,8 @@
* spring cloud plugin loadbalancer.
*/
public final class ShenyuSpringCloudServiceChooser {

private static final Logger LOG = LoggerFactory.getLogger(ShenyuSpringCloudServiceChooser.class);

private final DiscoveryClient discoveryClient;

Expand All @@ -60,6 +65,7 @@ public Upstream choose(final String serviceId, final String selectorId,
// load service instance by serviceId
List<ServiceInstance> available = this.getServiceInstance(serviceId);
if (CollectionUtils.isEmpty(available)) {
LOG.info("choose return 1");
return null;
}
final SpringCloudSelectorHandle springCloudSelectorHandle = SpringCloudPluginDataHandler.SELECTOR_CACHED.get().obtainHandle(selectorId);
Expand All @@ -71,6 +77,7 @@ public Upstream choose(final String serviceId, final String selectorId,
List<Upstream> divideUpstreams = UpstreamCacheManager.getInstance().findUpstreamListBySelectorId(selectorId);
// gray flow,but upstream is null
if (CollectionUtils.isEmpty(divideUpstreams)) {
LOG.info("choose return 2");
return this.doSelect(serviceId, ip, loadbalancer);
}
// select server from available to choose
Expand All @@ -82,8 +89,10 @@ public Upstream choose(final String serviceId, final String selectorId,
.findFirst().ifPresent(choose::add);
}
if (CollectionUtils.isEmpty(choose)) {
LOG.info("choose return 3");
return this.doSelect(serviceId, ip, loadbalancer);
}
LOG.info("choose return 4");
// select by divideUpstreams
return this.doSelect(choose, loadbalancer, ip);
}
Expand Down Expand Up @@ -117,8 +126,11 @@ private Upstream doSelect(final List<Upstream> upstreamList, final String loadba
*/
private List<ServiceInstance> getServiceInstance(final String serviceId) {
if (CollectionUtils.isEmpty(ServiceInstanceCache.getServiceInstance(serviceId))) {
return Optional.ofNullable(discoveryClient.getInstances(serviceId)).orElse(Collections.emptyList());
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
LOG.info("getServiceInstance: {}", JsonUtils.toJson(instances));
return Optional.ofNullable(instances).orElse(Collections.emptyList());
}
LOG.info("ServiceInstanceCache.getServiceInstance(serviceId)");
return ServiceInstanceCache.getServiceInstance(serviceId);
}

Expand Down

0 comments on commit efce72f

Please sign in to comment.