注解就调用一个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 second
com.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 "默认提示:太拥挤了,请稍后再试~~";
}
}