瀏覽代碼

Linux上 IP 获取问题处理

demon 8 年之前
父節點
當前提交
52275bcfca

+ 5 - 7
hos-broker/src/main/java/com/yihu/hos/common/scheduler/MonitorScheduler.java

@ -21,8 +21,6 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -153,12 +151,12 @@ public class MonitorScheduler {
        if (cursor.size() < 1) {
            try {
                Document result = new Document();
                InetAddress addr = null;
                addr = InetAddress.getLocalHost();
                result.put("name",  addr.getHostName());
                result.put("host",  addr.getHostAddress());
                String host = SigarUtil.getHost();
                String hostName = SigarUtil.getHostName();
                result.put("name",  hostName);
                result.put("host",  host);
                monoEnv.insert(MonitorConstant.HOST, result);
            } catch (UnknownHostException e) {
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

+ 16 - 19
hos-broker/src/main/java/com/yihu/hos/common/util/SigarUtil.java

@ -74,12 +74,23 @@ public class SigarUtil {
     * @throws UnknownHostException
     */
    public static String getHostName(){
        try {
            NetInfo netInfo = sigar.getNetInfo();
            String hostName = netInfo.getHostName();
            return hostName;
        } catch (SigarException e) {
            e.printStackTrace();
        }
        return "";
    }
    public static String getHost(){
        try {
            InetAddress addr = InetAddress.getLocalHost();
            String ip = addr.getHostAddress();
            NetInterfaceConfig netInterfaceConfig = sigar.getNetInterfaceConfig(null);
            String ip = netInterfaceConfig.getAddress();
            return ip;
        } catch (UnknownHostException e) {
        } catch (SigarException e) {
            e.printStackTrace();
        }
        return "unknowHost";
@ -304,26 +315,12 @@ public class SigarUtil {
        Map<String, Object> netMap = new HashMap<>();
        NetInterfaceConfig netConfig = null;
        try {
            netConfig = sigar.getNetInterfaceConfig();
            netConfig = sigar.getNetInterfaceConfig(null);
            NetInterfaceStat netIfStat = sigar.getNetInterfaceStat(netConfig.getName());
            // 取到当前机器的IP地址
            String address = null;
            try {
                address = InetAddress.getLocalHost().getHostAddress();
                InetAddress inet = InetAddress.getLocalHost();
                // 没有出现异常而正常当取到的IP时,如果取到的不是网卡循回地址时就返回
                // 否则再通过Sigar工具包中的方法来获取
                if (NetFlags.LOOPBACK_ADDRESS.equals(address)) {
                    address = netConfig.getAddress();
                }
            } catch (UnknownHostException e) {
                address = netConfig.getAddress();
            }
            Map<String, Object> bps = populate(netConfig.getName());//bps
            netMap.put("name", netConfig.getName());
            netMap.put("address", address);
            netMap.put("address", netConfig.getAddress());
            netMap.put("mac", netConfig.getHwaddr());
            netMap.put("type", netConfig.getType());
            netMap.put("broadcast", netConfig.getBroadcast());

+ 5 - 13
src/main/java/com/yihu/hos/monitor/service/ServerMonitorService.java

@ -11,9 +11,10 @@ import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by chenweida on 2016/1/27.
@ -78,16 +79,7 @@ public class ServerMonitorService {
        return actionResult;
    }
    public static String getHost(){
        try {
            InetAddress addr = InetAddress.getLocalHost();
            String ip = addr.getHostAddress();
            return ip;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "unknowHost";
    }
    public Result getHosts() {
        List<Map<String,Object>> result = new ArrayList<>();