Просмотр исходного кода

【修改】修改本地调试环境服务转发规则

zhuzhoutong 5 лет назад
Родитель
Сommit
49a84e84b1

+ 18 - 1
framework-boot/src/main/java/com/mrxu/framework/boot/config/MetaFilterAutoConfiguration.java

@@ -1,7 +1,11 @@
 package com.mrxu.framework.boot.config;
 
 import com.mrxu.framework.boot.irule.MetadataAwareRule;
+import com.mrxu.framework.boot.util.SystemInfo;
+import com.netflix.appinfo.ApplicationInfoManager;
 import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.AutoConfigureBefore;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -20,7 +24,10 @@ import org.springframework.context.annotation.Configuration;
 @ConditionalOnClass(DiscoveryEnabledNIWSServerList.class)
 @AutoConfigureBefore(RibbonClientConfiguration.class)
 @ConditionalOnProperty(value = "spring.application.metadata.enabled", matchIfMissing = true)
-public class MetaFilterAutoConfiguration {
+public class MetaFilterAutoConfiguration implements InitializingBean {
+
+    @Autowired
+    private ApplicationInfoManager manager;
 
     /**
      * 配置全局的路由策略
@@ -31,4 +38,14 @@ public class MetaFilterAutoConfiguration {
     public MetadataAwareRule metadataAwareRule() {
         return new MetadataAwareRule();
     }
+
+    /**
+     * 每个服务启动后讲元数据信息加入server中,用于过滤
+     * 开发环境的调试机器码信息
+     * @throws Exception
+     */
+    @Override
+    public void afterPropertiesSet() throws Exception {
+        manager.getInfo().getMetadata().put("mac-addr", SystemInfo.getLocalMac());
+    }
 }

+ 10 - 16
framework-boot/src/main/java/com/mrxu/framework/boot/irule/MetadataAwarePredicate.java

@@ -1,17 +1,15 @@
 package com.mrxu.framework.boot.irule;
 
 import com.mrxu.framework.boot.util.SpringApplicationContextUtil;
+import com.mrxu.framework.boot.util.SystemInfo;
 import com.netflix.loadbalancer.AbstractServerPredicate;
 import com.netflix.loadbalancer.IRule;
 import com.netflix.loadbalancer.PredicateKey;
 import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang.StringUtils;
 import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
 
-import java.util.Iterator;
 import java.util.Map;
-import java.util.Set;
 
 /**
  * 功能概要:[服务合法判断逻辑] <br>
@@ -43,20 +41,16 @@ public class MetadataAwarePredicate extends AbstractServerPredicate {
             return false;
         }
         DiscoveryEnabledServer server = (DiscoveryEnabledServer) input.getServer();
-        Map<String, String> currentMetaDataInfo = eurekaInstanceConfigBean.getMetadataMap();
-        Map<String, String> metadata = server.getInstanceInfo().getMetadata();
-        if(currentMetaDataInfo==null||currentMetaDataInfo.size()<=0||metadata==null||metadata.size()<=0){
-            return false;
-        }
-        Set<Map.Entry<String, String>> curEntries = currentMetaDataInfo.entrySet();
-        Iterator<Map.Entry<String, String>> curit = curEntries.iterator();
-        while(curit.hasNext()){
-            Map.Entry<String, String> entry = curit.next();
-            if("management.port".equalsIgnoreCase(entry.getKey())){
-                continue;
+        String isOpenDevStr = eurekaInstanceConfigBean.getEnvironment().getProperty("spring.application.open-dev","false");
+        if("true".equalsIgnoreCase(isOpenDevStr)){
+            //本地开启调试模式,转发本机机器码的服务
+            String localMac = SystemInfo.getLocalMac();
+            Map<String, String> metadata = server.getInstanceInfo().getMetadata();
+            if(metadata==null||metadata.size()<=0||!metadata.keySet().contains("mac-addr")){
+                return false;
             }
-            String fetchValue = metadata.get(entry.getKey());
-            if(StringUtils.isBlank(fetchValue)||!fetchValue.equals(entry.getValue())){
+            String remoteMac = metadata.get("mac-addr");
+            if(!localMac.equalsIgnoreCase(remoteMac)){
                 return false;
             }
         }

+ 1 - 1
framework-boot/src/main/java/com/mrxu/framework/boot/util/SystemInfo.java

@@ -7,7 +7,7 @@ import java.net.NetworkInterface;
 
 public class SystemInfo {
 
-    private static String getLocalMac()  {
+    public static String getLocalMac()  {
         StringBuffer sb = new StringBuffer("");
         try {
             InetAddress ia =  InetAddress.getLocalHost();