01 openFeign使用过程中如何差异化配置超时时间

vvEcho 2025-03-04 15:38:31
Categories: Tags:

全局默认:通过 Feign + Ribbon + Hystrix 的全局配置实现 2秒超时。
特定接口:通过 @HystrixCommand 注解或 commandKey 配置覆盖为 30秒。
验证关键点:确保 Hystrix 超时覆盖 Ribbon 和 Feign,避免因优先级导致配置失效

服务消费方全局超时配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Feign 全局超时
feign:
client:
config:
default:
connectTimeout: 2000 # 连接超时2秒
readTimeout: 2000 # 读取超时2秒

# Ribbon 超时(需大于等于Feign超时)
ribbon:
ConnectTimeout: 2000 # 连接超时2秒
ReadTimeout: 2000 # 读取超时2秒

# Hystrix 默认超时(需覆盖Ribbon)
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 2000 # Hystrix全局超时2秒
circuitBreaker:
requestVolumeThreshold: 5 # 触发熔断的最小请求数
errorThresholdPercentage: 50 # 错误率阈值

服务消费方特定接口超时配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@FeignClient(name = "slow-service", fallback = SlowServiceFallback.class)
public interface SlowServiceClient {

@GetMapping("/slow-process")
@HystrixCommand(commandProperties = {
@HystrixProperty(
name = "execution.isolation.thread.timeoutInMilliseconds",
value = "30000" // 覆盖为30秒超时
)
})
String slowProcess();
}

@Component
public class SlowServiceFallback implements SlowServiceClient {
@Override
public String slowProcess() {
return "降级响应:接口处理超时";
}
}