13. CircuitBreaker: Hystrix Clients
Hystrix
breaker
13.1 How to Include Hystrix
The service consumer microservice-customer-movie-ribbon-withHistrict2 writes the circuit breaker above.
If the service provider microservice-provider-user is abnormal, a short circuit will occur and the written program will be executed
1. Add the annotation @EnableCircuitBreaker to the startup class
2.controller
@controller @RestController public class UseDemoController { @Autowired private RestTemplate restTemplate; @RequestMapping("/demo") @HystrixCommand(fallbackMethod = "findByIdFallback")//Set the circuit breaker, if it cannot be connected, execute the circuit breaker "findByIdFallback" public String useDemo () { return restTemplate.getForObject("http://microservice-provider-user/demo/dd", String.class);//Parameter 1 makes eureka's service-id, which is a virtual host name, you can use eureka.instance.secure. virtual-host-name definition, if not https, use eureka.instance.virtual-host-name } // circuit breaker method public String findByIdFallback() { return " ----findByIdFallback---"; } }
13.2Propagating the Security Context or using Spring Scopes
execution.isolation.strategy
This property indicates which isolation strategyHystrixCommand.run() executes with,one of the following two choices:隔离策略
THREAD——it executes on a separate thread and concurrent requests are limited by the number of threads in the thread-pool (it executes on a separate thread, concurrent requests are limited by the number of threads in the thread pool, the default policy)
SEMAPHORE - it executes on the calling thread and concurrent requests are limited by the semaphore count
hystrix.shareSecurityContext : spring security context
13.3 Health Indicator
/health exposes the state of connected circuit breakers
13.4 Hystrix Metrics Stream
hystrix.stream as management port