Configuration
About
In a Spring Boot application, OpenFeign clients are auto-configured by default when using the spring-cloud-starter-openfeign
dependency. However, enterprise-grade applications often need fine-grained control over aspects like:
Connection timeouts
Logging level
Interceptors (headers, authentication)
Retry mechanisms
Custom encoders/decoders
To enable such control, Spring allows custom configuration classes, either globally or per client.
Types of Configuration
Global Configuration
Applies to all Feign clients in the application
Per-Client Configuration
Applies only to a specific Feign client using @FeignClient(configuration = ...)
Global Configuration
Global configuration in OpenFeign refers to settings that apply across all Feign clients in your Spring Boot application. This is especially useful when:
You want a standard behavior across microservices.
You don't want to repeat configurations for each Feign client.
You’re handling common concerns like timeouts, retries, logging, etc.
Instead of configuring each client separately, you define a default configuration block in application.yml
or application.properties
.
Spring Cloud OpenFeign reads the global configuration from the feign.client.config.default
section. It uses this block to initialize every client, unless a specific override is defined for that client.
Example: application.yml
feign:
client:
config:
default:
connect-timeout: 5000 # in milliseconds
read-timeout: 10000
loggerLevel: full # NONE, BASIC, HEADERS, FULL
retryer:
period: 100 # initial retry interval
maxPeriod: 1000 # max retry interval
maxAttempts: 3 # max retry attempts
If we name a client (like account-service
), that block overrides the default:
yamlCopyEditfeign:
client:
config:
default:
connect-timeout: 3000
read-timeout: 7000
loggerLevel: basic
account-service:
read-timeout: 15000
Available Global Settings
connect-timeout
Timeout for establishing connection (ms)
read-timeout
Timeout for waiting for response (ms)
loggerLevel
Log verbosity: NONE
, BASIC
, HEADERS
, or FULL
retryer.period
Base interval between retries
retryer.maxPeriod
Maximum wait time between retries
retryer.maxAttempts
Max number of retry attempts
errorDecoder
Fully qualified class name of custom error decoder
requestInterceptors
List of fully qualified interceptor class names
decoder
/ encoder
Custom decoder or encoder bean class names (if overriding globally)
Per-Client Configuration
Per-client configuration in OpenFeign allows you to define custom settings for individual Feign clients, giving you fine-grained control over how each client behaves.
This is useful when certain services:
Have different timeout requirements
Need specific headers or interceptors
Require unique retry policies
Should use a different encoder, decoder, or error handler
Rather than applying a one-size-fits-all setup (like in global config), you tailor each client based on its communication pattern, SLAs, or external API behavior.
How It Works ?
OpenFeign allows us to configure each client separately in our application.yml
or application.properties
, using the name of the client defined in @FeignClient(name = "...")
.
We can also attach a Java config class using the configuration
attribute of @FeignClient
.
Configuration in application.yml
feign:
client:
config:
account-service:
connect-timeout: 2000
read-timeout: 6000
loggerLevel: full
retryer:
maxAttempts: 3
period: 100
maxPeriod: 500
payment-service:
connect-timeout: 4000
read-timeout: 10000
loggerLevel: basic
This ensures:
account-service
is faster and retries quickly.payment-service
allows for longer response times and lower log verbosity.
Using Java Configuration Class
We can create a dedicated configuration class:
@Configuration
public class AccountClientConfig {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
@Bean
public Retryer customRetryer() {
return new Retryer.Default(100, 1000, 3);
}
@Bean
public RequestInterceptor customHeaderInterceptor() {
return requestTemplate -> requestTemplate.header("X-Custom-Header", "AccountClient");
}
}
And link it like this:
@FeignClient(
name = "account-service",
url = "http://localhost:8081",
configuration = AccountClientConfig.class
)
public interface AccountClient {
@GetMapping("/accounts/{id}")
Account getAccount(@PathVariable String id);
}
Available Settings
connect-timeout
application.yml/properties
read-timeout
Same as above
loggerLevel
application.yml
or Java config class
retryer
Java config class or YAML block
errorDecoder
Java class implementing ErrorDecoder
requestInterceptors
One or more RequestInterceptor
beans
encoder
/ decoder
Custom classes to override default behavior
Last updated