13. CircuitBreaker: Hystrix Clients
Hystrix
断路器
13.1 How to Include Hystrix
服务消费者microservice-customer-movie-ribbon-withHistrict2上面写断路器。
如果服务提供者microservice-provider-user异常,就会发生短路,执行写好的程序
1.启动类增加注解@EnableCircuitBreaker
2.controller
@controller
@RestController
public class UseDemoController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/demo")
@HystrixCommand(fallbackMethod = "findByIdFallback")//设置断路器,如果连接不上,就执行断路器“findByIdFallback”
public String useDemo() {
return restTemplate.getForObject("http://microservice-provider-user/demo/dd", String.class);//参数1使eureka的service-id,是个虚拟主机名,可以用eureka.instance.secure.virtual-host-name定义,如果不是https的,就用eureka.instance.virtual-host-name
}
//断路器方法
public String findByIdFallback() {
return " ----findByIdFallback---";
}
}
13.2Propagating the Security Context orusing Spring Scopes(传播安全上下文,和springscopes)
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 limitedby the number of threads in the thread-pool(它是一个单独的线程上执行,并发请求受线程池中的线程数量的限制,默认策略)
SEMAPHORE——it executes on the calling thread and concurrent requests arelimited by the semaphore count(它在调用线程上执行,并发请求受到信号量计数的限制)
hystrix.shareSecurityContext :spring security上下文
13.3 Health Indicator
/health 暴露了连接的断路器的状态
13.4 Hystrix Metrics Stream
hystrix.stream作为管理端口