Feign is a declarative Web Service client. Its purpose is to make Web Service calls easier. Feign provides an HTTP request template. By writing a simple interface and inserting annotations, you can define the parameters, format, address and other information of the HTTP request, which is more convenient to use than RestTemplate.
Feign has the following characteristics:
Pluggable annotation support, including Feign annotation and JAX-RS annotation;
support pluggable HTTP encoder and decoder;
support Hystrix and its Fallback;
support Ribbon load balancing;
support HTTP request and response compression.
This looks a bit like the RequestMapping mapping of the Controller layer of our springmvc model. This model is something we like very much. Feign uses @FeignClient to map services
We can do a few modifications to the demo code in the previous articles.
First, we create an interface and implementation class for the producer in the producer service.
Then add the dependency configuration of feign in the pom.xml of the consumer service consumer
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
To write a consumer service,
let’s first look at the @FeignClient annotation added to the interface. This annotation identifies that it is currently a Feign client, and the value attribute is the name of the corresponding service, which is the interface in which service you need to call. So we added the service name of our producer.
Make a call in the consumer controller.
Finally, add the feign call annotation to the consumer startup class.
After startup, the following figure