Switch spring-webflux microservice to http/2 (netty)
Asked Answered
S

4

7

Is there anyone who used spring-webflux with netty (http/2)?

Spring Documentation says:

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.

The flag server.http2.enabled is not working for me.

I'm using:

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

Please take a look at my config:

application.yml

HTTPS works as well. But the protocol is still the same (http/1.1)

enter image description here.

Is this a problem with ALPN? Should I upgrade my app to JDK10? I will appreciate any suggestions. Thanks.

Stander answered 16/5, 2018 at 8:28 Comment(1)
what browser do you use? I notice HTTP1.1 if I use IE, chrome supports HTTP/2, and uses it, if available. Also: Look at the logs, with Tomcat embedded it told me I had to include native libs.Centipoise
T
5

In short, it's supported in Spring Framework 5.1. With JDK1.8, you need to use native library for ALPN support.

The quoted statement from Spring document is misleading.

Spring HTTP/2 wiki page (https://github.com/spring-projects/spring-framework/wiki/HTTP-2-support) has more up-to-date info:

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.

Here is the pom.xml that works for me:

<?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>

Two key points:

  1. it uses spring-boot-starter-parent 2.1.0.BUILD-SNAPSHOT. If the release version is available, then you don't need to have the repo in the pom file.
  2. it uses netty-tcnative-boringssl-static native library to support ALPN (needed for JDK1.8)
Thickskinned answered 19/10, 2018 at 15:23 Comment(0)
S
4

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

Seems, I found the answer. Webflux Docs:

Currently Spring WebFlux does not support HTTP/2 with Netty. There is also no support for pushing resources programmatically to the client.

Stander answered 16/5, 2018 at 8:32 Comment(1)
Hey this, comment seems to be a little outdated when opening the official documentation.Cryptography
C
-1

Tomcat embedded is working with h2. Jetty and undertow, too, I think. So in effect: Every supported embedded container, but netty :-)

Centipoise answered 17/5, 2018 at 9:7 Comment(1)
netty isn't really a container, but a free-standing networking library. undertow is using Netty for its networking, btw.Scotney
B
-3

You could place nginx in front of webflux and configure nginx to configuration line of listen 443 ssl http2;

Backcross answered 17/5, 2018 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.