xujunwei 1 anno fa
parent
commit
52387c53d7

+ 2 - 3
framework-boot/src/main/java/com/mrxu/framework/boot/config/MetaFilterAutoConfiguration.java

@@ -1,6 +1,5 @@
 package com.mrxu.framework.boot.config;
 
-import cn.hutool.core.net.NetUtil;
 import com.mrxu.framework.boot.irule.MetadataAwareRule;
 import com.netflix.appinfo.ApplicationInfoManager;
 import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList;
@@ -50,7 +49,7 @@ public class MetaFilterAutoConfiguration implements InitializingBean {
      */
     @Override
     public void afterPropertiesSet() throws Exception {
-        manager.getInfo().getMetadata().put(MAC_KEY, NetUtil.getLocalMacAddress());
+        manager.getInfo().getMetadata().put(MAC_KEY, MetadataAwareRule.getMetaMac());
     }
 
-}
+}

+ 12 - 2
framework-boot/src/main/java/com/mrxu/framework/boot/irule/MetadataAwareRule.java

@@ -1,6 +1,7 @@
 package com.mrxu.framework.boot.irule;
 
 import cn.hutool.core.net.NetUtil;
+import cn.hutool.core.util.StrUtil;
 import com.mrxu.framework.boot.config.MetaFilterAutoConfiguration;
 import com.mrxu.framework.common.MrxuConst;
 import com.netflix.loadbalancer.ILoadBalancer;
@@ -23,7 +24,7 @@ public class MetadataAwareRule extends ZoneAvoidanceRule {
     public Server choose(Object key) {
         log.debug("开启微服务本地调试");
         ILoadBalancer lb = getLoadBalancer();
-        String localMac = NetUtil.getLocalMacAddress();
+        String localMac = getMetaMac();
         List<Server> upList = lb.getReachableServers();
         // 优先查找本机可用服务
         for(Server upServer : upList) {
@@ -60,4 +61,13 @@ public class MetadataAwareRule extends ZoneAvoidanceRule {
         //没有找到合适的服务使用默认规则
         return super.choose(key);
     }
-}
+
+    // 获取本地mac 机器唯一标记
+    public static String getMetaMac() {
+        String mac = NetUtil.getLocalMacAddress();
+        if(StrUtil.isEmpty(mac)) {
+            mac = NetUtil.getLocalhostStr();
+        }
+        return mac;
+    }
+}