【问题标题】:How to enabled http1.1 and http2.0 both together in a spring boot REST application in tomcat 9.0.14如何在tomcat 9.0.14的spring boot REST应用程序中同时启用http 1.1和http 2.0
【发布时间】:2019-06-23 16:25:23
【问题描述】:

我使用配置“server.http2.enabled=true”和 application.properties 文件中的其他 SSL 配置在带有嵌入式 tomcat 的 spring boot 2.1.2 REST 应用程序中启用了带有 SSL 的 http 2.0。它工作正常。当 http2 客户端发布 https 请求时,我的服务器正在正确处理和响应(正在使用 java 9 http 客户端)。但是当 http 客户端版本碰巧到 1.1 时,会看到以下异常 -

WARNING: Using incubator modules: jdk.incubator.httpclient
java.util.concurrent.ExecutionException: java.io.IOException: Engine is closed
    at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:395)
    at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2022)
    at KnHttp2Client.sendGet(KnHttp2Client.java:81)
    at KnHttp2Client.main(KnHttp2Client.java:113)
Caused by: java.io.IOException: Engine is closed

我尝试使用 apache-http-client4.5.7 发布 https 请求,但我仍然低于异常

Jan 30, 2019 4:45:12 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {s}->https://localhost:8443: The target server failed to respond
Jan 30, 2019 4:45:12 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://localhost:8443
org.apache.http.NoHttpResponseException: localhost:8443 failed to respond
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:141)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
    at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
    at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at apacheclient.KnConnPoolClient$1.run(KnConnPoolClient.java:48)
    at java.base/java.lang.Thread.run(Thread.java:844)


After setting setRetryHandler(new DefaultHttpRequestRetryHandler(0, true)) got below exception <br>
org.apache.http.NoHttpResponseException: localhost:8443 failed to respond
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:141)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
    at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
    at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at apacheclient.KnConnPoolClient$1.run(KnConnPoolClient.java:48)
    at java.base/java.lang.Thread.run(Thread.java:844)

使用 http 2.0 的工作客户端

{HttpClient httpClient = HttpClient.newBuilder()
            .version(HttpClient.Version.HTTP_2) 
            .build();
HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://localhost:8443/....."))
            .GET()
            .build();
CompletableFuture<HttpResponse<String>> resp = httpClient.sendAsync(request, HttpResponse.BodyHandler.asString()); }

当 http 客户端版本如下更改为 1.1 时,它不起作用

{HttpClient httpClient = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_1_1) 
                .build();}

下面的带有 Apache 客户端的客户端代码也不起作用

{
final CloseableHttpClient httpclient = 
HttpClients.custom().setConnectionManager(pool).build();
final String url = "https://localhost:8443/....";
    for (int i = 0; i < 1; i++) {
        new Thread(new Runnable() {

            public void run() {
                try {                        
                    HttpGet httpGet = new HttpGet(url);
                    CloseableHttpResponse response =httpclient.execute(httpGet);
                    HttpEntity entity = response.getEntity();
                    System.out.println(EntityUtils.toString(entity));                        
                    EntityUtils.consume(entity);
                    response.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    } 

我希望支持 http 2.0 的服务器也应该支持 http 1.1。

【问题讨论】:

    标签: spring-boot apache-httpclient-4.x http2


    【解决方案1】:

    这是 Spring Boot 中的一个已知问题,请参阅 issue #15764

    目前配置Tomcat时只支持http/2,但Jetty和Undertow都支持。在解决此问题之前,您可以切换到 Jetty 或 Undertow 作为解决方法。

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      相关资源
      最近更新 更多