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;
}
}