Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

Java Spring Webflux WebClient - send http request using HTTP2

Small question regarding how to send HTTP2 requests using Webflux WebClient please.

One of our third party API did an upgrade, and they now support HTTP2.

Hence, I believe on my side, as a client, it is worth it to leverage this upgrade.

However, currently, I believe I am sending only HTTP1. To confirm, I saw the access log on the third party side, and it seems the requests are indeed HTTP1.

I have a WebClient bean, and using the bean to send requests.

WebClient.create().mutate().defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).clientConnector(new ReactorClientHttpConnector(HttpClient.create().wiretap(true).secure(sslContextSpec -> sslContextSpec.sslContext(getSslContext())))).build()

public Mono<ThirdPartyResponse> sendAnHTTP2RequestToThirdParty(ThirdPartyRequest request, Map<String, String> headers) {
        return webClient.mutate()
                .baseUrl(configuration.getThirdPartyUrl())
                .build()
                .post()
                .uri(ROUTE_ThirdParty)
                .headers(httpHeaders -> httpHeaders.setAll(headers))
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(request))
                .retrieve()
                .bodyToMono(ThirdPartyResponse.class);
    }

What are the configuration, or the code that I am missing please?

Thank you

question from:https://stackoverflow.com/questions/65947451/java-spring-webflux-webclient-send-http-request-using-http2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Response from Spring Team:

Reactor Netty provides two types of SslContext configuration:

https://projectreactor.io/docs/netty/release/api/reactor/netty/tcp/SslProvider.SslContextSpec.html

You can provide either SslContext or SslContextBuilder.

n your use case you provide SslContext for the HttpClient and thus we do not touch it. However there is no configuration for HTTP/2 support in that SslContext and in this case the client and the server will try to negotiate to use HTTP/1.1 protocol.

Solution:

Either extend SslContext with a configuration that supports HTTP/2 Or provide SslContextBuild and Reactor Netty will be able to apply some default HTTP/2 configuration

Verified and this is now rocking for me


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...