星空网 > 软件开发 > ASP.net

apache + tomcat 负载均衡模式下 getLocalAddr() 为null的解决办法

在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地址:

Java代码 apache + tomcat 负载均衡模式下 getLocalAddr() 为null的解决办法 apache + tomcat 负载均衡模式下 getLocalAddr() 为null的解决办法apache + tomcat 负载均衡模式下 getLocalAddr() 为null的解决办法

  1. String currentIp = InetAddress.getByName(request.getServerName()).getHostAddress();  

String currentIp = InetAddress.getByName(request.getServerName()).getHostAddress();

这个一直是获得负载服务器的IP
先通过request.getServerName()获取到访问域名,然后通过InetAddress.getByName(String serverName)解析到ip地址,但格式是 域名/ip 的形式,再通过getHostAddress()获取到ip地址。
实际上如果需要通过域名解析到ip,也可以通过这个函数来获取:

Java代码 apache + tomcat 负载均衡模式下 getLocalAddr() 为null的解决办法 apache + tomcat 负载均衡模式下 getLocalAddr() 为null的解决办法apache + tomcat 负载均衡模式下 getLocalAddr() 为null的解决办法

  1. InetAddress.getByName(域名).getHostAddress()  

InetAddress.getByName(域名).getHostAddress()

。       

 



原标题:apache + tomcat 负载均衡模式下 getLocalAddr() 为null的解决办法

关键词:Apache

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

最高奖励500万!深圳又要给跨境企业发钱了:https://www.kjdsnews.com/a/670553.html
谷歌工具指南-出海必备:https://www.kjdsnews.com/a/670554.html
【融资发布】MFS Africa完成超1亿美元C轮融资:https://www.kjdsnews.com/a/670555.html
亚马逊开放库存限制了?旺季补货不再发愁:https://www.kjdsnews.com/a/670556.html
Q4爆卖技巧!如何利用亚马逊捆绑销售抢流量?:https://www.kjdsnews.com/a/670557.html
80%亚马逊欧盟卖家都忽略了的欧盟B2B申报教程:https://www.kjdsnews.com/a/670558.html
七月份适合去日本旅游吗 7月份去日本哪里好玩:https://www.vstour.cn/a/363192.html
凤岗汽车站到松山湖风景区 松岗汽车站到凤凰山怎么坐车:https://www.vstour.cn/a/363193.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流