Overriding Feign defaults

The default configuration is used by each Spring Cloud Feign using FeignClientsConfiguration. Spring Cloud creates a new configuration context on-demand for each named client using the FeignClientsConfiguration file. This configuration file has almost all the required attributes of FeignClient, such as feign.Decoderfeign.Encoder, and feign.Contract. But Spring Cloud allows you to override these configuration attributes by adding an additional configuration file on top of FeignClientsConfiguration.

Spring Cloud Netflix provides the following beans as default configurations for Feign:

  • Decoder feignDecoder: ResponseEntityDecoder class provides feignDecode bean
  • Encoder feignEncoder: SpringEncoder class provides feignEncoder bean
  • Logger feignLogger: Slf4jLogger class provides feignLogger bean
  • Contract feignContract: SpringMvcContract class provides feignContract bean
  • Feign.Builder feignBuilder: HystrixFeign.Builder class provides feignBuilder bean
  • Client feignClient: If Ribbon is enabled it is a LoadBalancerFeignClient, otherwise the default Feign client is used

We can override all these listed default configurations for Feign either using the custom configuration file or the configuration properties file (YMAL or properties).

Let's see the following example of the configuration file:

@FeignClient(name = "account-service", configuration = AccountConfiguration.class) 
public interface AccountService { 
    //.. 
} 

Now, this AccountService client will be used for both the FeignClientsConfiguration and AccountConfiguration configurations, but the same attributes will be overridden by the attributes in the AccountConfiguration file.

In the Feign client configuration, we don't need to annotate the AccountConfiguration class with @Configuration. If you have annotated it with the @Configuration annotation, then take care to exclude it from any @ComponentScan because this configuration will become the default source for feign.Decoder, feign.Encoder, feign.Contract, and so on. So, you have to avoid putting it with the common configuration files and put this file in a separate, non-overlapping package from any @ComponentScan or @SpringBootApplication, or you can also explicitly exclude this configuration file from the component scanning by using @ComponentScan.

The @FeignClient annotation also supports placeholders in the name and URL attributes of this annotation. Let's see the following example:

@FeignClient(name = "${feign.name}", url = "${feign.url}", configuration = AccountConfiguration.class) 
public interface AccountService { 
    //.. 
} 

Let's see the following configuration file that will be used with the @FeignClient annotation:

@Configuration 
public class AccountConfiguration { 
    @Bean 
    public Contract feignContract() { 
        return new feign.Contract.Default(); 
    } 
 
    @Bean 
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() { 
        return new BasicAuthRequestInterceptor("user", "password"); 
    } 
} 

This configuration file replaces SpringMvcContract with feign.Contract.Default and adds a RequestInterceptor bean.

Spring Cloud also allows you to override the default configuration of the @FiegnClient annotation using configuration properties, let's see the following .yml file:

feign: 
  client: 
    config: 
      feignName: 
        connectTimeout: 5000 
        readTimeout: 5000 
        loggerLevel: full 
        errorDecoder: com.dineshonjava.decode.CustomErrorDecoder 
        retryer: com.dineshonjava.CustomRetryer 
        requestInterceptors: 
          - com.dineshonjava.interceptor.AccountRequestInterceptor 
          - com.dineshonjava.interceptor.CustomRequestInterceptor 
        decode404: false 
        encoder: com.dineshonjava.CustomEncoder 
        decoder: com.dineshonjava.CustomDecoder 
        contract: com.dineshonjava.CustomContract

Spring Cloud also allows you to configure default configuration in the @EnableFeignClients attribute, defaultConfiguration. The given configuration in the @EnableFeignClients annotation will apply to all Feign clients. Let's see the following:

package com.dineshonjava.customerservice; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.openfeign.EnableFeignClients; 
 
@SpringBootApplication 
@EnableFeignClients(defaultConfiguration=BasicFeignConfig.class) 
public class CustomerServiceApplication { 
 
   public static void main(String[] args) { 
         SpringApplication.run(CustomerServiceApplication.class, args); 
   } 
} 

Suppose we have both the @Configuration bean and configuration properties in our project, then configuration properties will be used. The configuration properties file overrides the @Configuration values. But you can change the priority to @Configuration by setting feign.client.default-to-properties to false.

Spring Cloud also allows you to create the Feign client manually, let's see it in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.163.229