Skip to main content
 首页 » 编程设计

SpringCloud之Hystrix-超时设置

2022年07月18日53Terrylee

由于客户端请求服务端方法时,服务端方法响应超过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=3000 
 
2.配置具体方法的超时时间 
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 //开启断路器 
@SpringCloudApplication 
public class FajunClientTestApplication { 
 
    public static void main(String[] args) { 
        SpringApplication.run(FajunClientTestApplication.class, args); 
    } 
}
复制代码

4.pom:

<!--hystrix--> 
<dependency> 
  <groupId>org.springframework.cloud</groupId> 
  <artifactId>spring-cloud-starter-hystrix</artifactId> 
</dependency>

本文参考链接:https://www.cnblogs.com/maohuidong/p/9858089.html