超时设置
注解就調用一個fallback方法,如果有很多的話,是不是得寫很多,他這里提供了一個默認的@DefaultProperties,里面有一個defaultFallback,為了以示區別,我們這里加幾個字吧,默認提示太擁擠了,這里你要服務降級的話,你可以直接加這個注解,@HystrixCommand,里面可以什么都不用填,這個時候他就是使用默認的fallback,我們來測試一下http://localhost:8010/getProductInfoList讓大家看一下這塊的代碼,啟動好了,我們再來測一下,默認提示太擁擠了@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController { //@HystrixCommand(fallbackMethod = "fallback")@HystrixCommand@GetMapping("/getProductInfoList")public String getProductInfoList(){RestTemplate restTemplate = new RestTemplate();return restTemplate.postForObject("http://127.0.0.1:7900/product/listForOrder", Arrays.asList("157875196366160022"),String.class); }private String fallback(){return "太擁擠了,請稍后再試~~";}private String defaultFallback(){return "默認提示:太擁擠了,請稍后再試~~";}
}核心業務的方法,比如買家查詢,那我們就得優先保證他,他用了原始的注解,指定特殊的降級處理邏輯,在fallback方法里面執行,里面是50%的概率是可以正常處理的,那其他方法是非核心的,他們統一的降級策略,就是直接回到一個靜態頁面之內,這個大家可以腦補一下,其實更多的場景,要注意服務的訪問時間,也就是超時的問題,比如我在這里加一個sleep@PostMapping("/listForOrder")
public List<ProductInfo> listForOrder(@RequestBody List<String> productIdList){System.out.println("============"+productIdList.get(0)+"============");try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}ProductInfo p = new ProductInfo();p.setProductId(productIdList.get(0));p.setProductName("皮蛋粥");List<ProductInfo> list = new ArrayList<ProductInfo>();list.add(p);return list;
}我這里給他sleep兩秒,訪問這個接口,就會休眠2秒,我們再來訪問一下看看,調用方什么都不改,啟動好了,這個時候再來訪問,永遠都是太擁擠了,我們可以打開網絡請求看一下,看一下這個網絡到底訪問了多久,看看1.03秒,這邊其實他是運行了的,代碼是運行的,訪問到了,SQL都打出來了,但是由于這邊休眠時間比較長,2秒了,所以請求方等不及了,直接就返回了,直接就觸發降級,所以我們這里可以配置一個超時時間,用到的是commandProperties,這里面填的數組,只要看到提示跟著去填就好了,大家不用記,這里面有name和value,那超時時間這個值是什么呢,我們來看一下,超時時間的name值很長,也不用去記,是在太長了我覺得去記沒意思,我點開這個注解@HystrixProperty,點進來你就可以看到他的源碼了,HystrixCommandProperties,點開這,就是這個類,你看他這里有很多很多個配置,那么超時時間我們選的是哪一個呢private static final Integer default_executionTimeoutInMilliseconds = 1000; // default => executionTimeoutInMilliseconds: 1000 = 1 secondcom.netflix.hystrix.HystrixCommandProperties.default_executionTimeoutInMilliseconds默認的是一秒,所以這就是為什么呢,所以我們剛剛控制臺里面看到的基本上就是1.0幾秒,1.08,就是由于這個地方,配置的是一秒,我們該如何來配置呢,點進去this.executionTimeoutInMilliseconds = getProperty(propertyPrefix, key,
"execution.isolation.thread.timeoutInMilliseconds",
builder.getExecutionIsolationThreadTimeoutInMilliseconds(),
default_executionTimeoutInMilliseconds);從這個"execution.isolation.thread.timeoutInMilliseconds"配置里面讀,如果讀不到的話,讀不到的話就default,所以我們把這個復制一下,改成多少呢,我們這里是兩秒,超時時間我們改成5秒,重啟一下@HystrixCommand(commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") //超時時間設置為5秒
})再來訪問,現在訪問的時間就比較長了http://localhost:8010/getProductInfoList現在訪問到了吧,我們可以再刷新一下,看一下時間,2.05秒,這就是超時時間的配置,這個很重要,這個超時時間怎么個設置呢,得看什么業務,比如有些場景他請求到外網,請求到第三方,舉個例子吧,比如要開戶,充值,體現,這類業務往往耗時是比較長的,那么你超時時間就得寫的比較長一些,還是得具體情況具體看@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {//@HystrixCommand(fallbackMethod = "fallback")//2、超時設置@HystrixCommand(commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") //超時時間設置為3秒})@GetMapping("/getProductInfoList")public String getProductInfoList(){RestTemplate restTemplate = new RestTemplate();return restTemplate.postForObject("http://127.0.0.1:7900/product/listForOrder", Arrays.asList("157875196366160022"),String.class); }private String fallback(){return "太擁擠了,請稍后再試~~";} private String defaultFallback(){return "默認提示:太擁擠了,請稍后再試~~";}
}
package com.learn.controller;import java.util.Arrays;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {//@HystrixCommand(fallbackMethod = "fallback")//2、超時設置@HystrixCommand(commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") //超時時間設置為3秒})//3.
// @HystrixCommand(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")
// })
// @HystrixCommand@GetMapping("/getProductInfoList")
// public String getProductInfoList(@RequestParam("number") Integer number){public String getProductInfoList(){
// if(number % 2 == 0){
// return "success";
// }RestTemplate restTemplate = new RestTemplate();return restTemplate.postForObject("http://127.0.0.1:7900/product/listForOrder", Arrays.asList("157875196366160022"),String.class);}private String fallback(){return "太擁擠了,請稍后再試~~";}private String defaultFallback(){return "默認提示:太擁擠了,請稍后再試~~";}
}
?
總結