Skip to content

setting connection pool size for restclient group service spring 4 #36783

@alinayebi9080

Description

@alinayebi9080

as i undestand one benefit of using spring 4 restclient group service is to shared common client config. i am working on microservice system with several services,... each call others through rest api. i need to set different connection timeout for each group(currently set it through applicataion.yml) . also some client should be called via secure ssl channel (again through setting ssl bundle properties in application.yml). but i have some global config witch should be shared by all groups like status handle interceptor and connection pool. i could add my custom interceptor as i show below:

    @Bean
    public RestClientHttpServiceGroupConfigurer globalConfigurer(
            ClientRequestTraceIdService clientRequestTraceIdService,
            JsonMapper jsonMapper
    ) {
        return groups -> {
            groups.forEachClient((group, client) -> {
                client.requestInterceptor(clientRequestTraceIdService)
                        .defaultStatusHandler(
                                HttpStatusCode::isError, new GlobalRestClientErrorHandler(jsonMapper)
                        );
            });
        };
    }

but i have problem to config connection pool when i use ClientHttpRequestFactoryBuilder like below. when i use the following config my application.yml properties overrided.

  @Bean
    PoolingHttpClientConnectionManager poolingHttpClientConnectionManager(
            @Value("${rest-client.connection.max-total:150}") Integer maxTotalConnection,
            @Value("${rest-client.connection.max-per-route:50}") Integer maxPerRouteConnection
    ) {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(maxTotalConnection);
        cm.setDefaultMaxPerRoute(maxPerRouteConnection);
        return cm;
    }

    @Bean
    ClientHttpRequestFactoryBuilder<?> clientHttpRequestFactoryBuilder(
            PoolingHttpClientConnectionManager connectionManager) {

        return ClientHttpRequestFactoryBuilder.httpComponents()
                .withHttpClientCustomizer(httpClientBuilder ->
                        httpClientBuilder.setConnectionManager(connectionManager)
                );
    }

    @Bean
    public RestClientHttpServiceGroupConfigurer globalConfigurer(
            ClientRequestTraceIdService clientRequestTraceIdService,
            JsonMapper jsonMapper,
            ClientHttpRequestFactoryBuilder<?> clientHttpRequestFactoryBuilder
    ) {
        return groups -> {
            groups.forEachClient((group, client) -> {
                client.requestInterceptor(clientRequestTraceIdService)
                        .requestFactory(clientHttpRequestFactoryBuilder.build())
                        .defaultStatusHandler(
                                HttpStatusCode::isError, new GlobalRestClientErrorHandler(jsonMapper)
                        );
            });
        };
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions