sis forum index.php,SiS001 获取当前服务器的Ip地址
sis forum index.php,SiS001 获取当前服务器的Ip地址
名片进入公众号【娱乐857】
即可获取完整版资料
public board static void main(String[] args) {
try {
//Enter.https://1024td.com @InetAddress的服务器地址
InetAddress address = InetAddress.getLocalHost();
System.out.println(address.getHostName());//网站主机名
System.out.println(address.getCanonicalHostName());//主机别名
System.out.println(address.getHostAddress());//获取亚洲IP地址
System.out.println("===============");
//用域名创建 InetAddress原创区对象
InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn");
//获取的是该网站的ip地址,如果我们所有的请求都通过nginx的,所以这里获取到的其实是nginx服务器的IP地址
System.out.println(address1.getHostName());//www.wodexiangce.cn
System.out.println(address1.getCanonicalHostName());//124.237.121.122
System.out.println(address1.getHostAddress());//124.237.121.122
System.out.println("===============");
//用IP地址创建InetAddress对象
InetAddress address2 = InetAddress.getByName("220.181.111.188");
System.out.println(address2.getHostName());//220.181.111.188
System.out.println(address2.getCanonicalHostName());//220.181.111.188
System.out.println(address2.getHostAddress());//220.181.111.188
System.out.println("===============");
//根据主机名返回其可能的所有InetAddress对象
InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");
for (InetAddress addr : addresses) {
System.out.println(addr);
//www.baidu.com/220.181.111.188
//www.baidu.com/220.181.112.244
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
复制代码
如果使用了反向代理,这种获取方式显然是不准确的,我们采用的方法是新建一个java类,里面配的是当前服务器的IP地址(也就是说这个类在每个节点服务器上部署的是不同的),程序里用的话直接获取这个工具类就可以了,虽然方法有点笨,但是解决问题了。
复制代码
public class CommonServerIP {
/**
* 这个类主要是保存常用的一些固定的服务器IP
*/
public static String CURRENT_SERVER="124.237.121.46";//当前服务器的ip地址
获取本地的Ip地址:
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
String hostAddress = address.getHostAddress());//192.168.0.121
InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn");//获取的是该网站的ip地址,比如我们所有的请求都通过nginx的,所以这里获取到的其实是nginx服务器的IP地
String hostAddress1 = address1.getHostAddress());//124.237.121.122
InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");//根据主机名返回其可能的所有InetAddress对象
for(InetAddress addr:addresses){
System.out.println(addr);//www.baidu.com/14.215.177.38
//www.baidu.com/14.215.177.37
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
获取服务器的Ip地址(其他人写的):
/**
* 获取服务器IP地址
* @return
*/
@SuppressWarnings("unchecked")
public static String getServerIp(){
String SERVER_IP = null;
try {
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
ip = (InetAddress) ni.getInetAddresses().nextElement();
SERVER_IP = ip.getHostAddress();
if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {
SERVER_IP = ip.getHostAddress();
break;
} else {
ip = null;
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SERVER_IP;
}
链接:https://1024td.com/p?1209
来源:资源库之家
著作权归作者所有。非商业转载注明出处。
更多推荐
所有评论(0)