panh 5 gadi atpakaļ
vecāks
revīzija
3297d60190

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

@@ -1,7 +1,7 @@
 package com.mrxu.framework.boot.config;
 package com.mrxu.framework.boot.config;
 
 
 import com.mrxu.framework.boot.irule.MetadataAwareRule;
 import com.mrxu.framework.boot.irule.MetadataAwareRule;
-import com.mrxu.framework.boot.util.SystemInfo;
+import com.mrxu.framework.boot.util.ComputerInfo;
 import com.netflix.appinfo.ApplicationInfoManager;
 import com.netflix.appinfo.ApplicationInfoManager;
 import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList;
 import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.beans.factory.InitializingBean;
@@ -46,6 +46,6 @@ public class MetaFilterAutoConfiguration implements InitializingBean {
      */
      */
     @Override
     @Override
     public void afterPropertiesSet() throws Exception {
     public void afterPropertiesSet() throws Exception {
-        manager.getInfo().getMetadata().put("mac-addr", SystemInfo.getLocalMac());
+        manager.getInfo().getMetadata().put("mac-addr", ComputerInfo.getMacAddress());
     }
     }
 }
 }

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

@@ -1,7 +1,7 @@
 package com.mrxu.framework.boot.irule;
 package com.mrxu.framework.boot.irule;
 
 
 import com.mrxu.framework.boot.util.SpringApplicationContextUtil;
 import com.mrxu.framework.boot.util.SpringApplicationContextUtil;
-import com.mrxu.framework.boot.util.SystemInfo;
+import com.mrxu.framework.boot.util.ComputerInfo;
 import com.netflix.loadbalancer.AbstractServerPredicate;
 import com.netflix.loadbalancer.AbstractServerPredicate;
 import com.netflix.loadbalancer.IRule;
 import com.netflix.loadbalancer.IRule;
 import com.netflix.loadbalancer.PredicateKey;
 import com.netflix.loadbalancer.PredicateKey;
@@ -44,7 +44,7 @@ public class MetadataAwarePredicate extends AbstractServerPredicate {
         String isOpenDevStr = eurekaInstanceConfigBean.getEnvironment().getProperty("spring.application.open-dev","false");
         String isOpenDevStr = eurekaInstanceConfigBean.getEnvironment().getProperty("spring.application.open-dev","false");
         if("true".equalsIgnoreCase(isOpenDevStr)){
         if("true".equalsIgnoreCase(isOpenDevStr)){
             //本地开启调试模式,转发本机机器码的服务
             //本地开启调试模式,转发本机机器码的服务
-            String localMac = SystemInfo.getLocalMac();
+            String localMac = ComputerInfo.getMacAddress();
             Map<String, String> metadata = server.getInstanceInfo().getMetadata();
             Map<String, String> metadata = server.getInstanceInfo().getMetadata();
             if(metadata==null||metadata.size()<=0||!metadata.keySet().contains("mac-addr")){
             if(metadata==null||metadata.size()<=0||!metadata.keySet().contains("mac-addr")){
                 return false;
                 return false;

+ 93 - 0
framework-boot/src/main/java/com/mrxu/framework/boot/util/ComputerInfo.java

@@ -0,0 +1,93 @@
+package com.mrxu.framework.boot.util;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class ComputerInfo {
+
+    private static String macAddressStr = null;
+    private static final String[] windowsCommand = { "ipconfig", "/all" };
+    private static final String[] linuxCommand = { "/sbin/ifconfig", "-a" };
+    private static final Pattern macPattern = Pattern.compile(".*((:?[0-9a-f]{2}[-:]){5}[0-9a-f]{2}).*", Pattern.CASE_INSENSITIVE);
+
+    /**
+     * 获取多个网卡地址
+     *
+     * @return
+     * @throws IOException
+     */
+    private final static List<String> getMacAddressList() throws IOException {
+        final ArrayList<String> macAddressList = new ArrayList<String>();
+        final String os = System.getProperty("os.name");
+        final String command[];
+
+        if (os.startsWith("Windows")) {
+            command = windowsCommand;
+        }
+        else if (os.startsWith("Linux") || os.startsWith("Mac")) {
+            command = linuxCommand;
+        }
+        else {
+            throw new IOException("Unknow operating system:" + os);
+        }
+        // 执行命令
+        final Process process = Runtime.getRuntime().exec(command);
+
+        BufferedReader bufReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+        for (String line = null; (line = bufReader.readLine()) != null;) {
+            Matcher matcher = macPattern.matcher(line);
+            if (matcher.matches()) {
+                macAddressList.add(matcher.group(1));
+                // macAddressList.add(matcher.group(1).replaceAll("[-:]",
+                // ""));//去掉MAC中的“-”
+            }
+        }
+        process.destroy();
+        bufReader.close();
+        return macAddressList;
+    }
+
+    /**
+     * 获取一个网卡地址(多个网卡时从中获取一个)
+     *
+     * @return
+     */
+    public static String getMacAddress() {
+        if (macAddressStr == null || macAddressStr.equals("")) {
+            StringBuffer sb = new StringBuffer(); // 存放多个网卡地址用,目前只取一个非0000000000E0隧道的值
+            try {
+                List<String> macList = getMacAddressList();
+                for (Iterator<String> iter = macList.iterator(); iter.hasNext();) {
+                    String amac = iter.next();
+                    if (!amac.equals("0000000000E0")) {
+                        sb.append(amac);
+                        break;
+                    }
+                }
+            }
+            catch (IOException e) {
+                e.printStackTrace();
+            }
+            macAddressStr = sb.toString();
+
+        }
+        return macAddressStr;
+    }
+
+
+    /**
+     * 限制创建实例
+     */
+    private ComputerInfo() {
+    }
+
+    public static void main(String[] args) {
+        System.out.println(getMacAddress());
+    }
+}

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

@@ -1,40 +0,0 @@
-package com.mrxu.framework.boot.util;
-
-import com.mrxu.framework.boot.entity.BusinessException;
-
-import java.net.InetAddress;
-import java.net.NetworkInterface;
-
-public class SystemInfo {
-
-    public static String getLocalMac()  {
-        StringBuffer sb = new StringBuffer("");
-        try {
-            InetAddress ia =  InetAddress.getLocalHost();
-            //获取网卡,获取地址
-            byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
-            for(int i=0; i<mac.length; i++) {
-                if(i!=0) {
-                    sb.append("-");
-                }
-                //字节转换为整数
-                int temp = mac[i]&0xff;
-                String str = Integer.toHexString(temp);
-                if(str.length()==1) {
-                    sb.append("0"+str);
-                }
-                else {
-                    sb.append(str);
-                }
-            }
-        } catch (Exception e) {
-            throw new BusinessException("获取mac异常"+e.getMessage());
-        }
-
-        return sb.toString().toUpperCase();
-    }
-
-    public static void main(String[] args) {
-        System.out.println(getLocalMac());
-    }
-}