繁体   English   中英

在 Jetty 上为 Spring Boot 启用 HTTP/2

[英]Enable HTTP/2 for Spring Boot on Jetty

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

有关如何启用HTTP / 2文档,以便码头我加server.http2.enabled=trueapllication.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 Connection Preface”

请参阅: https : //tools.ietf.org/html/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.

对于通过 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM