Spring Cloud Building MicroServices architecture-service consumption (feign)

Source: Internet
Author: User

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.

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:

    • According to eureka-consumer the copy of a service consumer project, named: eureka-consumer-feign . pom.xmlAdd the following dependencies in:
<dependencies>... <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-feign</artifactid> </dependency> </dependencies>
    • Modify the main class of the application. @EnableFeignClientsthe ability to scan the spring Cloud feign client with annotations opens:
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class application { public static void main(string[] args) { /c13>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:
@RestController public class Dccontroller { @Autowireddcclient 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.

After you have finished writing your code, the reader can start Eureka-server, eureka-client, and Eureka-consumer-feign. 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.

From now on, I will record the process and the essence of the recently developed Springcloud micro-service cloud architecture, and help more friends who are interested in developing the Spring cloud framework, hoping to help more good scholars. Let's explore how the Spring cloud architecture is built and how it can be used in enterprise projects. SOURCE Source

Spring Cloud Building MicroServices architecture-service consumption (feign)

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.