1、获取Tomcat端口
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = null;
try {
objectNames = beanServer
.queryNames(new ObjectName("*:type=Connector,*"), Query
.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
} catch (MalformedObjectNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
port = objectNames.iterator().next().getKeyProperty("port");
System.out.println(port);
2、获取应用服务器IP地址
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
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")) {
System.out.println(ipaddress);
}
}
}
}
}