1. Introduction
In this tutorial, we’ll discuss Spring Cloud OpenFeign. This tutorial is a summary from documentation page of Spring Cloud OpenFeign.
Spring Cloud OpenFeign project provides OpenFeign integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms.
2. Declarative REST Client: Feign
Feign is a declarative web service client. It makes writing web service clients easier. Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud integrates Eureka, Spring Cloud CircuitBreaker, as well as Spring Cloud LoadBalancer to provide a load-balanced http client when using Feign.
3. How to Include Feign
To include Feign in your project use the starter with group org.springframework.cloud
and artifact id spring-cloud-starter-openfeign
.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
spring-cloud-starter-openfeign
supports spring-cloud-starter-loadbalancer
. However, as is an optional dependency, you need to make sure it has been added to your project if you want to use it. Starting with Spring Cloud OpenFeign 4, the Feign Apache HttpClient 4 is no longer supported. We suggest using Apache HttpClient 5 instead.
4. Enable Feign Clients
Annotate your main application class with @EnableFeignClients
to enable Feign functionality:
@SpringBootApplication @EnableFeignClients public class FeignExampleApplication { public static void main(String[] args) { SpringApplication.run(FeignExampleApplication.class, args); } }
To use @EnableFeignClients
annotation on @Configuration-annotated-classes, make sure to specify where the clients are located, for example: @EnableFeignClients(basePackages = "com.example.clients")
or list them explicitly: @EnableFeignClients(clients = InventoryServiceFeignClient.class)
5. Using Feign Client
@FeignClient("stores") public interface StoreClient { @RequestMapping(method = RequestMethod.GET, value = "/stores") List<Store> getStores(); @RequestMapping(method = RequestMethod.GET, value = "/stores") Page<Store> getStores(Pageable pageable); @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json") Store update(@PathVariable("storeId") Long storeId, Store store); @RequestMapping(method = RequestMethod.DELETE, value = "/stores/{storeId:\\d+}") void delete(@PathVariable Long storeId); }
In the @FeignClient
annotation the String value (“stores” above) is an arbitrary client name, which is used to create a Spring Cloud LoadBalancer client. You can also specify a URL using the url
attribute (absolute value or just a hostname). The name of the bean in the application context is the fully qualified name of the interface. To specify your own alias value you can use the qualifiers
value of the FeignClient
annotation.
The load-balancer client above will want to discover the physical addresses for the “stores” service. If your application is a Eureka client then it will resolve the service in the Eureka service registry. If you don’t want to use Eureka, you can configure a list of servers in your external configuration using SimpleDiscoveryClient. Spring Cloud OpenFeign supports all the features available for the blocking mode of Spring Cloud LoadBalancer.
6. Attribute resolution mode
While creating Feign client beans, we resolve the values passed via the @FeignClient
annotation. If you need the attributes to be resolved lazily, set the spring.cloud.openfeign.lazy-attributes-resolution
property value to true
.
7. Overriding Feign Defaults
Spring Cloud creates a new ensemble as an ApplicationContext
on demand for each named client using FeignClientsConfiguration
.
Spring Cloud lets you take full control of the feign client by declaring additional configuration (on top of the FeignClientsConfiguration
) using @FeignClient
. Example:
@FeignClient(name = "stores", configuration = FooConfiguration.class) public interface StoreClient { //.. }
Previously, using the url
attribute, did not require the name
attribute. Using name
is now required. Placeholders are supported in the name
and url
attributes.
@FeignClient(name = "${feign.name}", url = "${feign.url}") public interface StoreClient { //.. }
@FeignClient
also can be configured using configuration properties.
spring: cloud: openfeign: client: config: feignName: url: http://remote-service.com connectTimeout: 5000 readTimeout: 5000 loggerLevel: full errorDecoder: com.example.SimpleErrorDecoder retryer: com.example.SimpleRetryer defaultQueryParameters: query: queryValue defaultRequestHeaders: header: headerValue requestInterceptors: - com.example.FooRequestInterceptor - com.example.BarRequestInterceptor responseInterceptor: com.example.BazResponseInterceptor dismiss404: false encoder: com.example.SimpleEncoder decoder: com.example.SimpleDecoder contract: com.example.SimpleContract capabilities: - com.example.FooCapability - com.example.BarCapability queryMapEncoder: com.example.SimpleQueryMapEncoder micrometer.enabled: false
feignName
in this example refers to @FeignClient
value
, that is also aliased with @FeignClient
name
and @FeignClient
contextId
. In a load-balanced scenario, it also corresponds to the serviceId
of the server app that will be used to retrieve the instances. The specified classes for decoders, retryer and other ones must have a bean in the Spring context or have a default constructor.
Default configurations can be specified in the @EnableFeignClients
attribute defaultConfiguration
in a similar manner as described above. The difference is that this configuration will apply to all feign clients.
You can use spring.cloud.openfeign.client.config.feignName.defaultQueryParameters
and spring.cloud.openfeign.client.config.feignName.defaultRequestHeaders
to specify query parameters and headers that will be sent with every request of the client named feignName
.
spring: cloud: openfeign: client: config: default: connectTimeout: 5000 readTimeout: 5000 loggerLevel: basic
8. Timeout Handling
We can configure timeouts on both the default and the named client. OpenFeign works with two timeout parameters:
connectTimeout
prevents blocking the caller due to the long server processing time.readTimeout
is applied from the time of connection establishment and is triggered when returning the response takes too long.
9. Creating Feign Clients Manually
In some cases it might be necessary to customize your Feign Clients in a way that is not possible using the methods above. In this case you can create Clients using the Feign Builder API. Below is an example which creates two Feign Clients with the same interface but configures each one with a separate request interceptor.
@Import(FeignClientsConfiguration.class) class FooController { private FooClient fooClient; private FooClient adminClient; @Autowired public FooController(Client client, Encoder encoder, Decoder decoder, Contract contract, MicrometerObservationCapability micrometerObservationCapability) { this.fooClient = Feign.builder().client(client) .encoder(encoder) .decoder(decoder) .contract(contract) .addCapability(micrometerObservationCapability) .requestInterceptor(new BasicAuthRequestInterceptor("user", "user")) .target(FooClient.class, "https://PROD-SVC"); this.adminClient = Feign.builder().client(client) .encoder(encoder) .decoder(decoder) .contract(contract) .addCapability(micrometerObservationCapability) .requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin")) .target(FooClient.class, "https://PROD-SVC"); } }
In the above example FeignClientsConfiguration.class
is the default configuration provided by Spring Cloud OpenFeign. PROD-SVC
is the name of the service the Clients will be making requests to.
10. Feign Spring Cloud CircuitBreaker Support
If Spring Cloud CircuitBreaker is on the classpath and spring.cloud.openfeign.circuitbreaker.enabled=true
, Feign will wrap all methods with a circuit breaker. To disable Spring Cloud CircuitBreaker support on a per-client basis create a vanilla Feign.Builder
with the “prototype” scope, e.g.:
@Configuration public class FooConfiguration { @Bean @Scope("prototype") public Feign.Builder feignBuilder() { return Feign.builder(); } }
The circuit breaker name follows this pattern #()
. When calling a @FeignClient
with FooClient
interface and the called interface method that has no parameters is bar then the circuit breaker name will be FooClient#bar()
.
11. Configuring CircuitBreakers With Configuration Properties
You can configure CircuitBreakers via configuration properties.
For example, if you had this Feign client
@FeignClient(url = "http://localhost:8080") public interface DemoClient { @GetMapping("demo") String getDemo(); }
You could configure it using configuration properties by doing the following
spring: cloud: openfeign: circuitbreaker: enabled: true alphanumeric-ids: enabled: true resilience4j: circuitbreaker: instances: DemoClientgetDemo: minimumNumberOfCalls: 69 timelimiter: instances: DemoClientgetDemo: timeoutDuration: 10s
12. Feign Spring Cloud CircuitBreaker Fallbacks
Spring Cloud CircuitBreaker supports the notion of a fallback: a default code path that is executed when the circuit is open or there is an error. To enable fallbacks for a given @FeignClient
set the fallback
attribute to the class name that implements the fallback. You also need to declare your implementation as a Spring bean.
@FeignClient(name = "test", url = "http://localhost:${server.port}/", fallback = Fallback.class) protected interface TestClient { @RequestMapping(method = RequestMethod.GET, value = "/hello") Hello getHello(); @RequestMapping(method = RequestMethod.GET, value = "/hellonotfound") String getException(); } @Component static class Fallback implements TestClient { @Override public Hello getHello() { throw new NoFallbackAvailableException("Boom!", new RuntimeException()); } @Override public String getException() { return "Fixed response"; } }
If one needs access to the cause that made the fallback trigger, one can use the fallbackFactory
attribute inside @FeignClient
.
@FeignClient(name = "testClientWithFactory", url = "http://localhost:${server.port}/", fallbackFactory = TestFallbackFactory.class) protected interface TestClientWithFactory { @RequestMapping(method = RequestMethod.GET, value = "/hello") Hello getHello(); @RequestMapping(method = RequestMethod.GET, value = "/hellonotfound") String getException(); } @Component static class TestFallbackFactory implements FallbackFactory<FallbackWithFactory> { @Override public FallbackWithFactory create(Throwable cause) { return new FallbackWithFactory(); } } static class FallbackWithFactory implements TestClientWithFactory { @Override public Hello getHello() { throw new NoFallbackAvailableException("Boom!", new RuntimeException()); } @Override public String getException() { return "Fixed response"; } }
13. Feign request/response compression
You may consider enabling the request or response GZIP compression for your Feign requests. You can do this by enabling one of the properties:
spring.cloud.openfeign.compression.request.enabled=true spring.cloud.openfeign.compression.response.enabled=true
Feign request compression gives you settings similar to what you may set for your web server:
spring.cloud.openfeign.compression.request.enabled=true spring.cloud.openfeign.compression.request.mime-types=text/xml,application/xml,application/json spring.cloud.openfeign.compression.request.min-request-size=2048
14. Feign logging
A logger is created for each Feign client created. By default, the name of the logger is the full class name of the interface used to create the Feign client. Feign logging only responds to the DEBUG
level.
logging.level.project.user.UserClient: DEBUG
The Logger.Level
object that you may configure per client, tells Feign how much to log. Choices are:
NONE
, No logging (DEFAULT).BASIC
, Log only the request method and URL and the response status code and execution time.HEADERS
, Log the basic information along with request and response headers.FULL
, Log the headers, body, and metadata for both requests and responses.
For example, the following would set the Logger.Level
to FULL
:
@Configuration public class FooConfiguration { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
15. Feign Caching
If @EnableCaching
annotation is used, a CachingCapability
bean is created and registered so that your Feign client recognizes @Cache*
annotations on its interface:
public interface DemoClient { @GetMapping("/demo/{filterParam}") @Cacheable(cacheNames = "demo-cache", key = "#keyParam") String demoEndpoint(String keyParam, @PathVariable String filterParam); }
You can also disable the feature via property spring.cloud.openfeign.cache.enabled=false
.
16. HATEOAS support
Spring provides some APIs to create REST representations that follow the HATEOAS principle, Spring Hateoas and Spring Data REST.
If your project use the org.springframework.boot:spring-boot-starter-hateoas
starter or the org.springframework.boot:spring-boot-starter-data-rest
starter, Feign HATEOAS support is enabled by default.
When HATEOAS support is enabled, Feign clients are allowed to serialize and deserialize HATEOAS representation models: EntityModel, CollectionModel and PagedModel.
@FeignClient("demo") public interface DemoTemplate { @GetMapping(path = "/stores") CollectionModel<Store> getStores(); }