Micro-service with spring cloud-fusing mechanism

Source: Internet
Author: User

1. Introduction of fusing mechanism

Before we introduce the fusing mechanism, we need to understand the avalanche effect of microservices. In a microservices architecture, microservices are a single business function, and the benefit of doing so is decoupling, and each micro-service can evolve independently. However, an application may consist of multiple microservices, and data interactions between microservices are accomplished through remote procedure calls. This poses a problem, assuming that microservices a calls MicroServices B and MicroServices C, microservices B and microservices C call other microservices, which is called "fanout." If the invocation response time of a micro-service on a fan-out link is too long or unavailable, the call to microservices a will take up more and more system resources, causing the system to crash, the so-called "avalanche effect".

The fusing mechanism is a kind of micro-service link protection mechanism to deal with avalanche effect. We are exposed to the two words of fusing in all kinds of scenes. In high voltage circuits, if the voltage in a place is too high, the fuse fuses and the circuit is protected. In stock trading, if the stock index is too high, it will also use the fusing mechanism to suspend the trading of the stock. Similarly, in the micro-service architecture, the fusing mechanism also plays a similar role. When a microservices on the fanout link is unavailable or the response time is too long, the service is degraded, which fuses the invocation of the node microservices and quickly returns the wrong response information. Resumes the call link when the node MicroServices call response is detected as normal.

In the spring cloud framework, the fusing mechanism is implemented through Hystrix. Hystrix will monitor the status of calls between MicroServices, and when a failed call to a certain threshold, the default is 5 seconds within 20 calls failed, will start the fuse mechanism. The note of the fusing mechanism is that @hystrixcommand,hystrix will find the method of this annotation and associate such methods with the agents that are connected to the fuse. Currently, @HystrixCommand only works if the annotations for the class are @service or @component.

In the previous section, we mentioned that calls between microservices can be made in two ways, one is resttemplate and the other is feign. Correspondingly, there are hystrix calling methods in both of these invocation modes.



2. Code implementation and validation

This code implements the Resttemplate and feign two microservices invocation scenarios, using Hystrix to validate the fusing mechanism of spring cloud.

2.1 Resttemplate's microservices invocation scenario

In the fifth section of load Balancing, we used resttemplate to implement a call between services. Based on this part of the code, we add Hystrix to implement the fusing mechanism.

1) Start Eureka-server and Loadbalance-service, do not start bookingcar-service


Click on the "Loadbalance-service" link, enter the Load Balancer verification page, enter the request parameter/v1/lb/testport?name=bookingcar-service, we will get the following error on the page:


2) in Pom.xml, add a dependency on the Hystrix


3) Add the note @enablecircuitbreaker at the Ribbon-service entrance program Ribbonserviceapplication.java to turn on the fuse function as follows:


4) in Loadbalanceservice.java, the Loadbalanceservice class is declared using the @service annotation. In the Loadbalanceservice class, we introduce the fusing mechanism of the testport call through the @hystrixcommand annotation, which can be added to the @HystrixCommand annotations, such as @hystrixcommand ( Callbackcommand = "XXX"), when the Testport service cannot be invoked, the callback function of the fuse mechanism will take effect and quickly process the error.


5) Restart the Loadbalance-service, enter the Load Balancer verification page again, enter the request parameters/v1/lb/testport?name=bookingcar-service, we found that the fusing mechanism has played a role:


2.2 Feign's microservices invocation scenario

In the previous section, we implemented internal calls between microservices using feign, and support for Hystrix was built into the feign.

1) Run the report and do not start the Bookingcar-service


Click Reporting-service back link, go to Feign verification page, input parameter v1/order/1, we can see the following error reminder page:


2) in Application.properties, add feign.hystrix.enabled=true to enable feign support for Hystrix, as follows:


3) in Pom.xml, add a dependency on the Hystrix


4) in the report's entry procedure reportsapplication, add the annotation @enablecircuitbreaker to turn on the fuse function as follows:


5) in the Orderclient.java feign annotation, add the parameters of the callback function


6) Add a new class Orderclienthystrix, when the call to the microservices error occurs, processing, in this example, will return "Hystrix works in feign" information.


7) Rerun report, click Reporting-service back link, input request parameter V1/ORDER/1, we found that the fusing mechanism has played a role, shown as follows:


3. Summary:

The microservices themselves are a distributed architecture, and the availability of the system is a big challenge when the call chain goes through the long way. The Spring Cloud Framework's Hystrix provides a fusing mechanism that can be used in scenarios where the resttemplate and feign have two micro-service invocations, which can be quickly degraded when there is a problem with the call link. Provides a great help in improving the usability of our microservices architecture.

Code: Https://github.com/shuxingliu/microservices

Using Spring Cloud for MicroServices (vii)-Internal calls

Implement MicroServices with Spring Cloud (vi)-service Gateway

Using Spring cloud for microservices (v)-load balancing

Using spring Cloud to implement MicroServices (iv)-MICROSERVICES implementation and registration

Leveraging Spring Cloud for MicroServices (iii)-business domain driven microservices design

Implementing MicroServices with Spring Cloud (ii)-domain-driven design

Implementing MicroServices with Spring Cloud (i): Eureka server



Book Hing
Links: http://www.jianshu.com/p/0d53a9101ec6
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Micro-service with spring cloud-fusing mechanism

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.