1.介绍
- 可以执行主流程,command,也可以执行一个备用降级的command
- 一般来说,都是去执行一个主流程的command,如果说你现在知道有问题了,希望能够手动降级的话,动态给服务发送个请求。在请求中修改标识位,自动就让command以后都直接过来执行备用command
- 3个command,套在最外面的command,是用semaphore信号量做限流和资源隔离的,因为这个command不用去care timeout的问题,嵌套调用的command会自己去管理timeout超时的
2.例子
public class GetProductInfoFacadeCommand extends HystrixCommand<ProductInfo> {
private Long productId;
public GetProductInfoFacadeCommand(Long productId) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ProductInfoService"))
.andCommandKey(HystrixCommandKey.Factory.asKey("GetProductInfoFacadeCommand"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE)
.withExecutionIsolationSemaphoreMaxConcurrentRequests(15)));
this.productId = productId;
}
@Override
protected ProductInfo run() throws Exception {
if(!IsDegrade.isDegrade()) {
return new GetProductInfoCommand(productId).execute();
} else {
return new GetProductInfoFromMySQLCommand(productId).execute();
}
}
@Override
protected ProductInfo getFallback() {
return new ProductInfo();
}
}
public class IsDegrade {
private static boolean degrade = false;
public static boolean isDegrade() {
return degrade;
}
public static void setDegrade(boolean degrade) {
IsDegrade.degrade = degrade;
}
}
@Controller
public class CacheController {
@RequestMapping("/isDegrade")
@ResponseBody
public String isDegrade(boolean degrade) {
IsDegrade.setDegrade(degrade);
return "success";
}
}