Spring Cloud OpenFeign
repository·main·Indexed 23 days ago
https://github.com/spring-cloud/spring-cloud-openfeignA declarative REST client for Spring Boot applications that creates dynamic implementations of interfaces decorated with JAX-RS or Spring MVC annotations. It provides autoconfiguration, integration with Spring Cloud LoadBalancer, CircuitBreaker fallbacks, and OAuth2 support. Note: As of Spring Cloud 2022.0.0, the project is feature-complete; new projects are encouraged to migrate to Spring HTTP Service Clients.
What's inside Spring Cloud OpenFeign
- Spring Cloud OpenFeign provides OpenFeign integrations for Spring Boot applications. It achieves this through autoconfiguration and by binding to the Spring Environment and other Spring programming model idioms, allowing for seamless declarative REST client usage within the Spring ecosystem.
Overview of Spring Cloud OpenFeign
mainSpring Cloud OpenFeign is a declarative REST client. It allows you to define a REST service by creating an interface decorated with JAX-RS or Spring MVC annotations. Feign then creates a dynamic implementation of that interface at runtime, enabling you to make HTTP calls by simply calling methods on the interface.Observability Conventions for Spring Cloud OpenFeign
mainSpring Cloud OpenFeign provides a set ofGlobalObservationConventionandObservationConventionimplementations to standardize how observability data (metrics and traces) is captured during Feign client interactions. These conventions allow developers to understand the specific naming patterns and metadata used when instrumenting Feign clients with Micrometer Observation.Migration recommendation from Spring Cloud OpenFeign
mainAs of the Spring Cloud 2022.0.0 release, the Spring Cloud OpenFeign project is considered feature-complete. Maintenance is limited to bugfixes and small community feature PRs. For new development or long-term projects, it is recommended to migrate to Spring HTTP Service Clients instead.Configure Feign timeouts
mainFeign supports two timeout parameters:
connectTimeout: Prevents blocking the caller due to long server processing time during connection establishment.readTimeout: Triggered when returning the response takes too long after the connection is established.
Migration recommendation: Use Spring HTTP Service Clients
mainSpring Cloud OpenFeign is now considered feature-complete. Future updates will be limited to bugfixes and small community feature PRs. For new development, it is recommended to migrate to Spring HTTP Service Clients instead.Declarative REST Clients with OpenFeign
mainOpenFeign allows you to create dynamic implementations of interfaces that act as REST clients. Instead of writing boilerplate code for HTTP requests, you simply define an interface and use standard annotations to describe the endpoints. This project supports both Spring MVC and JAX-RS annotations for this purpose.Understand URL resolution precedence in Feign clients
mainThe way a Feign client resolves its target URL depends on how it is configured. Use the following table to determine how your client will behave:
|= | Configuration Method | Example | **Resolution Behavior | |
@FeignClientannotation |@FeignClient(name="testClient", url="http://localhost:8081")| Resolved from theurlattribute; no load-balancing. | Annotation + Properties |@FeignClient(name="testClient", url="http://localhost:8081")ANDspring.cloud.openfeign.client.config.testClient.url=http://localhost:8081| Resolved from theurlattribute; no load-balancing. The property is ignored. | Properties only |@FeignClient(name="testClient")ANDspring.cloud.openfeign.client.config.testClient.url=http://localhost:8081| Resolved from configuration properties; no load-balancing. Supports refresh ifrefresh-enabled=true. | Name only |@FeignClient(name="testClient")| Resolved via thenameattribute using load balancing. |=Generate documentation using Antora
mainThe
spring-cloud-buildmodule includes adocsprofile that uses [Antora] to build asciidoc sources frommodules/ROOT/. To generate the documentation site, run the following command:./mvnw -pl docs -P docs antora:antoraBuild and test Spring Cloud OpenFeign
mainTo build the source, you need JDK 17 installed. The project uses Maven for build activities. You can use the included Maven wrapper (
./mvnw) or your own Maven installation (version 3.3.3 or better).If using your own Maven installation, you may need to add the
-P springprofile to resolve Spring milestone and snapshot repositories if your localsettings.xmldoes not already contain them.$ ./mvnw installImplement Feign CircuitBreaker fallbacks
mainIf Spring Cloud CircuitBreaker is enabled (
spring.cloud.openfeign.circuitbreaker.enabled=true), you can define afallbackclass or afallbackFactoryin the@FeignClientannotation. The fallback class must implement the Feign client interface and be declared as a Spring bean (e.g., using@Component).Use
fallbackFactoryif you need access to theThrowablecause that triggered the fallback.@FeignClient(name = "test", url = "http://localhost:${server.port}/", fallback = Fallback.class) protected interface TestClientWithFallback { @GetMapping("/hello") Hello getHello(); } @Component static class Fallback implements TestClientWithFallback { @Override public Hello getHello() { return new Hello("Fallback response"); } }Include Spring Cloud OpenFeign in your project
mainTo use Feign, add the
spring-cloud-starter-openfeignstarter to your project dependencies. You must also enable Feign clients in your Spring Boot application using the@EnableFeignClientsannotation.@SpringBootApplication @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }