6.27 HTTP/2 Support

Since Micronaut 2.x, Micronaut’s Netty-based HTTP server can be configured to support HTTP/2.

Configuring the Server for HTTP/2

The first step is to set the supported HTTP version in the server configuration:

Enabling HTTP/2 Support

  1. micronaut:
  2. server:
  3. http-version: 2.0

With this configuration, Micronaut enables support for the h2c protocol (see HTTP/2 over cleartext) which is fine for development.

Since browsers don’t support h2c and in general HTTP/2 over TLS (the h2 protocol), it is recommended for production that you enable HTTPS support. For development this can be done with:

Enabling h2 Protocol Support

  1. micronaut:
  2. ssl:
  3. enabled: true
  4. buildSelfSigned: true
  5. server:
  6. http-version: 2.0

For production, see the configuring HTTPS section of the documentation.

Note that if your deployment environment uses JDK 8, or for improved support for OpenSSL, define the following dependencies on Netty Tomcat Native:

  1. runtimeOnly("io.netty:netty-tcnative:2.0.40.Final")
  1. <dependency>
  2. <groupId>io.netty</groupId>
  3. <artifactId>netty-tcnative</artifactId>
  4. <version>2.0.40.Final</version>
  5. <scope>runtime</scope>
  6. </dependency>
  1. runtimeOnly("io.netty:netty-tcnative-boringssl-static:2.0.40.Final")
  1. <dependency>
  2. <groupId>io.netty</groupId>
  3. <artifactId>netty-tcnative-boringssl-static</artifactId>
  4. <version>2.0.40.Final</version>
  5. <scope>runtime</scope>
  6. </dependency>

In addition to a dependency on the appropriate native library for your architecture. For example:

Configuring Tomcat Native

  1. runtimeOnly "io.netty:netty-tcnative-boringssl-static:2.0.40.Final:${Os.isFamily(Os.FAMILY_MAC) ? 'osx-x86_64' : 'linux-x86_64'}"

See the documentation on Tomcat Native for more information.

HTTP/2 Clients

By default, Micronaut’s HTTP client is configured to support HTTP 1.1. To enable support for HTTP/2, set the supported HTTP version in configuration:

Enabling HTTP/2 in Clients

  1. micronaut:
  2. http:
  3. client:
  4. http-version: 2.0

Or by specifying the HTTP version to use when injecting the client:

Injecting a HTTP/2 Client

  1. @Inject
  2. @Client(httpVersion=HttpVersion.HTTP_2_0)
  3. ReactorHttpClient client;