springspring-bootspring-cloudopenfeign

Running out of ports when using Spring Boot Open Feign during performance test


I am encountering an issue in my project related to performance measurement using JMeter, where I need to use OpenFeign. When I start my benchmark after some time (around 15 seconds), I receive an exception for certain requests.

2025-03-09T21:01:43.736+01:00 ERROR 4304 --- [orderService] [-handler-482851] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: feign.RetryableException: Address already in use: getsockopt executing POST http://localhost:8081/api/xxx] with root cause

java.net.BindException: Address already in use: getsockopt

From what I understand, this issue occurs because the operating system runs out of ephemeral ports, as FeignClient, by default, creates a new connection for each call. I have found that enabling connection pooling via an external HTTP client could be a solution, and I see there are multiple options available (such as Apache, OKHttp, etc.). However, the examples I have come across are either incomplete or outdated.

Could someone kindly assist me with this issue? I would greatly appreciate the simplest solution that does not require modifying the TCP TIME_WAIT settings at the OS level.

Thank you in advance for your help!


Solution

  • Ok, so I figured it out. FeignClient can use either the Apache HTTP Client (HC5) or OK HTTP. However, by default, it uses a basic Java library that doesn’t properly reuse TCP connections.

    Simply having Apache HTTP Client 5 (HC5) included as a dependency is enough for Feign to pick it up. However, if I understand correctly, it can't just be the standard Apache Client from Maven—it has to be the OpenFeign version.

    I now know what the issue was, though I still don’t fully understand the details. Anyway, I’m sharing my configuration here in case someone else needs it in the future.

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-hc5</artifactId>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-httpclient</artifactId>
    </dependency>
    
    spring.cloud.openfeign.okhttp.enabled=false
    spring.cloud.openfeign.http2client.enabled=false
    
    spring.cloud.openfeign.httpclient.hc5.enabled=true
    spring.cloud.openfeign.httpclient.connection-timeout=5000
    spring.cloud.openfeign.httpclient.max-connections=50
    spring.cloud.openfeign.httpclient.time-to-live=5
    spring.cloud.openfeign.client.config.default.read-timeout=5000
    spring.cloud.openfeign.client.config.default.loggerLevel=full
    

    Here are my settings, and they work. The specific values (such as timeout or TTL) aren’t relevant for you, but the configuration itself does the job.