java - 在 Jetty 上为 Spring Boot 启用 HTTP/2

标签 java spring spring-boot jetty

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

根据 docs on how to enable http/2 for jetty我将 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>

然后我启动它并执行一些 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 

为什么我的第二个请求在控制台中打印为 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!

这里是 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

看起来 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!

最佳答案

顺序...

PRI * HTTP/2.0

SM

看起来像 "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.

关于java - 在 Jetty 上为 Spring Boot 启用 HTTP/2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48992061/

相关文章:

Java链表在第一个位置插入节点

java - 猜谜游戏中的随机数

java - Spring Rest 模板 - 响应主体为空

java - Spring中的线程命名

spring - 创建自动配置 spring 库到 spring-boot 应用程序

java - 获取当前的 Remote 或游戏 Controller

java - 在 servlet 中通过 Hibernate 连接数据库

Spring 3.0 与 Java EE 6.0

java - ExceptionMappingAuthenticationFailureHandler 没有捕获我的异常

spring - 无法在 spring 工具套件 4.2.1 中创建动态 Web 项目