拿到tomcat实例的ip和端口

001--情景:

集群环境,接口联调或者线上出问题,很难定位请求是发到了集群里的哪台机器。所以想在接口的返回值里加上tomcat实例的ip和端口

002--解决方案:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 * 系统类,为拿到tomcat实例的ip和端口
 * @author  邵亚东  2016年11月17日下午5:54:03
 */
public class OSUtil {
private static boolean isWindowsOS() {
        boolean isWindowsOS = false;
        String osName = System.getProperty("os.name");
        if (osName.toLowerCase().indexOf("windows") > -1) {
            isWindowsOS = true;
        }
        return isWindowsOS;
    }
private static String getLinuxLocalIp() throws Exception{
String ip = "";
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
String name = intf.getName();
if (!name.contains("docker") && !name.contains("lo")) {
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String ipaddress = inetAddress.getHostAddress().toString();
if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
ip = ipaddress;
}
}
}
}
}
        return ip;
    }
private static String getSelfIP() throws Exception {        
if (isWindowsOS()) {
       return InetAddress.getLocalHost().getHostAddress();
   } else {
       return getLinuxLocalIp();
   }
}


private static String getSelfPort() throws Exception {
String port = "";
DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
DocumentBuilder db = builder.newDocumentBuilder();


// 读取项目资源文件
String filePath = getServerXmlPath();
File file = new File(filePath);
InputStream is = new FileInputStream(file);
Document doc = db.parse(is);
Element root = doc.getDocumentElement();
NodeList list = root.getElementsByTagName("Connector");
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.hasAttributes()) {
NamedNodeMap map = node.getAttributes();
if (map.item(2).getNodeValue().equals("HTTP/1.1")) {
port = map.item(1).getNodeValue();
}
}
}
return port;
}


// 获取server.xml文件路径
private static String getServerXmlPath() throws Exception {
String filePath = "";
String classPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
classPath = classPath.replaceAll("%20", " ");
int fromIndex = classPath.indexOf("/WEB-INF") - 1;
int endIndex = classPath.lastIndexOf("/", fromIndex) - 1;
endIndex = classPath.lastIndexOf("/", endIndex) + 1;
filePath = classPath.substring(0, endIndex);
filePath += "conf/server.xml";
return filePath;
}
public static String getIpAndPort(){
String ip;
String port;
try {
ip = getSelfIP();
port = getSelfPort();
} catch (Exception e) {
return "获取ip:port失败";
}
return ip+":"+port;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值