Change Default Feign Client implementation in Spring Boot Change Default Feign Client implementation in Spring Boot

Page content

In this quick tutorial, we’ll learn how to change default Feign Client implementation to ApacheHttpClient or OkHttpClient in Spring Boot application.

When you configure a Feign Client in Spring Boot application to execute outbound API calls, then it uses HttpClient under the covers by default which can be changed to ApacheHttpClient or OkHttpClient

Change to ApacheHttpClient


You can change the default Feign Client implementation to ApacheHttpClient by:-

  1. Setting feign.httpclient.enabled property to true, and
  2. Adding io.github.openfeign:feign-httpclient dependency in the project classpath
Property application.yml
feign.httpclient.enabled: true
Maven pom.xml

Click on the below link to get initial pom.xml which includes web and cloud-feign dependencies:-

https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.1.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=api&name=api&description=Create%20Feign%20Client%20to%20consume%20RESTFul%20APIs&packageName=com.example.api&dependencies=cloud-feign,web

and then add the feign-httpclient dependency:-

<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-httpclient</artifactId>
</dependency>
Gradle build.gradle
plugins {
    id 'org.springframework.boot' version '2.5.1'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "2020.0.3")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    implementation 'io.github.openfeign:feign-httpclient'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

Spring Boot takes care of the dependency version of feign-httpclient. You can also get it from maven


You can further customize the Apache HTTP Client by providing a bean of type org.apache.http.impl.client.CloseableHttpClient

@Configuration
public class FeignClientConfig {

    @Bean
    public CloseableHttpClient feignClient() {
        return HttpClients.createDefault();
    }
}

Please note the @Configuration annotation on the FeignClientConfig, which makes it global configuration and applied to all FeignClient in your spring boot application.

Do not annotate with @Configuration if you want to use it for specific FeignClient, Custom feignClient bean doesn’t work without @Configuration, You need to provide custom Feign.Builder bean instead

public class FeignClientConfig {

    @Bean
    public Feign.Builder feignBuilder() {
        return Feign.builder()
            .retryer(Retryer.NEVER_RETRY)
            .client(new ApacheHttpClient());
    }
}

Change to ApacheHttp5Client


You can change the default Feign Client implementation to ApacheHttp5Client (latest version of ApacheHttpClient) by:-

  1. Setting feign.httpclient.enabled property to true, and
  2. Adding io.github.openfeign:feign-hc5 dependency in the project classpath
Property application.yml
feign.httpclient.enabled: true
Maven pom.xml

Click on the below link to get initial pom.xml which includes web and cloud-feign dependencies:-

https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.1.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=api&name=api&description=Create%20Feign%20Client%20to%20consume%20RESTFul%20APIs&packageName=com.example.api&dependencies=cloud-feign,web

and then add the feign-hc5 dependency:-

<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-hc5</artifactId>
</dependency>
Gradle build.gradle
plugins {
    id 'org.springframework.boot' version '2.5.1'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "2020.0.3")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    implementation 'io.github.openfeign:feign-hc5'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

Spring Boot takes care of the dependency version of feign-hc5. You can also get it from maven


You can further customize the Apache HTTP Client by providing a bean of type org.apache.hc.client5.http.impl.classic.CloseableHttpClient

@Configuration
public class FeignClientConfig {

    @Bean
    public CloseableHttpClient feignClient() {
        return HttpClients.createDefault();
    }
}

Please note the @Configuration annotation on the FeignClientConfig, which makes it global configuration and applied to all FeignClient in your spring boot application.

Do not annotate with @Configuration if you want to use it for specific FeignClient, Custom feignClient bean doesn’t work without @Configuration, You need to provide custom Feign.Builder bean instead

public class FeignClientConfig {

    @Bean
    public Feign.Builder feignBuilder() {
        return Feign.builder()
            .retryer(Retryer.NEVER_RETRY)
            .client(new ApacheHttp5Client());
    }
}

Change to OkHttpClient


You can change the default Feign Client implementation to OkHttpClient by:-

  1. Setting feign.okhttp.enabled property to true, and
  2. Adding io.github.openfeign:feign-okhttp dependency in the project classpath
Property application.yml
feign.okhttp.enabled: true
Maven pom.xml

Click on the below link to get initial pom.xml which includes web and cloud-feign dependencies:-

https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.1.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=api&name=api&description=Create%20Feign%20Client%20to%20consume%20RESTFul%20APIs&packageName=com.example.api&dependencies=cloud-feign,web

and then add the feign-okhttp dependency:-

<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-okhttp</artifactId>
</dependency>
Gradle build.gradle
plugins {
    id 'org.springframework.boot' version '2.5.1'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "2020.0.3")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    implementation 'io.github.openfeign:feign-okhttp'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

Spring Boot takes care of the dependency version of feign-okhttp. You can also get it from maven


You can further customize the Ok HTTP Client by providing a bean of type okhttp3.OkHttpClient

@Configuration
public class FeignClientConfiguration {

    @Bean
    public OkHttpClient feignClient() {
        return new OkHttpClient();
    }
}

Conclusion

If we create both @Configuration bean and configuration properties, configuration properties will win. It will override @Configuration values. But if you want to change the priority to @Configuration, you can change feign.client.default-to-properties to false.

Please refer to Spring Cloud OpenFeign official documentation for more details.