在 Jetty 上为 Spring Boot 启用 HTTP/2

Ran*_*Guy 5 java spring jetty spring-boot

我有一个带有嵌入式 jetty 的 Spring Boot 2.0.0 RC2,我想启用 HTTP/2。

根据有关如何为 jetty 启用 http/2 的文档,我添加server.http2.enabled=true到了我的apllication.properties

我的pom.xml看起来像这样:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RC2</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!-- Exclude the Tomcat dependency -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Use Jetty instead -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-alpn-conscrypt-server -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-alpn-conscrypt-server</artifactId>
            <version>9.4.8.v20171121</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.eclipse.jetty.http2/http2-server -->
        <dependency>
            <groupId>org.eclipse.jetty.http2</groupId>
            <artifactId>http2-server</artifactId>
            <version>9.4.8.v20171121</version>
        </dependency>

</dependencies>
Run Code Online (Sandbox Code Playgroud)

然后我启动它并执行一些 GET 请求(第一个是 HTTP1,第二个是 HTTP2)。这是输出:

2018-02-26 18:24:59.670  INFO 20338 --- [           main] o.s.b.web.embedded.jetty.JettyWebServer  : Jetty started on port(s) 8080 (http/1.1) with context path '/'
2018-02-26 18:24:59.674  INFO 20338 --- [           main] ru.example.vpndemo.VpnDemoApplication    : Started VpnDemoApplication in 4.772 seconds (JVM running for 5.45)
127.0.0.1 - - [26/Feb/2018:15:25:11 +0000] "GET /example/ HTTP/1.1" 200 13 
127.0.0.1 - - [26/Feb/2018:15:33:52 +0000] "PRI * HTTP/2.0" 426 0 
Run Code Online (Sandbox Code Playgroud)

为什么我的第二个请求PRI在控制台中打印?

以下是 HTTP1 请求的 cURL 输出:

$ curl http://localhost:8080/example/ -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /example/ HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Mon, 26 Feb 2018 15:37:03 GMT
< Content-Type: text/plain;charset=utf-8
< Content-Length: 13
< 
* Connection #0 to host localhost left intact
Hello Spring!
Run Code Online (Sandbox Code Playgroud)

这是 HTTP2 的输出:

$ curl --http2-prior-knowledge http://localhost:8080/example/ -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x1ac3f90)
> GET /example/ HTTP/2
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
> 
* Connection #0 to host localhost left intact
curl: (16) Error in the HTTP2 framing layer
Run Code Online (Sandbox Code Playgroud)

看起来 HTTP2 仍然没有启用。如果是这样,我如何为 Jetty 启用它(我知道,可以轻松为 Tomcat 或 Undertow 启用它)。

如果它已经启用,那么我做错了什么才能获得所需的输出?

有任何想法吗?

提前致谢!

UPD

为了确保我的 cURL 能够与 HTTP2 正常工作,我使用 Undertow HTTP2 启动了应用程序。这是输出:

$ curl --http2-prior-knowledge http://localhost:8080/example/ -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x2582f90)
> GET /example/ HTTP/2
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
> 
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200 
< content-type: text/plain;charset=UTF-8
< content-length: 13
< date: Mon, 26 Feb 2018 14:31:06 GMT
< 
* Connection #0 to host localhost left intact
Hello Spring!
Run Code Online (Sandbox Code Playgroud)

Joa*_*elt 1

序列 ...

PRI * HTTP/2.0

SM
Run Code Online (Sandbox Code Playgroud)

看起来像是"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"《HTTP/2 连接前言》

请参阅:https://www.rfc-editor.org/rfc/rfc7540#section-3.5

In HTTP/2, each endpoint is required to send a connection preface as
a final confirmation of the protocol in use and to establish the
initial settings for the HTTP/2 connection.  The client and server
each send a different connection preface.
Run Code Online (Sandbox Code Playgroud)