Spring Cloud OpenFeign Proxy Setup

Spring cloud OpenFeign supports three underlying implementations for feign client:

  • Default
  • ApacheHttpClient
  • OkHttpClient

You often come across use cases, where the feign client should use an HTTP proxy to make the outbound API call.

Depending on what implementation is used in your project, here is how you configure the feign client to use HTTP proxy:

TRICK: You configure the feign client and then override the default client bean in the Spring application context.

DEFAULT

Comes out of box client with spring cloud, no additional config or dependency is needed:

import feign.Client;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.InetSocketAddress;
import java.net.Proxy;
@Configuration
public class FeignConfiguration {
...
@Bean
public Client feignClient() {
return new Client.Proxied(null, null,
new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort)));
}
}

ApacheHttpClient

This is in effect when you have feign.httpclient.enabled: true in application.yml and io.github.openfeign:feign-httpclient in the project classpath.

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

build.gradle
implementation 'io.github.openfeign:feign-httpclient'
NOTE: Version is managed by Spring Bootimport org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.http.HttpHost;
@Configuration
public class FeignConfiguration {
...
@Bean
public CloseableHttpClient feignClient() {
return HttpClientBuilder.create().setProxy(
new HttpHost(proxyHost, proxyPort)).build();
}
}

OkHttpClient

This is in effect when you have feign.okhttp.enabled: true in application.yml and io.github.openfeign:feign-okhttp in the project classpath.

pom.xml
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>

build.gradle
implementation 'io.github.openfeign:feign-okhttp'
NOTE: Version is managed by Spring Bootimport org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.InetSocketAddress;
import java.net.Proxy;
import okhttp3.OkHttpClient;
@Configuration
public class FeignConfiguration {
...
@Bean
public OkHttpClient okHttpClient() {
return new OkHttpClient.Builder()
.proxy(new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort)))
.build();
}
}

I hope you liked ❤️ this article, stay tuned for more posts. All feedback, comments & questions are welcomed. 🏳️‍🌈

Donation😇

If this helped you reduce time to develop, you can buy me a cup of coffee ☕

Lead Full Stack & DevOps Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store