在使用 Feign 时,如果需要在代码中写死 feign.hystrix.enabled 属性,而不读取配置文件内容,可以使用 @FeignClient 注解的 configuration 属性来自定义 Feign 的配置类。在该配置类中,可以通过 @Bean 注解手动创建 HystrixFeign.Builder 实例,并设置 hystrixEnabled 属性为 false。以下是一个示例:

@Configuration
public class FeignConfiguration {

    @Bean
    public HystrixFeign.Builder feignBuilder() {
        return HystrixFeign.builder().setterFactory((target, method) -> {
            HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
                    .withExecutionTimeoutInMilliseconds(10000)
                    .withCircuitBreakerEnabled(false); // 设置 hystrixEnabled 为 false
            return HystrixCommand.Setter
                    .withGroupKey(HystrixCommandGroupKey.Factory.asKey(target.name()))
                    .andCommandKey(HystrixCommandKey.Factory.asKey(Feign.configKey(target.type(), method)))
                    .andCommandPropertiesDefaults(setter);
        });
    }
}

然后,在使用 @FeignClient 注解的接口上添加 configuration 属性,指定上述自定义的 Feign 配置类:

@FeignClient(name = "example", configuration = FeignConfiguration.class)
public interface ExampleClient {
    // ...
}

通过上述配置,feign.hystrix.enabled 属性将被写死为 false,而不会从配置文件中读取。

Feign Hystrix 禁用:代码写死 `feign.hystrix.enabled` 属性

原文地址: https://www.cveoy.top/t/topic/Mob 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录