将Spring WebFlux微服务切换到HTTP/2(Netty)

7

有人使用过spring-webflux和netty(http/2)吗?

Spring Documentation说:

您可以通过server.http2.enabled配置属性在Spring Boot应用程序中启用HTTP/2支持。由于该协议不是JDK8的开箱即用功能,因此此支持取决于所选的Web服务器和应用程序环境。Spring Boot不支持h2c,即HTTP/2协议的明文版本。因此,您必须先配置SSL。

server.http2.enabled标志对我无效。

我正在使用:

  1. JDK8
  2. org.springframework.boot:spring-boot-starter-parent:2.0.2.RELEASE
  3. Netty 4.1.24.Final

请查看我的配置:

application.yml

HTTPS同样有效。但协议仍然是相同的(http/1.1)。

enter image description here.

这是ALPN的问题吗?我需要将我的应用程序升级到JDK10吗?我将感激任何建议。谢谢。


你用什么浏览器?我注意到如果我使用IE,它会使用HTTP1.1,而Chrome支持HTTP/2并在可用时使用它。另外:查看日志,使用嵌入式Tomcat时,它告诉我必须包含本地库。 - Frischling
4个回答

5
简而言之,它在Spring Framework 5.1中得到支持。对于JDK1.8,您需要使用本地库来支持ALPN。
Spring文档中引用的声明是误导性的。
Spring HTTP/2 wiki页面(https://github.com/spring-projects/spring-framework/wiki/HTTP-2-support)有更多最新信息:
Reactor Netty
从Spring Framework 5.1(Reactor Netty 0.8)开始,该服务器也支持HTTP/2。JDK9+部署将支持该协议而无需特定的基础设施更改。
对于JDK 8环境或实现最佳运行时性能,该服务器还支持具有本地库的HTTP/2。要启用它,您的应用程序需要有额外的依赖项。
以下是适用于我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.BUILD-SNAPSHOT</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-tcnative-boringssl-static</artifactId>
        <version>2.0.17.Final</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots><enabled>true</enabled></snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <url>https://repo.spring.io/milestone</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>https://repo.spring.io/snapshot</url>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <url>https://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>

两个关键点:

  1. 它使用spring-boot-starter-parent 2.1.0.BUILD-SNAPSHOT。如果发布版本可用,则不需要在pom文件中有该存储库。
  2. 它使用netty-tcnative-boringssl-static本地库来支持ALPN(JDK1.8所需)

4

嘿,这个注释在打开官方文档时似乎有点过时了。 - Zebedeu

-1

Tomcat嵌入式可以与h2一起使用。Jetty和Undertow也可以,我认为。所以实际上:除了Netty之外的每个支持的嵌入式容器 :-)


Netty并不是一个容器,而是一个独立的网络库。顺便说一下,Undertow正在使用Netty进行网络通信。 - oligofren

-3
您可以将nginx放在webflux前面,并配置nginx的配置行:listen 443 ssl http2;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接