使用配置项
                            
                            
                            寫到代碼里都是寫死的,我能不能寫到配置項里面去呢,沒錯,這也是可以的,我們先從上面的超時開始,我把這個也注釋掉,我把超時拷貝一下,寫到配置項里面,為了演示,我先寫到本地的文件,就先不放到統一配置中心了,拷貝過來之后就是這個樣子的hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 3000hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000這里前面還要配置一些參數,用yml配置這個的時候真不美觀,如果是properties方式的話,那么一行就寫完了,這個跟走樓梯一樣的,還是得這么配,我們再來啟動一下,大家覺得這樣的配置能成功嗎,我們之前配置的超時是5秒,來刷新一下https://blog.51cto.com/zero01/2173377https://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現在就出現太擁擠了,說明配置已經生效,配置的是一秒,大家看到我這里配置的是defaulthystrix.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: 3000hystrix.command.getProductInfoList.execution.isolation.thread.timeoutInMilliseconds=5000注意對齊,default和我自定義的,同一級,這邊設置5秒,再試一下,再來訪問,看這個時候就可以訪問到了 
server.port=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.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=5672spring.cloud.stream.bindings.myMessage.group=order
spring.cloud.stream.bindings.myMessage.content-type=application/jsonhystrix.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 "默認提示:太擁擠了,請稍后再試~~";}
}
 
                        ?
總結
                            
                        - 上一篇: 探讨断路器模式
 - 下一篇: feign-hystrix的使用