Altering default HTTP client configuration

    The above example sets of readTimeout property of the class.

    If you wish to have a separate configuration per client then there 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://foo2
    7. read-timeout: 5s (1)

    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 other than the service id.

    Then simply inject the named client configuration:

    Injecting an HTTP client

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

    You can also simply define a bean that extends from and ensuring that the annotation is used to name it appropriately:

    Defining an HTTP client configuration bean

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

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

    Alternatively if you are not using service discovery then 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. RxHttpClient httpClient;

    If you have a client that needs to handle a significant number of requests then you can benefit from enabling HTTP client connection pooling. The following configuration will enable pooling for the foo client:

    Manually configuring HTTP services

    See the API for for details on available options to configure the pool.

    By default Micronaut will share a common Netty EventLoopGroup for worker threads and all HTTP clients threads.

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

    Configuring The Default Event Loop

    1. micronaut:
    2. event-loops:
    3. default:
    4. num-threads: 10
    5. 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:

    🔗

    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” that uses 10 threads:

    Configuring Additional Event Loops

    1. micronaut:
    2. netty:
    3. event-loops:
    4. other:

    Altering the Event Loop Group used by Clients