feign-Use best practices

Source: Internet
Author: User
The microservices provided by Spring Cloud are HTTP-based, so it would be convenient to initiate a microservices call if the resttemplate provided by spring, plus a @loadbalanced annotation provided by the Ribbon, are available at the time of request. And load balancing is also possible.      However, if the service provider parameters are more complex, then the use of this method requires splicing URLs or use the form of map, but it is not easy to develop.      Spring Cloud Family Bucket provides a component such as feign, feign is a declarative, templated HTTP client developed by Netflix, feign is very simple to use, create an interface, add some annotations to the interface, and complete the code development. Spring Cloud has further enhanced feign based on Netflix, adding support for spring MVC annotations and integrating the Ribbon and Eureka to make the feign easier to use. Next, the best practices for using feign are described. Api/client
When we write the Dubbo service, we define an interface interface, and then the provider implements the interface to complete the specific service logic and export the service interface, and the consumer injects the interface (which needs to be configured) and then it can invoke the service. The input and output of the provider can also be defined in the same package, and both the provider and the consumer are dependent, which can reduce the amount of code development.
So also in the use of feign, we have to do this: rely on
Compile ("Org.springframework.cloud:spring-cloud-netflix-core:1.3.1.release") {Exclude group: ' Org.springframework.boot ', module: ' spring-boot ' exclude group: ' Org.springframework.boot ', module: ' Spring-boot-autoconfigure '} compile ("Org.springframework:spring-web:4.3.8.release") used here is the DALSTON.SR1 version, go to Drop Spring-boot related packages, because only @feignclient such annotations are required, the Spring-web package is added in order to have spring MVC-related annotation Interfaces @FeignClient ("Eureka-client-test") @ Requestmapping ("Spring-cloud") public interface HelloService {
@RequestMapping (value = "Hello1", method = Requestmethod.get) String Hello (@RequestParam (value = "name", required = Fals e) String name);
@RequestMapping (value = "Hello2", method = Requestmethod.get) matchmessage Hello (@RequestHeader (value = "Name", required = False) String name, @RequestHeader (value = "age", required = False) the Integer age);
@RequestMapping (value = "Hello3", method = Requestmethod.post) matchmessage Hello (@RequestBody (required = false) Userdto USERDTO); The @FeignClient represents which service (the name of the service registered to the Eureka Server), @RequestMapping is the traditional spring MVC-related annotation input and output that we are familiar with
Matchmessage and userdto the definition is relatively simple, no longer paste code, Matchmessage is mainly the data returned by the service, Userdto is the service needs the parameter service provider to join the dependency compile ("Spring-cloud-demo: feign-client:0.0.1 ") {Exclude group: ' Org.springframework ', module: ' Spring-web ' exclude group: ' Org.springframework. Cloud ', module: ' Spring-cloud-netflix-core '} The implementation of this service only requires the definition and implementation of the input and output of the interface, so there is no need for other related packages, can exclude off the service real Now @RestController public class Hellocontroller implements HelloService {
private static final Logger Logger = Loggerfactory.getlogger (Hellocontroller.class);
@Override public string Hello (@RequestParam (value = ' name ', required = False) string Name {logger.info ("Call Hello,   Simple ");   Return "Hello" + name; }
@Override public Matchmessage Hello (@RequestHeader (value = "name", required = False) String name, @RequestHeader (value   = "Age", required = False) Integer-age) {Userdto userdto = new Userdto (name, age);   Logger.info ("Call Hello, GET");   return new Matchmessage (Userdto, matchmessage.result_matched); }
@Override public Matchmessage Hello (@RequestBody (required = false) Userdto userdto) {logger.info ("Call Hello, POST");   return new Matchmessage (Userdto, matchmessage.result_matched); }} code is relatively simple, implement the HelloService interface, some of the definition of interfaces such as @requestmapping is no longer needed, has been defined in the client, this is actually feign support for inheritance. Call @RestController public class Hellocontroller {
private static final Logger Logger = Loggerfactory.getlogger (Hellocontroller.class);
@Autowired private HelloService HelloService;
@RequestMapping (value = "Feign-consumer", method = requestmethod.get) public matchmessage consume1 () {Logger.info (thi   S.helloservice.hello ("Levi.qian"));   Matchmessage message = This.helloService.hello ("Levi.qian", 20);   Logger.info (Message.getresult () + "");   Return This.helloService.hello (New Userdto ("Levi.qian", 27));      }} to inject HelloService, the direct adjustment method, the bottom layer will use the Ribbon load balancer to adjust the specific services.      This is done in a way that is coupled with the code of the provider and the consumer, but this approach can reduce a lot of code, which is acceptable if the interface is well defined in the early stages. Code location: Https://github.com/JThink/spring-cloud-demo/tree/feign-client The specific feign principle will be discussed later (why add the @feign can be called. )
Share a project: Https://github.com/JThink/SkyEye, Java, Scala and other programs running in the JVM, such as real-time log collection, indexing and visualization, the system to monitor the process level, the internal operation of the system for strategic alarm, Trace tracing is performed on distributed RPC calls for performance analysis purposes. Welcome to the exchange of interest in distributed tracking ~ ~, Exchange Group: 624054633

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.