1. 程式人生 > 其它 >服務熔斷Hystrix-3(服務消費者Hystrix測試)

服務熔斷Hystrix-3(服務消費者Hystrix測試)

技術標籤:springCloudhystrixjavaspringribbon

服務熔斷Hystrix-3(服務消費者Hystrix測試)

hystrix 預設超過時間是1000毫秒,如果你後端的響應時間超過此時間,就會觸發斷路器。

測試環境 2個提供者 1個消費者 一個註冊中心 

1.修改hystrix 預設的超時時間: 3秒我這裡修改的

 @RequestMapping("/web/hystrix")
    //加入熔斷器註解 對這個方法
    // fallbackMethod 如果出現短路 就返回error方法
    @HystrixCommand(fallbackMethod =
"error",commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000") }) public String hystrix(){ //呼叫springCloud 服務者 提供的服務 /** * 使用url進行呼叫但是使用了 Eureka後 使用服務名來呼叫 */ return
restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/hello",String.class).getBody(); }

2.在1個服務提供者中 新增睡眠

 @RequestMapping("/service/hello")
    public String hello(){

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.
printStackTrace(); } System.out.println("服務提供者---- 1"); return "test"; }

3.測試結果 (負載均衡使用輪轉預設策略)

第一次測試
在這裡插入圖片描述
第二次測試 (3秒後出現error)
在這裡插入圖片描述