Spring Cloud配置假装回退(CircuitBreaker)规则

时间:2017-03-08 08:57:24

标签: spring-cloud hystrix spring-cloud-netflix spring-cloud-feign

现在我使用feys with hystrix,事实证明当回退方法在5s内调用20次时,Circuit将转为Open状态。我该如何更改此规则。例如,当回退方法在5s内调用50次或者通过回退回调率时,让Circuit状态变为打开。 这是我的主要java代码。

ConsumerApplication.java

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
@RibbonClients({@RibbonClient(name = "cloud-provider", configuration = CloudProviderConfiguration.class)})
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

UserFeignClient.java

@FeignClient(name = "cloud-provider", fallback = UserFeignClient.HystrixClientFallback.class)
public interface UserFeignClient {
    @RequestMapping("/{id}")
    BaseResponse findByIdFeign(@RequestParam("id") Long id);

    @RequestMapping("/add")
    BaseResponse addUserFeign(UserVo userVo);

    @Component
    class HystrixClientFallback implements UserFeignClient {
        private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFallback.class);

        @Override
        public BaseResponse findByIdFeign(@RequestParam("id") Long id) {
            BaseResponse response = new BaseResponse();
            response.setMessage("disable!!!!");
            return response;
        }

        @Override
        public BaseResponse addUserFeign(UserVo userVo) {
            BaseResponse response = new BaseResponse();
            response.setMessage("disable");
            return response;
        }
    }
}

1 个答案:

答案 0 :(得分:5)

配置参数在此处描述https://github.com/Netflix/Hystrix/wiki/Configuration

hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds会更改您看到的5秒窗口。

hystrix.command.default.circuitBreaker.requestVolumeThreshold默认为20个请求。