为 HTTP/2.0 配置 Tomcat 8 以进行 HTTP(不是 https)调用

问题描述 投票:0回答:1

我们的旧 Tomcat 服务器版本为

8.5.81

我们计划升级到 HTTP/2,以获得更快的 api 响应时间(优化)

以下配置已应用于

server.xml

        <Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol"
                   connectionTimeout="20000"
                   redirectPort="8443">
            <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        </Connector>

但是调用仍然保留在 HTTP/1.1 上

需要帮助..

  1. 还需要做哪些改变才能在服务器上实现 HTTP/2?
  2. 我们的大部分电话都是通过 8080(http) 进行的。 443(https) 上的流量非常少。我的互联网搜索建议了一些示例,其中 HTTP2 似乎仅适用于 Tomcat 8.5 系列的 HTTPS。 如何为 HTTP 和 HTTPS 实现此目的?
java spring http tomcat http2
1个回答
0
投票

请求必须明确请求 HTTP/2 协议,可以通过 HTTP/1.1 协议升级或直接

协议升级:
要求:

GET /sample/hello HTTP/1.1

标题:
Connection: Upgrade, HTTP2-Settings

curl --verbose --http2 http://localhost:8080/sample/hello
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /sample/hello HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.0.1
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAAQCAAAAAAIAAAAA
> 
< HTTP/1.1 101 
< Connection: Upgrade
< Upgrade: h2c
< Date: Thu, 24 Aug 2023 18:11:37 GMT
* Received 101, Switching to HTTP/2
< HTTP/2 200 
< content-type: text/html;charset=ISO-8859-1
< content-length: 311
< date: Thu, 24 Aug 2023 18:11:37 GMT
(response redacted)

直接请求
要求:

GET /sample/hello HTTP/2

 curl --verbose --http2-prior-knowledge http://localhost:8080/sample/hello
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
* h2h3 [:method: GET]
* h2h3 [:path: /sample/hello]
* h2h3 [:scheme: http]
* h2h3 [:authority: localhost:8080]
* h2h3 [user-agent: curl/8.0.1]
* h2h3 [accept: */*]
* Using Stream ID: 1 (easy handle 0x558d436ecf40)
> GET /sample/hello HTTP/2
> Host: localhost:8080
> user-agent: curl/8.0.1
> accept: */*
> 
< HTTP/2 200 
< content-type: text/html;charset=ISO-8859-1
< content-length: 311
< date: Thu, 24 Aug 2023 18:14:01 GMT
< 
(response redacted)

如果没有明确请求 HTTP/2,将提供 HTTP/1.1

curl --verbose http://localhost:8080/sample/hello
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /sample/hello HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.0.1
> Accept: */*
> 
< HTTP/1.1 200 
< Content-Type: text/html;charset=ISO-8859-1
< Content-Length: 311
< Date: Thu, 24 Aug 2023 18:18:39 GMT
< 

使用此配置进行测试

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443">
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
    </Connector>
© www.soinside.com 2019 - 2024. All rights reserved.