@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.1X-Span-Name: http:/product/queryCategoriesCodeByProductCodeX-B3-SpanId: 25fdbf1bf842dd8fX-B3-ParentSpanId: 6b2b0946e4957196X-B3-Sampled: 0X-B3-TraceId: 6b2b0946e4957196Content-Length: 6Content-Type: text/plain;charset=UTF-8123132dominos-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 samplingdominos-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 ...
@Configurationpublic 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) ); } }); }}