SpringCloud 实战十六:Hystrix服务熔断

  作者:记性不好的阁主

1、服务提供者


//====服务格断
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"), // 是否开启断路器
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), //请求次数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 时间窗口日期
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60") // 失败率达到多少后跳闸
})
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
if (id < 0) {
throw new RuntimeException("x不能负数");
}
String serialNumber = IdUtil.simpleUUID();

return Thread.currentThread().getName() + "\t" + "调用成功,流水号: " + serialNumber;
}

public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
return "id不能负数,请稍后再试, /(ToT)/~ id: " + id;
}


2、控制器


@GetMapping("/payment/circuit/{id}")
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
String result = paymentService.paymentCircuitBreaker(id);
log.info("******result: " + result);
return result;
}


3、访问: http://localhost:8001/payment/circuit/1  成功返回



4、在10秒内持续访问:http://localhost:8001/payment/circuit/-1




在10秒中如果失败比率大于60那么就紧急熔断,服务暂时不可用,此时再次访问正常地址也会不可用:

 http://localhost:8001/payment/circuit/1




随后再次访问则恢复正常:





相关推荐

评论 抢沙发

表情

分类选择