Provider实现熔断(是否断,请求次数,请求窗口期,请求失败率)。
比如1分钟内,每一秒请求一次,失败了60次,就启用熔断。
1. 改payment8001的service即可。
@Service
public class PaymentService
{//=====服务熔断
@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("******id 不能负数");
}
String serialNumber = IdUtil.simpleUUID();
return Thread.currentThread().getName()+"\t"+"调用成功,流水号: " + serialNumber;
}
}
2.测试
测试 7001 8001 http://localhost:8001/payment/circuit/31 正常
http://localhost:8001/payment/circuit/-31 异常
http://localhost:8001/payment/circuit/11
正数:成功 paymentCircuitBreaker()
负数:丢异常,调用的是paymentCircuitBreaker_fallback()
错误率高:熔断
下面是网上找了张截图,便于大家理解。