2

I am using Spring WebClient and I would need to send my requests through a specific NIC (classic situation when you have two network adapters on the same computer).

I looked into the documentation to no avail, and the source code seems a bit complicated. How can I do that?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bruno Bossola
  • 1,128
  • 1
  • 13
  • 24
  • Sounds more like a network routing issue; there you can select packets to which server should be sent to what NIC. Not really a java issue, I'd say. – Frischling Jun 08 '18 at 07:36
  • In network comms, you can use "bind" together with "connect" to force your outgoing connection to use a specific address and port. In plain Java this is possible, while I was not able to find a way to do it with this library, hence the question. Hope this clarify. – Bruno Bossola Jun 08 '18 at 12:26
  • @Frischling you may be also interested to see how's done with apache http client: https://stackoverflow.com/questions/39136266/binding-network-interface-to-apache-httpclient – Bruno Bossola Jun 08 '18 at 12:28
  • Interesting :-) My instincts tell me, to avoid this, though - it is a kind of routing, and strongly linking your code to a NIC name (which even might change), is not so cool, I find. Depends on your organization, though. – Frischling Jun 08 '18 at 14:03
  • This is not about "linking your code to a NIC name", I am afraid. – Bruno Bossola Jun 08 '18 at 14:17
  • I meant "hardcoding", not "linking". sorry for that misnomer; can't help you, though, with the methods you'd like to have for Webclient. – Frischling Jun 11 '18 at 09:06

1 Answers1

0

At this time (Jun 2017) there's no nice solution to this problem. Internally, the API exists but it's not exposed to the external world.

The only viable solution is via reflection. Basically, you need to intercept the reactor HttpClient creation, and at that time:

  • collect the HttpClientOptions object from the "options" field of the HttpClient instance
  • collect the Bootstrap object from the "bootstrapTemplate" field of the HttpClientoptions instance
  • set the desired local address using the "localAddress" method of the Bootstrap instance

Hope this may help somebody, as it took me hours of debugging to understand the flow. You can see an example here, where we implemented this behavior. It's sad, but it's the only way at the moment.

Please do not downvote this answer in the future, when the API will be exposed.

Bruno Bossola
  • 1,128
  • 1
  • 13
  • 24