使用配置项
寫到代碼里都是寫死的,我能不能寫到配置項(xiàng)里面去呢,沒錯(cuò),這也是可以的,我們先從上面的超時(shí)開始,我把這個(gè)也注釋掉,我把超時(shí)拷貝一下,寫到配置項(xiàng)里面,為了演示,我先寫到本地的文件,就先不放到統(tǒng)一配置中心了,拷貝過來之后就是這個(gè)樣子的hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 3000hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000這里前面還要配置一些參數(shù),用yml配置這個(gè)的時(shí)候真不美觀,如果是properties方式的話,那么一行就寫完了,這個(gè)跟走樓梯一樣的,還是得這么配,我們?cè)賮韱?dòng)一下,大家覺得這樣的配置能成功嗎,我們之前配置的超時(shí)是5秒,來刷新一下https://blog.51cto.com/zero01/2173377https://www.jianshu.com/p/2dda8ac85a27可以的http://localhost:8010/getProductInfoList?number=1就是正常的,其實(shí)大家忽略了一個(gè)點(diǎn),我改成1秒了hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000我現(xiàn)在改成1秒,我改成1秒,這邊超時(shí)是兩秒,那這里是要出現(xiàn)服務(wù)降級(jí)才對(duì),我們?cè)賮砜匆幌?你看為什么沒有出現(xiàn)服務(wù)降級(jí)呢,所以我們這個(gè)配置壓根就沒有生效,什么原因呢,我們沒有加注解,大家千萬(wàn)要注意這一點(diǎn)@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);}如果要做到服務(wù)降級(jí)的話,第一步一定要記得加上這個(gè)注解,不然是沒用的,再來刷新http://localhost:8010/getProductInfoList?number=1現(xiàn)在就出現(xiàn)太擁擠了,說明配置已經(jīng)生效,配置的是一秒,大家看到我這里配置的是defaulthystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000默認(rèn)的就是全局的作用,那如果我想單獨(dú)為這個(gè)方法設(shè)置一個(gè)超時(shí)時(shí)間呢,怎么做呢,大家可以看到這里有一個(gè)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這是你自己可以設(shè)置的一個(gè)值,那默認(rèn)如果沒設(shè)置是什么呢,他這里注釋已經(jīng)寫得非常清楚了,可以看一下,假設(shè)你這個(gè)方法是這么來寫的,那默認(rèn)的就是方法名,其實(shí)大家可以看一下他的注解,看一下他的源碼,還是很有用的,那既然他的CommandKey是這個(gè),方法是這個(gè)getProductInfoList,我為這個(gè)方法單獨(dú)設(shè)置一個(gè)超時(shí)時(shí)間hystrix:command:getProductInfoList:execution:isolation:thread:timeoutInMilliseconds: 3000hystrix.command.getProductInfoList.execution.isolation.thread.timeoutInMilliseconds=5000注意對(duì)齊,default和我自定義的,同一級(jí),這邊設(shè)置5秒,再試一下,再來訪問,看這個(gè)時(shí)候就可以訪問到了
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、超時(shí)設(shè)置
// @HystrixCommand(commandProperties = {
// @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") //超時(shí)時(shí)間設(shè)置為3秒
// })
// 3.
// @HystrixCommand(commandProperties = {
// @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//設(shè)置熔斷
// @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 "太擁擠了,請(qǐng)稍后再試~~";}private String defaultFallback(){return "默認(rèn)提示:太擁擠了,請(qǐng)稍后再試~~";}
}
?
總結(jié)
- 上一篇: 探讨断路器模式
- 下一篇: feign-hystrix的使用