Greetings!
In our previous posts we used Spring RestTemplate as our Rest client. Even though it was easy to use, we have lots of boilerplate code. This is why we are going to use Netflix Feign Client. If you have used Retrofit, this looks familiar (or Spring Data JPA). With Feign, we are only creating the interface with few annotations. Then Feign will create the necessary implementation for us.
One of the main advantages using Feign is, inbuilt support of Ribbon load balancing and easy to connect with Eureka server.
Source code can be found at github
That is all!
In our previous posts we used Spring RestTemplate as our Rest client. Even though it was easy to use, we have lots of boilerplate code. This is why we are going to use Netflix Feign Client. If you have used Retrofit, this looks familiar (or Spring Data JPA). With Feign, we are only creating the interface with few annotations. Then Feign will create the necessary implementation for us.
One of the main advantages using Feign is, inbuilt support of Ribbon load balancing and easy to connect with Eureka server.
Source code can be found at github
Add Feign library
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Enable Feign client
@EnableFeignClients
@SpringBootApplication
public class ShoppingCartServiceApplication
Implement Feign client
- Create an interface.
- Annotate it with @FeignClient with url and mandatory name.
- Add method signature as same as the corresponding controller method.
@FeignClient(name = "catalog-service", url = "http://localhost:8010")
public interface CatalogServiceFeign {
@GetMapping("/{id}")
Product findProduct(@PathVariable("id") String id);
}
That is all!
Comments
Post a Comment