最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中如何獲取當(dāng)前服務(wù)器的IP地址

 更新時(shí)間:2023年07月13日 09:57:10   作者:18你磊哥  
這篇文章主要給大家介紹了關(guān)于Java中如何獲取當(dāng)前服務(wù)器的IP地址的相關(guān)資料,我們可以使用Java中的InetAddress類來獲取Linux服務(wù)器的IP地址,需要的朋友可以參考下

獲取ip的第一反應(yīng)就是:使用InetAddress這個(gè)類:方法如下

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)求都通過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;
     }

我的解決死方法(方法是死的,但是能解決問題^_^)

在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)文章

最新評(píng)論

贵溪市| 广灵县| 静海县| 阳东县| 车致| 娄烦县| 大港区| 安新县| 上虞市| 响水县| 安吉县| 公主岭市| 余江县| 阿鲁科尔沁旗| 乐亭县| 体育| 桦川县| 邮箱| 赤水市| 苍梧县| 彭山县| 南昌市| 时尚| 绥阳县| 长汀县| 中超| 桐庐县| 金昌市| 眉山市| 右玉县| 弥勒县| 富锦市| 屏东县| 紫阳县| 东源县| 九江县| 禄劝| 城固县| 合川市| 修水县| 邓州市|