Hystrix features
1. The fusing
When the number of HystrixCommand requests for back-end service failures exceeds a certain percentage (50% by default), the circuit breaker switches to the Open state (Open). After the circuit breaker remains OPEN for a period of time (5 seconds by default), it automatically switches to half-open. Then it determines the return status of the next request. If the request is successful, the circuit breaker switches back to the CLOSED state. Otherwise switch back to the OPEN state
2.Fallback
A Fallback is a degraded operation. Service degradation is the value returned by the fallback method when an exception occurs on the request back-end service.
3. Resource isolation
Requests are encapsulated in HystrixCommand, and these requests are then executed in a separate thread, with each dependent service maintaining a small thread pool (or semaphore) that can disconnect the dependent call or return the specified logic in case of call failure or timeout.
4. The current limit
Flow limiting is semaphore isolation (this mode is not normally used)
Registries and service providers don’t need to change, just change our consumer
Add configuration file to configuration:
# Turn on Feign's Hystrix fuse feature
feign:
hystrix:
enabled: true
Copy the code
FeignClient invokes the service:
@Component
@FeignClient(value = "spring-cloud-eureka-pro",fallback = FeignServiceHystrix.class)
public interface FeignService {
@RequestMapping(value = "/hi")
public String hiService(@RequestParam(value = "name") String name);
}
Copy the code
Fallback callback class:
@Component
public class FeignServiceHystrix implements FeignService{
@Override
public String hiService(@RequestParam(value = "name")String name) {
return "hi" +name+", this is failed messge"; }}Copy the code
To test, start the registry, Provider, and Consumer
Access consumer’s HI2 method
The service is invoked normally
Stop the provider and access it again
Since the fusing function is enabled at this time, fallback is entered after the service invocation fails
If the circuit breaker function is turned off, it should return a call exception message
# Turn on Feign's Hystrix fuse feature
feign:
hystrix:
enabled: false
Copy the code
What can Hystrix do?
-
Provides system protection and control in the event of high latency or failure of dependent services accessed through third-party clients (usually over the network)
-
Prevent cascading failures in distributed systems
-
Fail fast and recover quickly
-
Provide Fallback and elegant service degradation mechanism
-
Provide near-real-time monitoring, alarm and operation and maintenance control means