01 openFeign使用过程中如何差异化配置超时时间
全局默认:通过 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: client: config: default: connectTimeout: 2000 readTimeout: 2000
ribbon: ConnectTimeout: 2000 ReadTimeout: 2000
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 2000 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 "降级响应:接口处理超时"; } }
|