[url]http://www.51lingguang.com/?p=504[/url]
在apache + tomcat 负载均衡模式下,通过request.getLocalAddr()无法获取服务器端的ip地址,返回值为null的问题,查找了下,百度里没有有价值的回答,到官方网站上查找了下,说是:
The AJP protocol only passes the web server host name and port. It does not pass the IP address. Therefore, getLocalName() will return whatever is passed via AJP but getLocalAddr() will always return null.
Whilst getLocalAddr() could be modified to return the IP address Tomcat is listening on for AJP connections, I don’t like the inconsistency that would generate when compared to getLocalName() and getLocalPort().
因为AJP的转换,让java端获取不到服务器的ip地址,可以使用以下办法来获取服务器端的ip地址:
[color=red]这个一直是获得负载服务器的IP[/color]
先通过request.getServerName()获取到访问域名,然后通过InetAddress.getByName(String serverName)解析到ip地址,但格式是 域名/ip 的形式,再通过getHostAddress()获取到ip地址。
实际上如果需要通过域名解析到ip,也可以通过这个函数来获取:
在apache + tomcat 负载均衡模式下,通过request.getLocalAddr()无法获取服务器端的ip地址,返回值为null的问题,查找了下,百度里没有有价值的回答,到官方网站上查找了下,说是:
The AJP protocol only passes the web server host name and port. It does not pass the IP address. Therefore, getLocalName() will return whatever is passed via AJP but getLocalAddr() will always return null.
Whilst getLocalAddr() could be modified to return the IP address Tomcat is listening on for AJP connections, I don’t like the inconsistency that would generate when compared to getLocalName() and getLocalPort().
因为AJP的转换,让java端获取不到服务器的ip地址,可以使用以下办法来获取服务器端的ip地址:
String currentIp = InetAddress.getByName(request.getServerName()).getHostAddress();[color=red]这个一直是获得负载服务器的IP[/color]
先通过request.getServerName()获取到访问域名,然后通过InetAddress.getByName(String serverName)解析到ip地址,但格式是 域名/ip 的形式,再通过getHostAddress()获取到ip地址。
实际上如果需要通过域名解析到ip,也可以通过这个函数来获取:
InetAddress.getByName(域名).getHostAddress()。
文章详细介绍了在Apache+Tomcat负载均衡模式下,通过request.getLocalAddr()无法获取服务器端的ip地址的问题,并提供了一个解决办法:通过request.getServerName()获取访问域名,然后使用InetAddress.getByName()解析到ip地址。
6122

被折叠的 条评论
为什么被折叠?



