In the micro-services architecture, the system will be split into a number of service units, each unit between applications depend on each other through the service registration and subscription basis. Since each unit is running in a different process, call-dependent manner by remote execution, so there is a failure or delay in calling may occur because the network reasons or dependent services their own problems, and these problems will directly lead to the caller's Foreign Service also delays at this time if the caller's request is increasing, and finally there will be a failure waiting for a relying party to form a task in response to the backlog, the thread resources can not be released, eventually leading to paralysis of their own services, or even further spread of the failed final resulting in paralysis of the entire system. If such a framework exists such a serious hazard, even more so compared with the traditional architecture of instability. To solve this problem, thus creating a series of circuit breakers and other protection services.
In response to these problems, to achieve a series of service protection thread isolation, circuit breakers, etc. in Spring Cloud Hystrix in. It is also based on open source Netflix framework of Hystrix implemented, the framework aims by controlling access to those remote node systems, services and third-party libraries, providing a more robust fault tolerance for delay and failure. Hystrix have a service degradation, service fuse, thread isolation, request caching, request merging and service monitoring and other powerful features.
Next, we began to learn and use for Spring Cloud Hystrix from a simple example.
Try hands
Before you start using Spring Cloud Hystrix achieve breaker, something we acquire as a basis prior to implementation, including:
eureka-server
Project: Registration Service Center, Port: 1001eureka-client
Project: Service providers are two examples of start port 2001
Now we can copy it prior to implementation of a consumer service: eureka-consumer-ribbon
named eureka-consumer-ribbon-hystrix
. Here we begin to change them at:
The first step: pom.xml
The node dependencies introduced spring-cloud-starter-hystrix
dependency:
|
Step 2: Use the main class of the application @EnableCircuitBreaker
or @EnableHystrix
annotated open Hystrix use of:
|
Note: Here we can also use Spring Cloud applications @SpringCloudApplication
are modified Main Class annotation, the annotation shown specifically defined below. We can see that the annotation contains notes on our three cited, this also means that a standard application should include Spring Cloud service discovery and circuit breakers.
|
The third step: the transformation of service consumption, the new ConsumerService
class, and then in Controller
the last logical migration. Finally, on the specific function to perform logical increase @HystrixCommand
annotation to specify the method to downgrade services, such as:
|
Let's look at some basic features to verify the above Hystrix brings. We first covered services are starting up, and then access localhost:2101/consumer
, then you can get a normal return, such as: Services: [eureka-consumer-ribbon-hystrix, eureka-client]
.
In order to trigger service degradation logic, we can service providers eureka-client
logical add some delay, such as:
|
Restart eureka-client
after, and then try to access localhost:2101/consumer
, then we will return the results obtained are: fallback
. We from eureka-client
the console, you can see the results of the output of the service provider had to be returned, but due to the delay of 5 seconds before returning, while the service consumer triggers a service request timeout exceptions, service consumers annotation specified by HystrixCommand execution logic degraded, so the results of the request returns fallback
. Such a mechanism of self-service served to protect the foundation, but also provides automatic switching of service degradation mechanism is abnormal.