关于Hystrix超时

关于Hystrix超时

对于web的请求有些时候可能会超过2s,这时候需要设置超市请求

Hystrix的作用是对一段程序进行控制,这段程序做了什么无所谓。

程序请求服务的框架是feign,所以要对feign进行控制。

0、OpenFeign的程序

@FeignClient(value = "getname")
public interface OpenFeignTestController {

    @GetMapping("/server3")
    CommonResult getServer3Name();

    @GetMapping("/server1")
    CommonResult getServer1Name();

}

1、配置Hystrix的超时,允许程序运行。

 @GetMapping("/server3")
    @HystrixCommand(fallbackMethod = "fail",commandProperties={
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "4000")
    })
    public CommonResult client3(){
        return openFeignTestController.getServer3Name();
    }

2、配置feign的请求超时,这里是配置http的超时

feign.client.config.default.connect-timeout=4000
feign.client.config.default.read-timeout=4000

3、服务端睡眠2s

 @GetMapping("/server3")
    public CommonResult getServer3Name(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("I am "+server+" and sleep 2s");

        return ResultUtill.Ok(server);
    }

此时请求网关的/server3就可以超时了