在Jetty上为Spring Boot启用HTTP / 2

时间:2018-02-26 15:43:08

标签: 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!

1 个答案:

答案 0 :(得分:0)

对于通过maven依赖管理的http / 2和jetty应用程序服务器:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-alpn-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-alpn-conscrypt-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty.http2</groupId>
        <artifactId>http2-server</artifactId>
    </dependency>