spring cloud base combat (five) Feign

Feign is a lightweight open source Netflix, Rest client http://github.com/OpenFeign/feign , using Feign can be very convenient, simple implementation Http client using Feign only need to define an interface, and then add on the interface annotations can be.
Spring Cloud to Feign is encapsulated, Feign default integrated Ribbon implements client load balancing call.
Currently more accustomed oriented programming interface, such as Service interface, Dao interface, which is the default specification to follow.
Between micro-service call is in two ways:

  1. By micro service name, service address to get call
  2. + Notes through the interface, access to call service -Feign (to adapt to the industry and other programmers put forward, or to follow oriented programming interface) similar to that used previously @Mapper notes on a table consciousness Mapper interface, while using Feign for as long as the interface marked @FeignClient comment.
    Case:
    1. Add Feign module case on the basis of the original
    New studentservice-cloud-07-consumer- product-feign, reference studentservice-cloud-04-consumer- product-80

Here Insert Picture Description
1. Add rely pom.xml
Here Insert Picture Description
2. Create a master boot class StudentConsumer_80_feign, and add annotations @EnableFeignClients
Here Insert Picture Description
3. New ProductClientService interfaces, use @FeignClient ( "Service Name") notes logo, to specify which service calls such as: call in the code microservice-product and services / student / list, / student / get / {sid}, / student / add interfaces, as follows:
Here Insert Picture Description
4. test

  • Start two clusters eureka

  • Start 2 product provider 8001/8002

  • Start ProductConsumer_80_Feign

  • 访问:
    http://localhost/consumer/student/list
    Here Insert Picture Description

  • Successive requests have different data
    Here Insert Picture Description
    found there as load-balancing configuration features

5.Feign works:
Feign interface method by calling Rest service (before a Ribbon + RestTemplate), Eureka request to the server (http: // microservice-product / student / list), find the service interface directly through Feign, because integration the Ribbon technology, Feign own load-balancing configuration.

  • Add @EnableFeignClients startup class notes, Spring will scan marked @FeignClient annotation interface, and this interface generates a proxy object
  • @FeignClient ( "Service Name") which specifies the name of the product and services, Feign product will get a list of services from Eureka registry, and service calls by load balancing algorithm.
  • Use annotations in @RequestMapping interface method (value = "/ student / list ", method = RequestMethod.GET), the specified call url, Feign remote call will be in accordance url.
    6.Feign Note
    SpringCloud to Feign been enhanced compatible SpringMVC comments, we need to pay attention when using SpringMVC comments:
  • @FeignClient interface method must be added to a basic type parameter @PathVariable ( "XXX") parameter or @RequestParam ( "XXX")
  • When @FeignClient interface method return value is a complex object, the presence or absence of this type must constructor parameter.
Published 22 original articles · won praise 1 · views 811

Guess you like

Origin blog.csdn.net/CHENGXUYUAN09/article/details/104813842