由于客户端请求服务端方法时,服务端方法响应超过1秒将会触发降级,所以我们可以配置Hystrix默认的超时配置
如果我们没有配置默认的超时时间,Hystrix将取default_executionTimeoutInMilliseconds作为默认超时时间
this.executionTimeoutInMilliseconds = getProperty(propertyPrefix, key, "execution.isolation.thread.timeoutInMilliseconds", builder.getExecutionIsolationThreadTimeoutInMilliseconds(), default_executionTimeoutInMilliseconds)
1.代码中修改默认超时配置(改为3秒):
@HystrixCommand(commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")}) public String serverMethod() { return null; }
2.application.properties中设置默认超时时间:
1.默认:(方法上记得要加上@HystrixCommand,否则无效):hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=30002.配置具体方法的超时时间hystrix.command.serverMethod.execution.isolation.thread.timeoutInMilliseconds=3000
3.启动类:
package com.wangfajun;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.SpringCloudApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;//@SpringBootApplication//@EnableDiscoveryClient//@EnableCircuitBreaker //开启断路器@SpringCloudApplicationpublic class FajunClientTestApplication { public static void main(String[] args) { SpringApplication.run(FajunClientTestApplication.class, args); }}
4.pom:
org.springframework.cloud spring-cloud-starter-hystrix