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

Staying Connected While Travelling Abroad With Free International Data Roaming

Staying connected with your loved ones while travelling abroad used to be a challenge. Previously your cell phone didn’t work abroad, roaming charges were ridiculous and you may have had to use a sketchy computer at an airport to let your folks keep up with where you are if delays happen. For long layovers, getting a local SIM card may not be practical or even possible. You can use free airport Wi-Fi but that only works within the Airport most of the times. In my case with T-Mobile, I get free roaming and texting in over 200 countries, so as soon as I get into a new country, my phone has service! The data is throttled but it is enough to make calls and browse the web to get my boarding pass, itinerary etc. Even after arriving at my final destination, I tried to use local SIM card and buy data plans but they were ridiculously expensive and these expire nightly. T-Mobile offers faster speeds at $5 per day, but I did manage without having to buy extra data.
Personal Hotspot for my othe…

Introducing EfeyoPixel Technology Solutions

๐Ÿ†•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.