Spring Cloud Learning Series fourth "declarative Service invocation"

Source: Internet
Author: User

Previous essays our access to other microservices is through some of the template methods provided by the Ribbon components to access, the use of the process will find a problem is that the invocation of a service interface is often not a place, so we need this service interface for a package, the other place Unified call this package, And each package is actually the use of Resttemplate template method to the target service interface calls, so the code will have a lot of redundant resttemplate methods, code is not elegant, and the use of spring Cloud feign is not the same, it is spring Cloud Hystrix and Spring Cloud ribbon integration, with it at the framework level of spring cloud to help us encapsulate these things, we just need to define a need to access the target service method interface, directly through the dependency injection directly to use, The code is also more elegant, eliminating the step of our own encapsulation.

Let's rewrite the previous example and try it out. We added a feign project to access the ServiceA service as a declarative service call. Since feign needs to define an interface to access the ServiceA method, and then servicea need to provide the corresponding HTTP service, we have added a new FEIGNAPI project to define the common interface here, and then feign and servicea depend on Feignapi, Achieve the effect of code sharing. Combine the following code to understand the next.

first, the new FEIGNAPI project

Once again, FEIGAPI role of extracting common code for feign and ServiceA use, where there will be common code? This is a few interface information that needs to be accessed by the feign ServiceA.

  

The Feignservice interface defines the URL path that needs to be accessed.

 PackageCom.pumpkin.service;ImportCom.pumpkin.dto.User;Importorg.springframework.web.bind.annotation.*; @RequestMapping ("/USER") Public InterfaceFeignservice {@RequestMapping (value= "/hello1.do", method =requestmethod.get) String Hello (@RequestParam ("Name") String name); @RequestMapping (Value= "/hello2.do", method =requestmethod.get) User Hello (@RequestParam ("Name") String name, @RequestParam ("Sex") Integer sex); @RequestMapping (Value= "/hello3.do", method =requestmethod.post) String Hello (@RequestBody user user);}

Ii. feign project using declarative invocation

 Write a controller to wait for us to call Servciea's portal, the URL is: 127.0.0.1:7000/feign/hello.do

Spring.application.name=feign#eureka.instance.instance-id=${spring.application.name}:${random.  int}server.port=7000eureka.client.serviceUrl.defaultZone=http://localhost : 5001/eureka/,http://localhost: 5002/eureka/

 PackageCom.pumpkin.controller;ImportCom.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;ImportCom.pumpkin.dto.User;ImportCom.pumpkin.service.FeignService;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RestController, @RestController @requestmapping ("/feign") Public classFeigncontroller {@AutowiredPrivateFeignservice Feignservice; @RequestMapping (Value= "/hello.do", method =requestmethod.get) PublicString Feignhello () {StringBuilder sb=NewStringBuilder (); Sb.append (Feignservice.hello ("MIMI")). Append ("\ n")); Sb.append (Feignservice.hello ("MIMI"). Append ("\ n")); Sb.append (Feignservice.hello (NewUser ("MIMI")). Append ("\ n")); returnsb.tostring (); }    }

Write the Feignserviceimpl interface because Feignservice is in the FEIGNAPI project, so we have to rely on the FEIGNAPI project first. Then Feignserviceimpl only needs to inherit Feignservice and add @feignclient (value = "service-a") annotations, which can be used by dependency injection into the controller layer's portal.

 Package Com.pumpkin.service.impl; Import Com.pumpkin.service.FeignService; Import  = "Service-a")publicinterfaceextends  feignservice {}

iii. Transformation of the SERVICEA project

A new Feigncontroller is available for external service. You can see that Feigncontroller is through the implementation of the Feignservice interface, so ServiceA project will not need to redundant the code of the several methods, directly rely on FEIGNAPI.

 PackageCom.pumpkin.controller;ImportCom.pumpkin.dto.User;ImportCom.pumpkin.service.FeignService;ImportOrg.apache.log4j.Logger;ImportOrg.springframework.web.bind.annotation.RequestBody;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classFeigncontrollerImplementsFeignservice {Private FinalLogger Logger =Logger.getlogger (GetClass ()); @Override PublicString Hello (@RequestParam ("name") (String name) {return"Hello"; } @Override PublicUser Hello (@RequestParam ("name") String name, @RequestParam ("Sex") (Integer sex) {Logger.info ("Name=" +name+ ", sex=" +sex); return NewUser ("Pumpkin", 0); } @Override PublicString Hello (@RequestBody user user) {return"Hello user=" +user.tostring (); }}

Four, start to see the effect

v. Summary

Spring Cloud feign is the integration of the spring cloud ribbon and the Spring Hystrix package. This essay mainly introduces the simple use of spring Cloud feign, it is not involved in how to conduct service fusing and downgrading, but it is a few annotations from the use level, as long as the hystrix mechanism has some understanding, it is very easy to use. In addition, feign does not encapsulate the Hysrix request merge and request cache, so if you need these features, you can only use the ribbon back.

Vi. References

Spring Cloud Micro-service combat-Suphing. This series of studies is based on this book, and the source uses the spring boot and Spring cloud versions to be consistent with it.

Vii. Source Code

Code Cloud Address: [email protected]:p umpkingg/spring-cloud-study.git the code corresponding to this essay is the tag named Blog3 under the master branch

Spring Cloud Learning Series fourth "declarative Service invocation"

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.