Skip to main content

Spring Boot Apache HTTP Client Performance Tuning

 Spring Boot Apache HTTP Client Performance Tuning


How do you optimize pooled connections to downstream services using Apache HttpClient?
Here is an experiment.


Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.efeyopixel</groupId>
    <artifactId>myclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>myclient</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.12</version>
    </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>




Java Code;

package com.efeyopixel.myclient;

import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@SpringBootApplication
public class IwsclientApplication {

    public static void main(final String[] args) {
        SpringApplication.run(IwsclientApplication.classargs);
    }

    @Bean
    public CloseableHttpClient connectionManager() {
        final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
        connManager.setMaxTotal(100);
        connManager.setDefaultMaxPerRoute(10);
        final HttpHost host = new HttpHost("localhost"8083);
        connManager.setMaxPerRoute(new HttpRoute(host), 8);
        final CloseableHttpClient client = 
        HttpClients.custom().setConnectionManager(connManager).build();
        return client;
    }

}

@Slf4j
@RestController
@AllArgsConstructor
class IndexController {

    private final DownstreamService dService;

    @GetMapping("/pass")
    public ResponseEntity<?name() {

        log.info("pass endpoint");
        return ResponseEntity.ok(dService.name());
    }

}

@Slf4j
@Service
@AllArgsConstructor
class DownstreamService {

    
    final CloseableHttpClient client;
    
    public String name() {
        String response = null;
        CloseableHttpResponse execute = null;
        try {

            execute = client.execute(new HttpGet("http://localhost:8083/apage"));

            response = EntityUtils.toString(execute.getEntity());

        } catch (final IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (response != null) {
                log.debug("closing response");
                EntityUtils.consumeQuietly(execute.getEntity());
            }
        }   

        return response;
    }
}


Comments

Popular posts from this blog

๐Ÿ†•Maven Spring Boot Run Command Line Arguments ๐Ÿฝ๐Ÿ‘‰๐Ÿพ Spring Boot Project Intellij Idea Run

This video is presents how to properly use maven spring boot run command line arguments.

Steve Gibson - SQRL - Secure Quick Reliable LoginSQRL

SQRL - Secure Quick Reliable Login   Steve Gibson of GRC.com explains what the future of logins will be. Slide deck - https://www.grc.com/sqrl-presentation.pdf

Interesting finds April 2022

 Interesting Finds for April 2022 A list of new(ish) command line tools (jvns.ca) Podman Desktop Companion (iongion.github.io) Analyzing iMessage with SQL | by Arctype | Mar, 2022 | Database Dive