|
|
@@ -1,8 +1,16 @@
|
|
|
package com.mrxu.framework.boot.irule;
|
|
|
|
|
|
-import com.netflix.loadbalancer.AbstractServerPredicate;
|
|
|
-import com.netflix.loadbalancer.CompositePredicate;
|
|
|
+import com.google.common.base.Optional;
|
|
|
+import com.mrxu.framework.boot.util.ComputerInfo;
|
|
|
+import com.mrxu.framework.boot.util.SpringApplicationContextUtil;
|
|
|
+import com.netflix.loadbalancer.ILoadBalancer;
|
|
|
+import com.netflix.loadbalancer.Server;
|
|
|
import com.netflix.loadbalancer.ZoneAvoidanceRule;
|
|
|
+import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
|
|
|
+import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 功能概要:[原信息过滤器,根据原信息过滤合法的服务,可用于开发调试和版本过滤] <br>
|
|
|
@@ -12,33 +20,36 @@ import com.netflix.loadbalancer.ZoneAvoidanceRule;
|
|
|
*/
|
|
|
public class MetadataAwareRule extends ZoneAvoidanceRule {
|
|
|
|
|
|
- /**
|
|
|
- * 过滤服务逻辑
|
|
|
- */
|
|
|
- private CompositePredicate compositePredicate;
|
|
|
-
|
|
|
- public MetadataAwareRule() {
|
|
|
- ZoneAvoidanceRule zoneAvoidanceRule = new ZoneAvoidanceRule();
|
|
|
- MetadataAwarePredicate metadataAwarePredicate = new MetadataAwarePredicate(this);
|
|
|
- compositePredicate = createCompositePredicate(metadataAwarePredicate,zoneAvoidanceRule.getPredicate());
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
- public AbstractServerPredicate getPredicate() {
|
|
|
- return compositePredicate;
|
|
|
- }
|
|
|
+ public Server choose(Object key) {
|
|
|
+ ILoadBalancer lb = getLoadBalancer();
|
|
|
+ //是注册中心中的服务时,判断过滤逻辑
|
|
|
+ EurekaInstanceConfigBean eurekaInstanceConfigBean = SpringApplicationContextUtil.getBean(EurekaInstanceConfigBean.class);
|
|
|
+ if(eurekaInstanceConfigBean != null){
|
|
|
+ String isOpenDevStr = eurekaInstanceConfigBean.getEnvironment().getProperty("spring.application.open-dev","false");
|
|
|
+ if("true".equalsIgnoreCase(isOpenDevStr)){
|
|
|
+ //本地开启调试模式,转发本机机器码的服务
|
|
|
+ String localMac = ComputerInfo.getMacAddress();
|
|
|
+ List<Server> upList = lb.getReachableServers();
|
|
|
+ for(Server upServer : upList){
|
|
|
+ DiscoveryEnabledServer server = (DiscoveryEnabledServer) upServer;
|
|
|
+ Map<String, String> metadata = server.getInstanceInfo().getMetadata();
|
|
|
+ if(metadata!=null&&metadata.size()>0&&metadata.keySet().contains("mac-addr")){
|
|
|
+ String remoteMac = metadata.get("mac-addr");
|
|
|
+ if(localMac.equalsIgnoreCase(remoteMac)){
|
|
|
+ return upServer;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 优先p0选择,再使用原始的选择逻辑
|
|
|
- * @param p0 原信息选择
|
|
|
- * @param p1 地区地域、可用服务
|
|
|
- * @return 组合选择逻辑
|
|
|
- */
|
|
|
- private CompositePredicate createCompositePredicate(MetadataAwarePredicate p0,AbstractServerPredicate p1) {
|
|
|
- return CompositePredicate.withPredicates(p0, p1)
|
|
|
- .addFallbackPredicate(p1)
|
|
|
- .addFallbackPredicate(AbstractServerPredicate.alwaysTrue())
|
|
|
- .build();
|
|
|
+ //没有找到合适的服务
|
|
|
+ Optional<Server> server = getPredicate().chooseRoundRobinAfterFiltering(lb.getAllServers(), key);
|
|
|
+ if (server.isPresent()) {
|
|
|
+ return server.get();
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
}
|