【问题标题】:Switch spring-webflux microservice to http/2 (netty)将 spring-webflux 微服务切换到 http/2 (netty)
【发布时间】:2018-10-26 05:06:21
【问题描述】:

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

Spring Documentation 说:

您可以使用 server.http2.enabled 配置属性在 Spring Boot 应用程序中启用 HTTP/2 支持。这种支持取决于所选的 Web 服务器和应用程序环境,因为 JDK8 不支持开箱即用的协议。 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

请看一下我的配置:

HTTPS 也可以。 但是协议还是一样的(http/1.1)

.

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

【问题讨论】:

  • 你用什么浏览器?如果我使用 IE,我会注意到 HTTP1.1,chrome 支持 HTTP/2,并使用它(如果可用)。另外:查看日志,嵌入了 Tomcat,它告诉我必须包含本机库。

标签: netty http2 spring-webflux project-reactor alpn


【解决方案1】:

https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-http2

看来,我找到了答案。 Webflux Docs

目前 Spring WebFlux 不支持带有 Netty 的 HTTP/2。也不支持以编程方式向客户端推送资源。

【讨论】:

    【解决方案2】:

    Tomcat 嵌入式正在使用 h2。我想,Jetty 和 Undertow 也一样。所以实际上:每个支持的嵌入式容器,但 netty :-)

    【讨论】:

    • netty 并不是一个真正的容器,而是一个独立的网络库。 undertow 正在使用 Netty 作为其网络,顺便说一句。
    【解决方案3】:

    你可以把nginx放在webflux前面,配置nginx到listen 443 ssl http2;的配置行

    【讨论】:

      【解决方案4】:

      简而言之,它在 Spring Framework 5.1 中得到支持。 JDK1.8需要使用原生库来支持ALPN。

      Spring 文档中引用的声明具有误导性。

      Spring HTTP/2 wiki 页面 (https://github.com/spring-projects/spring-framework/wiki/HTTP-2-support) 有更多最新信息:

      反应堆网

      从 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 文件中包含 repo。
      2. 它使用 netty-tcnative-boringssl-static 原生库来支持 ALPN(JDK1.8 需要)

      【讨论】:

        猜你喜欢
        • 2019-03-18
        • 2020-12-19
        • 1970-01-01
        • 2021-09-03
        • 1970-01-01
        • 2022-11-20
        • 2019-10-22
        • 2019-04-29
        • 2019-04-24
        相关资源
        最近更新 更多