SleuthHystrixFeign 怎么设置hystrix参数

@Configuration
@ConditionalOnProperty(value = "spring.sleuth.feign.enabled", matchIfMissing = true)
@ConditionalOnClass(Client.class)
@ConditionalOnBean(Tracer.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
@AutoConfigureAfter({SleuthHystrixAutoConfiguration.class, TraceWebAutoConfiguration.class})
public class TraceFeignClientAutoConfiguration {

    /**
     默认一秒超时,hystrix.
     当启动spring.sleuth.enabled=true 会启用配置,SleuthHystrixFeignBuilder.builder从代码层面没法重写该访问,要想设置hystrix超时时间,只能通过配置:
   1.hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=7000
   2.重写该配置类
    *
    /
	@Bean
	@Scope("prototype")
	@ConditionalOnClass(name = "com.netflix.hystrix.HystrixCommand")
	@ConditionalOnProperty(name = "feign.hystrix.enabled", matchIfMissing = true)
	Feign.Builder feignHystrixBuilder(BeanFactory beanFactory) {
		return SleuthHystrixFeignBuilder.builder(beanFactory);
	}

	@Bean
	@ConditionalOnMissingBean
	@Scope("prototype")
	@ConditionalOnProperty(name = "feign.hystrix.enabled", havingValue = "false", matchIfMissing = false)
	Feign.Builder feignBuilder(BeanFactory beanFactory) {
		return SleuthFeignBuilder.builder(beanFactory);
	}

	@Configuration
	@ConditionalOnProperty(name = "spring.sleuth.feign.processor.enabled", matchIfMissing = true)
	protected static class FeignBeanPostProcessorConfiguration {

		@Bean
		FeignBeanPostProcessor feignBeanPostProcessor(TraceFeignObjectWrapper traceFeignObjectWrapper) {
			return new FeignBeanPostProcessor(traceFeignObjectWrapper);
		}

		@Bean
		FeignContextBeanPostProcessor feignContextBeanPostProcessor(BeanFactory beanFactory) {
			return new FeignContextBeanPostProcessor(beanFactory);
		}
	}

	@Bean
	TraceFeignObjectWrapper traceFeignObjectWrapper(BeanFactory beanFactory) {
		return new TraceFeignObjectWrapper(beanFactory);
	}
}

配置:

feign.hystrix.enabled=true
spring.sleuth.enabled=true

 

日志:

dominos-pe | 2019-04-10 15:00:02.362 | hystrix-dominos-im-1 | DEBUG | o.s.c.s.i.h.SleuthHystrixConcurrencyStrategy$HystrixTraceCallable.call(142) | Creating new span [Trace: 6b2b0946e4957196, Span: 6b2b0946e4957196, Parent: null, exportable:false]
dominos-pe | 2019-04-10 15:00:02.391 | hystrix-dominos-im-1 | DEBUG | o.s.b.f.s.DefaultListableBeanFactory.doGetBean(251) | Returning cached instance of singleton bean 'messageConverters'
dominos-pe | 2019-04-10 15:00:02.391 | hystrix-dominos-im-1 | DEBUG | o.s.c.n.feign.support.SpringEncoder.encode(77) | Writing [123132] using [org.springframework.http.converter.StringHttpMessageConverter@b548f51]
dominos-pe | 2019-04-10 15:00:02.398 | hystrix-dominos-im-1 | DEBUG | o.s.b.f.s.DefaultListableBeanFactory.doGetBean(251) | Returning cached instance of singleton bean 'sleuthTracer'
dominos-pe | 2019-04-10 15:00:02.398 | hystrix-dominos-im-1 | DEBUG | o.s.c.s.i.w.c.feign.TraceFeignClient.execute(69) | Created new Feign span [Trace: 6b2b0946e4957196, Span: 25fdbf1bf842dd8f, Parent: 6b2b0946e4957196, exportable:false]
dominos-pe | 2019-04-10 15:00:02.400 | hystrix-dominos-im-1 | DEBUG | o.s.b.f.s.DefaultListableBeanFactory.doGetBean(251) | Returning cached instance of singleton bean 'httpTraceKeysInjector'
dominos-pe | 2019-04-10 15:00:02.400 | hystrix-dominos-im-1 | DEBUG | o.s.c.s.i.w.c.feign.TraceFeignClient.execute(78) | The modified request equals POST http://localhost:9005/product/queryCategoriesCodeByProductCode HTTP/1.1
X-Span-Name: http:/product/queryCategoriesCodeByProductCode
X-B3-SpanId: 25fdbf1bf842dd8f
X-B3-ParentSpanId: 6b2b0946e4957196
X-B3-Sampled: 0
X-B3-TraceId: 6b2b0946e4957196
Content-Length: 6
Content-Type: text/plain;charset=UTF-8

123132
dominos-pe | 2019-04-10 15:00:05.510 | hystrix-dominos-im-1 | DEBUG | o.s.c.s.i.w.c.feign.TraceFeignClient.closeSpan(119) | Closing Feign span [Trace: 6b2b0946e4957196, Span: 25fdbf1bf842dd8f, Parent: 6b2b0946e4957196, exportable:false]
dominos-pe | 2019-04-10 15:00:05.510 | hystrix-dominos-im-1 | DEBUG | o.s.c.s.zipkin.ZipkinSpanListener.report(216) | The span [Trace: 6b2b0946e4957196, Span: 25fdbf1bf842dd8f, Parent: 6b2b0946e4957196, exportable:false] will not be sent to Zipkin due to sampling
dominos-pe | 2019-04-10 15:00:05.516 | hystrix-dominos-im-1 | DEBUG | com.netflix.hystrix.AbstractCommand.handleFailureViaFallback(1010) | Error executing HystrixCommand.run(). Proceeding to fallback logic ...
@Configuration
public class CommonHystrixConfiguration {

	/**
	 * hystrix 超时时间
	 */
	static int hystrixTimeOut = 10000;
	/**
	 * 请求超时时间
	 */
	static int requestTimeOut = 3000;
	
	@Bean
	public Request.Options options() {
		return new Request.Options(requestTimeOut, requestTimeOut);
	}

	@Bean
	Retryer feignRetryer() {
		return new Retryer.Default(100, SECONDS.toMillis(1), 1);
	}

//此配置无效,因为用的是TraceFeignClientAutoConfiguration种的feignHystrixBuilder,默认超时是1秒,只能通过配置文件配置
	@Bean
	public Feign.Builder feignHystrixBuilder() {
		return HystrixFeign.builder().setterFactory(new SetterFactory() {
			public HystrixCommand.Setter create(Target<?> target, Method method) {
				String groupKey = target.name();
				String commandKey = method.getName();
				return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
						.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults(
								HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(hystrixTimeOut)
										.withCircuitBreakerSleepWindowInMilliseconds(hystrixTimeOut)
				);
			}
		});
	}

}

 

转载于:https://my.oschina.net/xiaominmin/blog/3034649

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值