Spring Cloud OpenFeign

repository·main·Indexed 23 days ago

https://github.com/spring-cloud/spring-cloud-openfeign

A 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.

Tokens
6.8K
Snippets
15
Records
43
Agent score
79%

What's inside Spring Cloud OpenFeign

  1. Observability Conventions for Spring Cloud OpenFeign

    main
    Spring Cloud OpenFeign provides a set of GlobalObservationConvention and ObservationConvention implementations 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.
  2. Understand URL resolution precedence in Feign clients

    main

    The 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 | | @FeignClient annotation | @FeignClient(name="testClient", url="http://localhost:8081") | Resolved from the url attribute; no load-balancing. | Annotation + Properties | @FeignClient(name="testClient", url="http://localhost:8081") AND spring.cloud.openfeign.client.config.testClient.url=http://localhost:8081 | Resolved from the url attribute; no load-balancing. The property is ignored. | Properties only | @FeignClient(name="testClient") AND spring.cloud.openfeign.client.config.testClient.url=http://localhost:8081 | Resolved from configuration properties; no load-balancing. Supports refresh if refresh-enabled=true. | Name only | @FeignClient(name="testClient") | Resolved via the name attribute using load balancing. |=

  3. Build and test Spring Cloud OpenFeign

    main

    To 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 spring profile to resolve Spring milestone and snapshot repositories if your local settings.xml does not already contain them.

    $ ./mvnw install
  4. Implement Feign CircuitBreaker fallbacks

    main

    If Spring Cloud CircuitBreaker is enabled (spring.cloud.openfeign.circuitbreaker.enabled=true), you can define a fallback class or a fallbackFactory in the @FeignClient annotation. The fallback class must implement the Feign client interface and be declared as a Spring bean (e.g., using @Component).

    Use fallbackFactory if you need access to the Throwable cause 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");
        }
    }
  5. Include Spring Cloud OpenFeign in your project

    main

    To use Feign, add the spring-cloud-starter-openfeign starter to your project dependencies. You must also enable Feign clients in your Spring Boot application using the @EnableFeignClients annotation.

    @SpringBootApplication
    @EnableFeignClients
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }