咨询:Helidon MP RestClient是否支持发起HTTP/2请求?
Helidon MP RestClient 开启HTTP/2请求的配置方案
Helidon MP的RestClient支持通过微配置文件开启HTTP/2请求,无需完全依赖自定义代码实现,以下是具体配置方式:
1. 配置文件(microprofile-config.properties)方式
针对特定RestClient接口或全局启用HTTP/2:
- 针对单个RestClient接口配置(替换为你的接口全限定类名):
# 开启HTTP/2支持 com.example.MyRestClient/mp-rest/connection/http2.enabled=true # 指定协议版本为HTTP/2 com.example.MyRestClient/mp-rest/protocol=HTTP_2 - 全局配置(对所有RestClient生效):
mp-rest/connection/http2.enabled=true mp-rest/protocol=HTTP_2
2. 代码注解定制方式
如果需要更灵活的配置,可以通过@RestClientBuilderCustomizer定制RestClientBuilder:
@ApplicationScoped public class Http2RestClientCustomizer implements RestClientBuilderCustomizer { @Override public void customize(RestClientBuilder builder) { // 开启HTTP/2 builder.http2Enabled(true); // 可选:强制使用HTTP/2 builder.protocolVersion(ProtocolVersion.HTTP_2); } }
然后在你的RestClient接口上关联这个定制器:
@RegisterRestClient(configKey = "com.example.MyRestClient") @RestClientBuilderCustomizer(Http2RestClientCustomizer.class) public interface MyRestClient { // 接口方法定义 }
注意事项
- 确保目标服务端已启用HTTP/2支持,客户端开启HTTP/2后会与服务端进行协议协商,协商失败会降级到HTTP/1.1
- 建议使用Helidon 3.x及以上版本,该版本对HTTP/2的支持更成熟稳定
- 若配置后未生效,可开启Helidon HTTP客户端日志(设置
io.helidon.webclient.level=FINE),排查协议协商过程
内容的提问来源于stack exchange,提问作者Tom
相关产品推荐
相关产品推荐

