首页 > 解决方案 > 如何在tomcat 9.0.14的spring boot REST应用程序中同时启用http1.1和http2.0

问题描述

我使用配置“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-bootapache-httpclient-4.xhttp2

解决方案


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

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


推荐阅读