写到代码里都是写死的,我能不能写到配置项里面去呢,没错,这也是可以的,我们先从上面的超时开始,我把这个
也注释掉,我把超时拷贝一下,写到配置项里面,为了演示,我先写到本地的文件,就先不放到统一配置中心了,拷贝
过来之后就是这个样子的
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000
这里前面还要配置一些参数,用yml配置这个的时候真不美观,如果是properties方式的话,那么一行就写完了,这个跟走楼梯
一样的,还是得这么配,我们再来启动一下,大家觉得这样的配置能成功吗,我们之前配置的超时是5秒,来刷新一下
https://blog.51cto.com/zero01/2173377
https://www.jianshu.com/p/2dda8ac85a27
可以的
http://localhost:8010/getProductInfoList?number=1
就是正常的,其实大家忽略了一个点,我改成1秒了
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
我现在改成1秒,我改成1秒,这边超时是两秒,那这里是要出现服务降级才对,我们再来看一下,你看为什么没有
出现服务降级呢,所以我们这个配置压根就没有生效,什么原因呢,我们没有加注解,大家千万要注意这一点
@HystrixCommand
@GetMapping("/getProductInfoList")
public String getProductInfoList(@RequestParam("number") Integer number){
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);
}
如果要做到服务降级的话,第一步一定要记得加上这个注解,不然是没用的,再来刷新
http://localhost:8010/getProductInfoList?number=1
现在就出现太拥挤了,说明配置已经生效,配置的是一秒,大家看到我这里配置的是default
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
默认的就是全局的作用,那如果我想单独为这个方法设置一个超时时间呢,怎么做呢,大家可以看到这里有一个
commandKey
/**
* Hystrix command key.
* <p/>
* default => the name of annotated method. for example:
* <code>
* ...
* @HystrixCommand
* public User getUserById(...)
* ...
* the command name will be: 'getUserById'
* </code>
*
* @return command key
*/
String commandKey() default "";
commandKey这是你自己可以设置的一个值,那默认如果没设置是什么呢,他这里注释已经写得非常清楚了,可以看一下,
假设你这个方法是这么来写的,那默认的就是方法名,其实大家可以看一下他的注解,看一下他的源码,还是很有用的,
那既然他的CommandKey是这个,方法是这个getProductInfoList,我为这个方法单独设置一个超时时间
hystrix:
command:
getProductInfoList:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
hystrix.command.getProductInfoList.execution.isolation.thread.timeoutInMilliseconds=5000
注意对齐,default和我自定义的,同一级,这边设置5秒,再试一下,再来访问,看这个时候就可以访问到了
server.port=8010
eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eureka
spring.application.name=order
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
spring.rabbitmq.host=59.110.158.145
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.port=5672
spring.cloud.stream.bindings.myMessage.group=order
spring.cloud.stream.bindings.myMessage.content-type=application/json
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
hystrix.command.getProductInfoList.execution.isolation.thread.timeoutInMilliseconds=5000
package com.learn.controller;
import java.util.Arrays;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
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;
@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 "默认提示:太拥挤了,请稍后再试~~";
}
}