1 year ago
#388339

PatPanda
Java Spring WebClient - send multiple http requests using requests one after another using HTTP2
Small question on how to send multiple requests using a Java Spring WebClient to a HTTP2 enabled server, leveraging HTTP2 as client please.
I have a Java List/Iterator of payloads I need to send one after another to a third party server. I am the client.
The third party server, which I have no control over, was first HTTP1 only.
Recently, they migrated to HTTP2, and they now fully support HTTP2.
When it was still HTTP1 enabled, I was using this code: (note the http1Client
vs http2Client
), here is what I am trying
private static Iterator<String> sendRequestsLeverageHttpTwo(Iterator<String> requestsPayload) {
List<String> linkedList = new LinkedList<>();
while (requestsPayload.hasNext()) {
String requestPayload = requestsPayload.next();
HttpClient http1Client = HttpClient.create().wiretap(true).protocol(HttpProtocol.HTTP11); // was using this when the server only supports HTTP1
HttpClient http2Client = HttpClient.create().wiretap(true).protocol(HttpProtocol.H2); //now using this as the server supports HTTP2
final WebClient webClient =
WebClient.create().mutate().defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE, HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.clientConnector(new ReactorClientHttpConnector(http2Client)).build(); //replace http1Client -> http2Client
final String response = webClient.post().uri("http://the-http-two-enabled-server/api").body(BodyInserters.fromValue(requestPayload)).retrieve().bodyToMono(String.class).block();
if (!"".equals(response)) {
linkedList.add("OK");
}
}
return linkedList.iterator();
}
The third party server is now supporting HTTP2, I swapped for the http2Client
.
Things "work", the third party server told me they are able to see in their access logs, all my requests are now HTTP2, happy.
The problem is, I realize on the client side, me, I am using HTTP2 here like HTTP1!
Meaning, candid as I am, I just swapped the HttpClient, and don't think I am leveraging any of HTTP2 technology.
Looking at this code, I am really just doing the same as before, but just with HttpProtocol.H2
. I do not think this is fully leveraging the step up from HTTP1 to HTTP2, please correct me if I am wrong.
My question is, how to properly fully leverage the step up HTTP2 while sending this collection of payloads to the HTTP2 enabled back end please?
Thank you
spring-webclient
reactor-netty
0 Answers
Your Answer