🐛 Fixing a bug. #2264 灰度发布设置版本号时无法负载均衡
🐛 Fixing a bug. #2264 灰度发布设置版本号时无法负载均衡
This commit is contained in:
1 parent
36df95ca78
commit
b80fce8c10
2 files changed
+29
-15
No files matched your search
+28
-14
@@ -1,22 +1,30 @@
|
||||
package com.pig4cloud.pigx.common.gray.rule;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.cloud.nacos.NacosServiceInstance;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.*;
|
||||
import org.springframework.cloud.client.loadbalancer.DefaultRequestContext;
|
||||
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.RequestData;
|
||||
import org.springframework.cloud.client.loadbalancer.Response;
|
||||
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
|
||||
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
|
||||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
@@ -53,7 +61,7 @@ public class GrayRoundRobinLoadBalancer extends RoundRobinLoadBalancer {
|
||||
|
||||
// 注册中心无可用实例 抛出异常
|
||||
if (CollUtil.isEmpty(instances)) {
|
||||
log.warn("No instance available ");
|
||||
log.warn("No instance available serviceId: {}", serviceId);
|
||||
return new EmptyResponse();
|
||||
}
|
||||
|
||||
@@ -67,18 +75,24 @@ public class GrayRoundRobinLoadBalancer extends RoundRobinLoadBalancer {
|
||||
}
|
||||
|
||||
// 遍历可以实例元数据,若匹配则返回此实例
|
||||
for (ServiceInstance instance : instances) {
|
||||
List<ServiceInstance> serviceInstanceList = instances.stream().filter(instance -> {
|
||||
NacosServiceInstance nacosInstance = (NacosServiceInstance) instance;
|
||||
Map<String, String> metadata = nacosInstance.getMetadata();
|
||||
String targetVersion = MapUtil.getStr(metadata, CommonConstants.VERSION);
|
||||
if (reqVersion.equalsIgnoreCase(targetVersion)) {
|
||||
log.debug("gray requst match success :{} {}", reqVersion, nacosInstance);
|
||||
return new DefaultResponse(nacosInstance);
|
||||
}
|
||||
}
|
||||
return reqVersion.equalsIgnoreCase(targetVersion);
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
// 降级策略,使用轮询策略
|
||||
return super.choose(request).block();
|
||||
// 存在 随机返回
|
||||
if (CollUtil.isNotEmpty(serviceInstanceList)) {
|
||||
ServiceInstance instance = RandomUtil.randomEle(serviceInstanceList);
|
||||
|
||||
log.debug("gray instance available serviceId: {} , instanceId: {}", serviceId, instance.getInstanceId());
|
||||
return new DefaultResponse(instance);
|
||||
}
|
||||
else {
|
||||
// 不存在,降级策略,使用轮询策略
|
||||
return super.choose(request).block();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
"name": "gray.rule.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": false,
|
||||
"sourceType": "com.pig4cloud.pigx.common.gray.GrayRibbonLoadBalancerConfiguration",
|
||||
"sourceType": "com.pig4cloud.pigx.common.gray.GrayLoadBalancerAutoConfiguration",
|
||||
"description": "是否开启灰度路由功能"
|
||||
}
|
||||
],
|
||||
|
||||
Reference in new issue
Block a user