7.1.5 Configuring HTTP clients

Global Configuration for All Clients

The default HTTP client configuration is a Configuration Properties named DefaultHttpClientConfiguration that allows configuring the default behaviour for all HTTP clients. For example, in application.yml:

Altering default HTTP client configuration

  1. micronaut:
  2. http:
  3. client:
  4. read-timeout: 5s

The above example sets the readTimeout property of the HttpClientConfiguration class.

Client Specific Configuration

To have separate configuration per-client, there are a couple of options. You can configure Service Discovery manually in application.yml and apply per-client configuration:

Manually configuring HTTP services

  1. micronaut:
  2. http:
  3. services:
  4. foo:
  5. urls:
  6. - http://foo1
  7. - http://foo2
  8. read-timeout: 5s (1)
1The read timeout is applied to the foo client.

WARN: This client configuration can be used in conjunction with the @Client annotation, either by injecting an HttpClient directly or use on a client interface. In any case, all other attributes on the annotation will be ignored except the service id.

Then, inject the named client configuration:

Injecting an HTTP client

  1. @Client("foo") @Inject ReactorHttpClient httpClient;

You can also define a bean that extends from HttpClientConfiguration and ensure that the javax.inject.Named annotation names it appropriately:

Defining an HTTP client configuration bean

  1. @Named("twitter")
  2. @Singleton
  3. class TwitterHttpClientConfiguration extends HttpClientConfiguration {
  4. public TwitterHttpClientConfiguration(ApplicationConfiguration configuration) {
  5. super(configuration);
  6. }
  7. }

This configuration will be picked up if you inject a service named twitter with @Client using Service Discovery:

Injecting an HTTP client

  1. @Client("twitter") @Inject ReactorHttpClient httpClient;

Alternatively, if you don’t use service discovery you can use the configuration member of @Client to refer to a specific type:

Injecting an HTTP client

  1. @Client(value = "https://api.twitter.com/1.1",
  2. configuration = TwitterHttpClientConfiguration.class)
  3. @Inject
  4. ReactorHttpClient httpClient;

Using HTTP Client Connection Pooling

A client that handles a significant number of requests will benefit from enabling HTTP client connection pooling. The following configuration enables pooling for the foo client:

Manually configuring HTTP services

  1. micronaut:
  2. http:
  3. services:
  4. foo:
  5. urls:
  6. - http://foo1
  7. - http://foo2
  8. pool:
  9. enabled: true (1)
  10. max-connections: 50 (2)
1Enables the pool
2Sets the maximum number of connections in the pool

See the API for ConnectionPoolConfiguration for details on available pool configuration options.

Configuring Event Loop Groups

By default, Micronaut shares a common Netty EventLoopGroup for worker threads and all HTTP client threads.

This EventLoopGroup can be configured via the micronaut.netty.event-loops.default property:

Configuring The Default Event Loop

  1. micronaut:
  2. netty:
  3. event-loops:
  4. default:
  5. num-threads: 10
  6. prefer-native-transport: true

You can also use the micronaut.netty.event-loops setting to configure one or more additional event loops. The following table summarizes the properties:

🔗

Table 1. Configuration Properties for DefaultEventLoopGroupConfiguration
PropertyTypeDescription

micronaut.netty.event-loops..num-threads

int

micronaut.netty.event-loops..io-ratio

java.lang.Integer

micronaut.netty.event-loops..prefer-native-transport

boolean

micronaut.netty.event-loops..executor

java.lang.String

micronaut.netty.event-loops..shutdown-quiet-period

java.time.Duration

micronaut.netty.event-loops..shutdown-timeout

java.time.Duration

For example, if your interactions with an HTTP client involve CPU-intensive work, it may be worthwhile configuring a separate EventLoopGroup for one or all clients.

The following example configures an additional event loop group called “other” with 10 threads:

Configuring Additional Event Loops

  1. micronaut:
  2. netty:
  3. event-loops:
  4. other:
  5. num-threads: 10
  6. prefer-native-transport: true

Once an additional event loop has been configured you can alter the HTTP client configuration to use it:

Altering the Event Loop Group used by Clients

  1. micronaut:
  2. http:
  3. client:
  4. event-loop-group: other