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

原文:http://www.itmuch.com/spring-cloud-sum/feign-hystrix/ ,转载请说明出处。java

这也是一篇写于2017-08先后的工做日志,当时因为项目比较多,不少团队对Feign和Hystrix之间的小暧昧搞不清楚,因此写了本篇文章,但愿对你们的工做有所帮助。spring

  1. 要想全局关闭Hystrix,只需使用以下配置便可:ui

    feign.hystrix.enabled: false

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

  2. 全局配置够灵活,通常不能知足实际项目的要求。实际项目中,每每须要精确到指定服务的细粒度配置。例如:调用服务a时关闭Hystrix,调用b服务时打开Hystrix。可以下配置:日志

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

    那么,这个FooConfiguration只须要编写以下便可:code

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

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

  3. 不少场景下,关闭Hystrix相对暴力,特别是上文编写代码的方式。不少时候,咱们可能更但愿只是关闭熔断,抑或是关闭超时保护。此时要怎么搞呢?get

    关闭熔断:it

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

    设置超时:io

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

    关闭超时:

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

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

相关文章
相关标签/搜索