Java中如何獲取當(dāng)前服務(wù)器的IP地址
獲取ip的第一反應(yīng)就是:使用InetAddress這個(gè)類(lèi):方法如下
InetAddress.getLocalHost().getHostAddress();
public static void main(String[] args) {
try {
//用 getLocalHost() 方法創(chuàng)建的InetAddress的對(duì)象
InetAddress address = InetAddress.getLocalHost();
System.out.println(address.getHostName());//主機(jī)名
System.out.println(address.getCanonicalHostName());//主機(jī)別名
System.out.println(address.getHostAddress());//獲取IP地址
System.out.println("===============");
//用域名創(chuàng)建 InetAddress對(duì)象
InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn");
//獲取的是該網(wǎng)站的ip地址,如果我們所有的請(qǐng)求都通過(guò)nginx的,所以這里獲取到的其實(shí)是nginx服務(wù)器的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地址創(chuàng)建InetAddress對(duì)象
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("===============");
//根據(jù)主機(jī)名返回其可能的所有InetAddress對(duì)象
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();
}
}可以知道此時(shí)獲取到的服務(wù)器如果加了代理方式就是獲取到代理的地址,一般會(huì)使用netty代理轉(zhuǎn)發(fā)。
/**
* 獲取服務(wù)器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;
}我的解決死方法(方法是死的,但是能解決問(wèn)題^_^)
在nacos的配置里面新建一個(gè)
constant.ipHost=服務(wù)器的ip
//獲取服務(wù)器的ip
@Value("${constant.ipHost}")
private String ipHost;總結(jié)
到此這篇關(guān)于Java中如何獲取當(dāng)前服務(wù)器的IP地址的文章就介紹到這了,更多相關(guān)Java獲取服務(wù)器IP地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java常用工具類(lèi) IP、File文件工具類(lèi)
這篇文章主要為大家詳細(xì)介紹了java常用工具類(lèi),包括IP、File文件工具類(lèi),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
解決springboot 無(wú)法配置多個(gè)靜態(tài)路徑的問(wèn)題
這篇文章主要介紹了解決springboot 無(wú)法配置多個(gè)靜態(tài)路徑的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
mybatis打印SQL,并顯示參數(shù)的實(shí)例
這篇文章主要介紹了mybatis打印SQL,并顯示參數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
java字符串轉(zhuǎn)JSON簡(jiǎn)單代碼示例
這篇文章主要給大家介紹了關(guān)于java字符串轉(zhuǎn)JSON的相關(guān)資料,JSON?是一種輕量級(jí)的數(shù)據(jù)交換格式,常用于Web應(yīng)用程序中的數(shù)據(jù)傳輸,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
SpringBoot最簡(jiǎn)潔的國(guó)際化配置
這篇文章主要介紹了SpringBoot最簡(jiǎn)潔的國(guó)際化配置,Spring Boot是一個(gè)用于構(gòu)建獨(dú)立的、生產(chǎn)級(jí)別的Spring應(yīng)用程序的框架,國(guó)際化是一個(gè)重要的功能,它允許應(yīng)用程序根據(jù)用戶(hù)的語(yǔ)言和地區(qū)顯示不同的內(nèi)容,在Spring Boot中,實(shí)現(xiàn)國(guó)際化非常簡(jiǎn)單,需要的朋友可以參考下2023-10-10
Spring?Boot項(xiàng)目中遇到`if-else`語(yǔ)句七種具體使用方法解析
當(dāng)在Spring?Boot項(xiàng)目中遇到大量if-else語(yǔ)句時(shí),優(yōu)化這些代碼變得尤為重要,因?yàn)樗鼈儾粌H增加了維護(hù)難度,還可能影響應(yīng)用程序的可讀性和性能,以下是七種具體的方法,用于在Spring?Boot項(xiàng)目中優(yōu)化和重構(gòu)if-else語(yǔ)句,感興趣的朋友一起看看吧2024-07-07

