简体   繁体   English

来自不同源IP的HTTP请求(客户端)

[英]HTTP-Request from different source IP (client-side)

I have a machine which has five different IP-Addresses. 我有一台机器有五个不同的IP地址。 Is it possible to tell Java which IP-address should be used for HTTP-Requests (during runtime and client-side)? 是否有可能告诉Java哪个IP地址应该用于HTTP请求(在运行时和客户端)?

Example: First request -> first ip 示例:第一个请求 - >第一个ip

Second request -> second ip ... 第二次请求 - >第二次ip ...

Kind of a rotating IP-config. 一种旋转的IP配置。

The library Netty is available too, but I'm not forced to use it. 图书馆Netty也可用,但我没有被迫使用它。

Thanks in advance. 提前致谢。

It is possible if you create your own sockets, as one of the Socket constructors allows you to specify the local address as well as the remote address. 如果您创建自己的套接字是可能的,因为其中一个Socket构造函数允许您指定本地地址以及远程地址。 You might have to construct your own HTTP request and parse the response, though. 但是,您可能必须构造自己的HTTP请求并解析响应。

As best I can tell, the HttpUrlConnection class does not allow you to specify its local address, perhaps because it manages connections itself behind the scenes, so unfortunately it looks like you won't be able to take advantage of its HTTP and URL related convenience methods. 我可以说, HttpUrlConnection类不允许您指定其本地地址,可能是因为它在后台管理连接本身,所以不幸的是,您似乎无法利用其HTTP和URL相关的便利性方法。

You are trying to do client-side HTTP load balancing. 您正在尝试进行客户端HTTP负载平衡。 You can use Netflix Ribbon client library to get this done in a very configurable manner. 您可以使用Netflix Ribbon客户端库以非常可配置的方式完成此操作。 Its based on Netty, and is very performant when dealing with large number of connections for the client. 它基于Netty,在处理客户端的大量连接时非常高效。 Check this example . 检查此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM