'Customize HTTPClient for Spring Cloud Open Feign

I want to do some thing like the following using Spring Cloud Open Feign

  • Create a Pooling HTTP client connection manager
  • Have an idle Connection Manager monitor thread for housekeeping
  • Avoid CLOSE_WAIT, always close response after consuming the body

HTTP Client code would look like


  static class IdleConnectionMonitorThread extends Thread {
 
        private volatile boolean shutdown;
        private PoolingHttpClientConnectionManager connMgr;
 
        public IdleConnectionMonitorThread(PoolingHttpClientConnectionManager connMgr) {
            super();
            this.setName("TCP-Ideal-Monitroing-Thread");
            this.connMgr = connMgr;
        }
 
        @Override
        public void run() {
            try {
                while (!shutdown) {
                        sleep(TimeUnit.MINUTES.toMillis(1));
                        // Close expired connections
                        connMgr.closeExpiredConnections();
                        // Optionally, close connections
                        // that have been idle longer than 60 sec
                        connMgr.closeIdleConnections(10, TimeUnit.MINUTES);
                }
            } catch (InterruptedException ex) {
                // terminate
            }
        }
 
        public void shutdown() {
            shutdown = true;
        }
 
    }
    
public void createClient() throws Exception {
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(10, TimeUnit.MINUTES);
    connectionManager.setMaxTotal(1000);
    connectionManager.setDefaultMaxPerRoute(20);
     CloseableHttpClient httpClient = HttpClients.custom()
                                                     .setConnectionManager(connectionManager)
                                                     .build()) 
 
     Thread th = new IdleConnectionMonitorThread(connectionManager);
 
     th.start();
        
}

The reason i want to do is because i can see lot of HTTP Connections are open and not closed properly due to which i am getting some intermittent errors.

Is there a way to customize Spring Cloud Open Feign's HTTP Client based on what is mentioned above?



Solution 1:[1]

Please refer to the following link on how to customize it. https://codingnconcepts.com/spring-boot/change-default-feign-client-implementation/

<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-httpclient</artifactId>
</dependency>

IN application.properties or yaml file 

feign.httpclient.enabled: true


@Configuration
public class FeignClientConfig {

    @Bean
    public CloseableHttpClient feignClient() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(10, TimeUnit.MINUTES);
    connectionManager.setMaxTotal(1000);
    connectionManager.setDefaultMaxPerRoute(20);
     CloseableHttpClient httpClient = HttpClients.custom()
                                                     .setConnectionManager(connectionManager)
                                                     .build()) 
 
     Thread th = new IdleConnectionMonitorThread(connectionManager);
 
     th.start();
    return httpClient;
    }
}

  static class IdleConnectionMonitorThread extends Thread {
 
        private volatile boolean shutdown;
        private PoolingHttpClientConnectionManager connMgr;
 
        public IdleConnectionMonitorThread(PoolingHttpClientConnectionManager connMgr) {
            super();
            this.setName("TCP-Ideal-Monitroing-Thread");
            this.connMgr = connMgr;
        }
 
        @Override
        public void run() {
            try {
                while (!shutdown) {
                        sleep(TimeUnit.MINUTES.toMillis(1));
                        // Close expired connections
                        connMgr.closeExpiredConnections();
                        // Optionally, close connections
                        // that have been idle longer than 60 sec
                        connMgr.closeIdleConnections(10, TimeUnit.MINUTES);
                }
            } catch (InterruptedException ex) {
                // terminate
            }
        }
 
        public void shutdown() {
            shutdown = true;
        }
 
    }

Solution 2:[2]

You need to use the built-in property children, this name is used by convention and it is always plural. It will be set to whatever you put between the <Button> opening and closing tags.

In other words, rename the child destructuring property to children in the Button component, both in the header + where it is used.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Peter B