java中獲取當前服務器地址主要使用到InetAddress這個類
1 public static void main(String[] args) { 2 try { 3 //用 getLocalHost() 方法創建的InetAddress的對象 4 InetAddress address = InetAddress.getLocalHost(); 5 System.out.println(address.getHostName());//主機名 6 System.out.println(address.getCanonicalHostName());//主機別名 7 System.out.println(address.getHostAddress());//獲取IP地址 8 System.out.println("==============="); 9 10 //用域名創建 InetAddress對象 11 InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn"); 12 //獲取的是該網站的ip地址,如果我們所有的請求都通過nginx的,所以這里獲取到的其實是nginx服務器的IP地址 13 System.out.println(address1.getHostName());//www.wodexiangce.cn 14 System.out.println(address1.getCanonicalHostName());//124.237.121.122 15 System.out.println(address1.getHostAddress());//124.237.121.122 16 System.out.println("==============="); 17 18 //用IP地址創建InetAddress對象 19 InetAddress address2 = InetAddress.getByName("220.181.111.188"); 20 System.out.println(address2.getHostName());//220.181.111.188 21 System.out.println(address2.getCanonicalHostName());//220.181.111.188 22 System.out.println(address2.getHostAddress());//220.181.111.188 23 System.out.println("==============="); 24 25 //根據主機名返回其可能的所有InetAddress對象 26 InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com"); 27 for (InetAddress addr : addresses) { 28 System.out.println(addr); 29 //www.baidu.com/220.181.111.188 30 //www.baidu.com/220.181.112.244 31 } 32 } catch (UnknownHostException e) { 33 e.printStackTrace(); 34 } 35 }
如果使用了反向代理,這種獲取方式顯然是不准確的,我們采用的方法是新建一個java類,里面配的是當前服務器的IP地址(也就是說這個類在每個節點服務器上部署的是不同的),程序里用的話直接獲取這個工具類就可以了,雖然方法有點笨,但是解決問題了。
1 public class CommonServerIP { 2 /** 3 * ####################################################################### 4 * ###############這個類主要是保存常用的一些固定的服務器IP##################### 5 * ####################################################################### 6 */ 7 public static String CURRENT_SERVER="124.237.121.46";//當前服務器的ip地址 8 }
根據HttpServletRequest對象獲取ip
1 public String getIpAddress(HttpServletRequest request) { 2 // 獲取請求主機IP地址,如果通過代理進來,則透過防火牆獲取真實IP地址 3 try { 4 String ip = request.getHeader("X-Forwarded-For"); 5 6 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 7 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 8 ip = request.getHeader("Proxy-Client-IP"); 9 } 10 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 11 ip = request.getHeader("WL-Proxy-Client-IP"); 12 } 13 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 14 ip = request.getHeader("HTTP_CLIENT_IP"); 15 } 16 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 17 ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 18 } 19 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 20 ip = request.getRemoteAddr(); 21 } 22 } else if (ip.length() > 15) { 23 String[] ips = ip.split(","); 24 for (int index = 0; index < ips.length; index++) { 25 String strIp = ips[index]; 26 if (!("unknown".equalsIgnoreCase(strIp))) { 27 ip = strIp; 28 break; 29 } 30 } 31 } 32 return ip; 33 } catch (Exception e) { 34 e.printStackTrace(); 35 return ""; 36 } 37 }