netty - 将 spring-webflux 微服务切换到 http/2 (netty)

标签 netty http2 spring-webflux project-reactor alpn

有没有人在 netty (http/2) 中使用 spring-webflux?

Spring Documentation说:

You can enable HTTP/2 support in your Spring Boot application with the server.http2.enabled configuration property. This support depends on the chosen web server and the application environment, since that protocol is not supported out-of-the-box by JDK8. Spring Boot does not support h2c, the cleartext version of the HTTP/2 protocol. So you must configure SSL first.



旗帜server.http2.enabled不适合我。

我正在使用:
  • JDK8
  • org.springframework.boot:spring-boot-starter-parent:2.0.2.RELEASE
  • Netty 4.1.24.Final

  • 请看一下我的配置:

    application.yml

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

    enter image description here .

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

    最佳答案

    简而言之,它在 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

    As of Spring Framework 5.1 (Reactor Netty 0.8), this server supports as well HTTP/2. JDK9+ deployments will support that protocol without specific infrastructure changes.

    For JDK 8 environments, or for optimal runtime performance, this server also supports HTTP/2 with native libraries. To enable that, your application needs to have an additional dependency.


    这是对我有用的 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>
    

    两个关键点:
  • 它使用 spring-boot-starter-parent 2.1.0.BUILD-SNAPSHOT。如果发布版本可用,那么您不需要在 pom 文件中包含 repo。
  • 它使用 netty-tcnative-boringssl-static native 库来支持 ALPN(JDK1.8 需要)
  • 关于netty - 将 spring-webflux 微服务切换到 http/2 (netty),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50365877/

    相关文章:

    spring - 如何记录 Spring WebClient 响应

    multithreading - 经典与响应式(Reactive)方法中的连接、请求和线程

    node.js - 为什么请求事件被触发两次

    openshift - 我可以在 OpenShift 上启用 HTTP/2 吗?

    java - Spring WebFlux : How to access Request Body in HandlerFilterFunction

    java - 如何在 Java webflux webclient 请求中禁用安全证书检查

    java - 如何阅读 Netty 日志

    java - Netty 示例中 ctx.read() 与 ctx.channel.read() 的使用

    netty - 如何在 Netty 3.2.7 中实现 ObjectDecoder(ClassResolver)

    performance - 使用 HTTP2 衡量性能提升