First, background: In the tiny architecture, we split the system into many service units, each of which relies on service registration and subscription. Since each unit is in a different process and relies on remote invocation, this can cause a call failure or delay due to network or dependent service problems, which directly lead to a delay in the external service of the first party, and if the caller's request is increasing at this time, Finally, because of the waiting for the failure of the relying party to form a task squeeze, resulting in its own service paralysis.
Chestnut: E-commerce website, we may split the system into users, orders, inventory, points and other service units, the user to create an order, the customer will be the first OH AH with order service created order interface, at this time into AH order interface preferential want to inventory reset request shipment, at this time if the inventory service response is slow, The county of the service that will directly cause the order to be created is suspended to wait for the inventory request service to respond. If the waiting time is too long, users will fail to create orders because of failed requests for inventory, if the situation is high concurrency, because these suspended counties are waiting for the inventory service and failed to release, so that the subsequent creation of the order request is blocked, resulting in the order service is not available,
Second, the program: Circuit breaker mode: Spring Cloud hystrix, to achieve a circuit breaker, thread isolation and a series of protection functions, with service degradation, service fuse, Sinai City and signal isolation, request caching, request consolidation and service monitoring and other powerful features.
Third, realize:
1:
Join in Pom.xml
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</exclusion>
</exclusions>
<version>1.5.12</version>
</dependency>
2:
Test class:
It can be found that when there is an abnormality in test (), it goes into helltest ().
Four: Principle
Work Flow:
1: Create a Hystrixcommand or Hystrixobservablecommand object
Hystrixcommand used to return a single action result
Hystrixobservablecommand for returning multiple operation results
A, Command mode: Encapsulates the client's request as an object, allowing you to parameterize the client with different requests. Can be used to decouple the "behavior requestor" from "The Behavior's creator".
1) Receiver: Know how to handle the business logic
2) Abstract command: Defines a command to the play that should have a series of command operations, such as execute (), when the fame and fortune operation is invoked, it will trigger the receiver to do the specific command of the corresponding also no logic
3) The specifics of the command, in which the relationship between the command operation and the receiver is bound, the Execute () command implements the action () function of the delegate to the Force receiver
4) Caller: holds a Command object and can complete the specific business logic through the command object when needed
b, the caller and receiver's relationship is very similar to: request-response, so it is more suitable for logging, revocation operations, queue requests and so on.
The following situation should consider the command pattern:
1) Use Command mode as callback (callback): callback is talking about registering the function first, and then calling this function later.
2) You need to specify the request at different times, queue the request, the original request issuer may already be gone, and the command object itself is still alive
3) The system needs to support the command of the book is small, the command object can store the state, waiting for the client to undo the effect of the command can call the appropriate method.
4) To update all data in the system to the log so that it can be read back to the log when the system crashes.
2. Command execution
Execute () synchronous execution
Queue () executes asynchronously, returning a future object that contains a single result to be returned when the service execution ends.
Hystrixobservablecommand implements two other ways of executing a party:
Observe (): Returns the Observable (event source) object, which represents multiple results of the operation and is a hot Observable: regardless of whether the Observable (event source) has a subscriber, the event is published after it is created, It is possible for subscribers to start midway through the event and may just see the entire operation.
Toobservable (): Returns the Observable object, but returns a cold Observable: The time is not published when there is no subscriber, but waits
3. Whether the result is cached
If the request function of the current command is enabled, and the command is cached in the command, the cached result is immediately returned as a observable object.
4. Whether the circuit breaker is open
If open, Hystrix will not execute the command but the village street fallback processing logic (8)
No: Check if there are resources available to execute the command (5)
5. Whether the thread pool/request queue/signal volume is full
Yes: Go to fallback processing logic (8)
The thread pool judged by Hystrix is not the thread pool of the container, but the thread pool of each dependent service
6, Hystrixobservablecommand.construct () or Hystrixcommand.run ()
Hystrixcommand.run () returns a single result, or throws an exception
Hystrixobservablecommand.construct () Returns a observable object that emits multiple results, or sends an error notification via onerror
7, calculate the health of the circuit breaker
Hystrix will report "Success", "failure", "Reject", "timeout" and other information to the circuit breaker, and the circuit breaker will maintain a set of counters to count the data.
Use these statistics to determine if you want to open the circuit breaker
8, fallback processing
When a command fails, Hystrix attempts to rollback, often referred to as "service demotion"
Processing of service demotion:
4 steps: The command is in a "fuse/short" state, when the circuit breaker is open
5 Steps: The thread pool, request queue, or semaphore of the current command is fully occupied
6 steps: When throwing an exception
9. Return to successful response
Follow-up: command name, grouping, and thread pool partitioning, request caching
Spring Cloud Hystrix Service Fault Tolerant protection