如何在tomcat 9.0.14的spring boot REST应用程序中同时启用http1.1和http2.0

时间:2019-01-30 11:37:57

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

我已在带有嵌入式tomcat的Spring Boot 2.1.2 REST应用程序的春季启动2.1.2 REST应用程序中使用配置“ server.http2.enabled = true”和application.properties文件中的其他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。

1 个答案:

答案 0 :(得分:1)

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

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