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 ...
https://www.czetsuyatech.com/2019/11/spring-microservice-declarative-rest-with-openfeign.html
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
- Add the following dependencies:
- spring-cloud-starter-openfeign
- spring-cloud-starter-netflix-ribbon
- Annotate the SpringBoot class with @EnableFeignClients.
- 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.
- 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
- https://cloud.spring.io/spring-cloud-openfeign/reference/html/
- Codes are available at Github:
Post a Comment