SpringCloud project construction-feign call service interface (4)

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.
Insert picture description here
Insert picture description here
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,
Insert picture description here
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.
Insert picture description here
Make a call in the consumer controller.
Insert picture description here
Finally, add the feign call annotation to the consumer startup class.
Insert picture description here
After startup, the following figure
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_37980436/article/details/106998314
Recommended