生产技巧:Feign如何控制Hystrix的启停、超时、熔断?

生产技巧:Feign如何控制Hystrix的启停、超时、熔断?

这也是一篇写于2017-08前后的工作日志,当时由于项目比较多,很多团队对Feign和Hystrix之间的小暧昧搞不清楚,所以写了本篇文章,希望对大家的工作有所帮助。

  1. 要想全局关闭Hystrix,只需使用如下配置即可:

  • feign.hystrix.enabled: false
    

    这样,就会为所有服务关闭掉Feign的Hystrix支持。也就是说:A服务调用B服务,如果在A服务上设置该属性,A服务的所有Feign Client都不会再有Hystrix熔断的能力了。

  • 全局配置够灵活,一般不能满足实际项目的要求。实际项目中,往往需要精确到指定服务的细粒度配置。例如:调用服务a时关闭Hystrix,调用b服务时打开Hystrix。可如下配置:

@FeignClient(name="a", configuration = FooConfiguration.class)

那么,这个FooConfiguration只需要编写如下即可:

  • public class FooConfiguration {
        @Bean
    	@Scope("prototype")
    	public Feign.Builder feignBuilder() {
    		return Feign.builder();
    	}
    }
    

    这样,对于name = "a" 的Feign Client都会关闭Hystrix支持。

  • 很多场景下,关闭Hystrix相对暴力,特别是上文编写代码的方式。很多时候,我们可能更希望只是关闭熔断,抑或是关闭超时保护。此时要怎么搞呢?

    关闭熔断:

# 全局关闭熔断:
hystrix.command.default.circuitBreaker.enabled: false
# 局部关闭熔断:
hystrix.command.<HystrixCommandKey>.circuitBreaker.enabled: false

设置超时:

# 全局设置超时:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1000
# 局部设置超时:
hystrix.command.<HystrixCommandKey>.execution.isolation.thread.timeoutInMilliseconds: 1000

关闭超时:

  1. # 全局关闭:
    hystrix.command.default.execution.timeout.enabled: false
    # 局部关闭:
    hystrix.command.<HystrixCommandKey>.execution.timeout.enabled: false
    

    其中的<HystrixCommandKey> ,是个变量,可以打开服务的hystrix.stream 端点即可看到,也可在Hystrix Dashboard中查看。

相关文章

本文链接生产技巧:Feign如何控制Hystrix的启停、超时、熔断?

转载声明:本博客由周立创作,采用 CC BY 3.0 CN 许可协议。可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。

Feign结合Hystrix熔断时间配置通常在Feign客户端的配置中完成。具体来说,可以通过配置`hystrix.command.default`属性来设置熔断时间。以下是一个配置示例: ```java @Configuration public class FeignConfig { @Bean public Feign.Builder feignBuilder() { return Feign.builder() .encoder(new JacksonEncoder()) .decoder(new JacksonDecoder()) .requestInterceptor(template -> template.header("Accept", "application/json")) .options(new Request.Options(5000, 10000)); // 连接超时和读取超时设置 } @Bean @ConfigurationProperties(prefix = "hystrix.command.default") public HystrixPropertiesConfig hystrixProperties() { return new HystrixPropertiesConfig(); } @ConfigurationProperties(prefix = "hystrix.threadpool.default") @Bean public ThreadPoolTaskExecutor threadPoolTaskExecutor() { return new ThreadPoolTaskExecutor(); } public static class HystrixPropertiesConfig { private int executionTimeoutInMilliseconds = 1000; // 默认超时时间 public int getExecutionTimeoutInMilliseconds() { return executionTimeoutInMilliseconds; } public void setExecutionTimeoutInMilliseconds(int executionTimeoutInMilliseconds) { this.executionTimeoutInMilliseconds = executionTimeoutInMilliseconds; } } } ``` 在这个配置类中,`hystrixProperties`是一个bean,通过`@ConfigurationProperties`注解绑定配置文件中的`hystrix.command.default`属性。其中,`executionTimeoutInMilliseconds`属性就是设置熔断超时时间的配置项。 在`application.properties`或者`application.yml`文件中进行相应的配置: ```properties # application.properties 示例 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000 ``` 或者使用yaml格式: ```yaml # application.yml 示例 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 1000 ``` 这里配置的`timeoutInMilliseconds`就是熔断器打开的超时时间,也就是当服务调用超过这个时间没有响应时,Hystrix触发熔断逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值