Spring Cloud Learning Notes-007

Source: Internet
Author: User

Tag: size based on the use of directory ice NET interface call consolidation img

    • Declarative service invocation: Spring Cloud Feign

Feign, based on the Netflix feign, integrates the Spring cloud ribbon and Spring Cloud Hystrix, which provides a declarative approach to Web service client definition in addition to the powerful features of both.

When using the spring Cloud ribbon, it is common to use its request interception for Resttemplate to implement interface calls to dependent services, while Resttemplate has implemented encapsulation of HTTP requests, forming a set of templated calling methods. In real-world development, because the invocation of service dependency may be more than one place, often one interface is called in multiple places, it is common for each microservices to encapsulate some client classes to wrap these dependent service calls. Spring Cloud Feign is further encapsulated on the basis of resttemplate, which helps us define and implement the definition of a dependent service interface. With the implementation of Spring cloud feign, we simply create an interface and configure it in annotations to complete the interface binding to the service provider, simplifying the development of the self-encapsulating service invocation client when using the Spring cloud Ribbon.

1. Launch the registry and Demo-member instances, create Demo-customer-feign engineering, skeleton selection QuickStart.

2. Join related dependencies:

3. Write the Startup class:

4. Define the Memberservice interface, bind the service by specifying the service name with the @feignclient annotation (note: The service name here is not case sensitive), and then use spring MVC annotations to bind the rest interface provided by the service.

5. Create a Membercontroller to implement the call to the feign client. Use @autowired to directly inject the Memberservice instance defined above and invoke the GetMember method with the client that binds the Member-service service interface to initiate a call to the service for the/member interface.

6. Create the Application.yml file in the Src/main/resources directory:

7. Start the service to verify:

    • Parameter binding

The various business interfaces in a real-world system are much more complex, and the various locations of HTTP are passed into different types of parameters, and can be a complex object structure when returning a request response.

1. First extend the service provider Demo-member. Add the following interface definitions, which contain requests with request parameters, requests with header information, requests with requestbody, and requests to respond to body weight as an object (note: User object customization, only two fields, String Name and integer age, in addition, the default constructor method must be provided in user, otherwise spring Cloud feign throws an exception when converting a user object based on a JSON string.

2. Then implement the bindings for these new requests in the Demo-customer-feign application:

    • First, create the same user class as above in Demo-customer-feign.
    • Then, add a binding declaration to the above three new interfaces in the Memberservice interface:

  

3. Finally, a new/getmember2 interface is added to the Membercontroller to invoke the new declaration:

4. Start the project and test the results:

Spring Cloud Learning Notes-007

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.