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 }