Learn Declarative Rest Client With Openfeign


Go Back to Course Outline

Spring Cloud OpenFeign

  • A declarative web service client originally created by Netflix and is part of the Spring Cloud set of libraries.
  • It allows calling a REST service with only an annotated interface class by proxying the request to the actual microservice.
  • Spring Cloud integrates OpenFeign with Ribbon, Eureka and Load balancer to provide a load balance request.

Adding OpenFeign to our Project

  1. Add the following dependencies:
    1. spring-cloud-starter-openfeign
    2. spring-cloud-starter-netflix-ribbon
  2. Annotate the SpringBoot class with @EnableFeignClients.
  3. Create an interface annotated with @FeignClient and specify the value as the name of our service. For example “catalog”, since our application is a Eureka client, it will resolve the service in the Eureka service registry.
  4. In the interface define the proxy REST methods.

Feign Client

How to use Feign Client?

Load Balancing

To enable load balancing using Ribbon when calling a feign service add the following code in one of the configuration class from our gateway application:

@LoadBalanced 
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

HATEOAS

Since our microservices are returning EntityModel and CollectionModel as response, we also need to add the HATEOAS dependency to our gateway project. This will allow the Feign clients to serialize and deserialize the models.

References

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post
NERV Open Source
Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.