ayisha farhin
ayisha farhin

Reputation: 81

Spring Boot 3 Micrometer Integration with RestTemplate

I have upgraded to spring boot 3. I am trying to instantiate metric binder with pool connection manager.

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionPerRoute());
connectionManager.setMaxTotal(config.getMaxConnectionPerRoute() * config.getNoOfRoutes());
CloseableHttpClient httpClient = HttpClientBuilder
        .create().setConnectionManager(connectionManager)
        .build();
new PoolingHttpClientConnectionManagerMetricsBinder(connectionManager, "my-pool").bindTo(registry);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
restTemplate = new RestTemplate(requestFactory);

The previously used package

org.apache.http.impl.conn.PoolingHttpClientConnectionManager

The upgraded package

import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;

This updated package is incompatible with PoolingHttpClientConnectionManagerMetricsBinder with issue: 'PoolingHttpClientConnectionManagerMetricsBinder(org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>, java.lang.String, java.lang.String...)' in 'io.micrometer.core.instrument.binder.httpcomponents.PoolingHttpClientConnectionManagerMetricsBinder' cannot be applied to '(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager, java.lang.String)'

I tried to update to the old package

org.apache.http.impl.conn.PoolingHttpClientConnectionManager

and resolve this, but this cause issue with HttpComponentsClientHttpRequestFactory

HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

as the packages are incompatible 'HttpComponentsClientHttpRequestFactory(org.apache.hc.client5.http.classic.HttpClient)' in 'org.springframework.http.client.HttpComponentsClientHttpRequestFactory' cannot be applied to '(org.apache.http.client.HttpClient)'

Need solution to resolve this or any alternative approach.

Upvotes: 5

Views: 5452

Answers (3)

Radu Sebastian LAZIN
Radu Sebastian LAZIN

Reputation: 580

You need to use the latest micrometer-core dependency too and in there there's a new PoolingHttpClientConnectionManagerMetricsBinder in io.micrometer.core.instrument.binder.httpcomponents.hc5 package.

Here are the compatible imports:

import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import io.micrometer.core.instrument.binder.httpcomponents.hc5.PoolingHttpClientConnectionManagerMetricsBinder;

...

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionPerRoute());
connectionManager.setMaxTotal(config.getMaxConnectionPerRoute() * config.getNoOfRoutes());
CloseableHttpClient httpClient = HttpClientBuilder
        .create().setConnectionManager(connectionManager)
        .build();
new PoolingHttpClientConnectionManagerMetricsBinder(connectionManager, "my-pool").bindTo(registry);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
restTemplate = new RestTemplate(requestFactory);

Also you could use the connection manager builder when constructing the connection manager and HttpClients class to build the client:

import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import io.micrometer.core.instrument.binder.httpcomponents.hc5.PoolingHttpClientConnectionManagerMetricsBinder;

...

PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
        .setMaxPerRoute(config.getMaxConnectionPerRoute());
        .setMaxConnTotal(config.getMaxConnectionPerRoute() * config.getNoOfRoutes())
        .build();
CloseableHttpClient httpClient = HttpClients.custom()
        .setConnectionManager(connectionManager)
        .build();
new PoolingHttpClientConnectionManagerMetricsBinder(connectionManager, "my-pool")
        .bindTo(registry);
restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));

Upvotes: 1

Nieznany Anonimiec
Nieznany Anonimiec

Reputation: 31

Seems that Micrometer 1.10.x has done it for httpclient5 but not released yet in 1.10.4 :( https://github.com/micrometer-metrics/micrometer/blob/main/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/httpcomponents/hc5/PoolingHttpClientConnectionManagerMetricsBinder.java Need to wait for next release maybe 1.10.5 will include that.

Upvotes: 1

Brian Clozel
Brian Clozel

Reputation: 59141

Both Spring Framework 6 and Spring Boot 3 removed support for HttpComponents 4.x (see this Spring Framework issue and this Spring Boot issue).

If you'd like to keep the connection pool metrics in your application, I would suggest to ask the Micrometer team if they would consider supporting the new version of httpcomponents. Given the latest changes in Micrometer, the HTTP client itself could ship its own, optional, Observability instrumentation like most Spring projects do now.

Upvotes: 2

Related Questions