Spring Cloud Feign
Spring Cloud Feign is a set of declarative service invocation clients based on the Netflix feign implementation. It makes writing Web service clients easier. We only need to configure it by creating an interface and using annotations to complete the binding to the Web service interface. It features pluggable annotation support, including feign annotations, Jax-rs annotations. It also supports pluggable encoders and decoders. Spring Cloud Feign also extends support for spring MVC annotations, while also consolidating the ribbon and Eureka to provide an HTTP client implementation of a balanced load.
Below, we show an example of how feign can easily declare the definition and invocation of the Eureka-client service.
Give it a try.
In the following example, we will use the previously built as a service registry as a service eureka-server
eureka-client
provider as the basis. And based on the spring Cloud Ribbon implementation of the consumer, we can be implemented according to eureka-consumer
the content of the simple change can be completed, the specific steps are as follows:
- Under
eureka-consumer
Copy a service consumer project, named:eureka-consumer-feign
。 Inpom.xml
The following dependencies are added:<dependencies> ... <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> Spring-cloud-starter-feign</artifactid> </dependency></dependencies>
Modify the main class of the application. @EnableFeignClients
the ability to scan the spring Cloud feign client with annotations opens:
@EnableFeignClients @enablediscoveryclient@springbootapplicationpublic class Application {public static void main ( String[] args) {new Springapplicationbuilder (Application.class). Web (True). Run (args);}
Create a feign client interface definition. Using @FeignClient
annotations To specify the name of the service to invoke for this interface, the functions defined in the interface use spring MVC annotations to bind the service provider's rest interface, such as eureka-client
the following example of a binding service /dc
interface:
@FeignClient ("eureka-client") public interface Dcclient { @GetMapping ("/DC") String Consumer ();}
Modify the controller. The interface of the service provider is invoked by the defined feign client:
@RestControllerpublic class Dccontroller { @Autowired dcclient dcclient; @GetMapping ("/consumer") public String DC () { return Dcclient.consumer ();} }
The way to make service calls through spring Cloud feign is much simpler, by @FeignClient
defining interfaces to unify the lives we need to rely on for the MicroServices interface. And in the specific use of the call to the local method a little bit of the call. Since feign is implemented based on the Ribbon, it comes with client load balancing, and can be extended through the irule of the Ribbon. In addition, feign integrates the Hystrix to implement fault-tolerant protection of the service, and in Dalston version, feign Hystrix is turned off by default. After introducing Hystrix, we will introduce the hystrix in feign and how to configure them.
Source Source
After you have finished writing your code, the reader can start Eureka-server, Eureka-client, Eureka-consumer-feign, and then access Http://localhost:2101/consumer, To track how the Eureka-consumer-feign service consumes the interface of the Eureka-client service /dc
, and can also observe its load balancing effect by initiating multiple eureka-client services.
Spring Cloud Spring Boot mybatis Enterprise Distribution Micro Service Cloud (v) Service consumption (feign) "Dalston Edition"