Hystrix修改默认配置有两种方式,注解参数注入,和application.yml配置文件配置。
1、方法一:注解参数注入
@RequestMapping(value = "/helloHystrixA/{string}", method = RequestMethod.GET)
@HystrixCommand(fallbackMethod = "testFallback", // 请求失败降级回调方法,值为方法名,不需要括号
commandProperties = {// 针对单个方法的配置
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"), // 开启熔断器,可不加默认为true
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"), // 请求错误超过50%,开启熔断器
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), // 一个周期(十秒)内超过10个请求才进行进行容错率判断
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),// 开启熔断器后过10秒再尝试访问
})
public String helloHystirxA(@PathVariable String string) {
return "Nacos服务发现:远端调用成功! result="
+ restTemplate.getForObject("http://nacos.provider.demo/hello/" + string, String.class);
大约 2 分钟