feign feign.hystrix.enabled=true spring.sleuth.enabled=true的超時時間和重試次數
阿新 • • 發佈:2019-04-09
@Bean public Request.Options options() { return new Request.Options(10000 * 1, 10000 * 1); } @Bean Retryer feignRetryer() { return Retryer.NEVER_RETRY; } @Bean public Feign.Builder feignHystrixBuilder() { return HystrixFeign.builder().setterFactory(new SetterFactory() { public HystrixCommand.Setter create(Target<?> target, Method method) { String groupKey = target.name(); //@FeignClient(name = "dominos-im" System.out.println(groupKey); String commandKey = method.getName(); System.out.println("commandKey=="+commandKey); int time = 12000; return HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))// 控制 RemoteProductService 下,所有方法的Hystrix Configuration .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)) .andCommandPropertiesDefaults( HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(time) .withCircuitBreakerSleepWindowInMilliseconds(time)// 超時配置 ); } }); }
feign feign.hystrix.enabled=true
開啟hystrix配置。不自己配置使用預設配置
spring.sleuth.enabled=true
開啟追蹤服務
日誌:
dominos-pe | 2019-04-09 20:48:02.407 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).circuitBreaker.enabled to use NEXT property: hystrix.command.default.circuitBreaker.enabled = true dominos-pe | 2019-04-09 20:48:02.466 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).circuitBreaker.requestVolumeThreshold to use NEXT property: hystrix.command.default.circuitBreaker.requestVolumeThreshold = 20 dominos-pe | 2019-04-09 20:48:02.479 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).circuitBreaker.sleepWindowInMilliseconds to use NEXT property: hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds = 5000 dominos-pe | 2019-04-09 20:48:02.491 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).circuitBreaker.errorThresholdPercentage to use NEXT property: hystrix.command.default.circuitBreaker.errorThresholdPercentage = 50 dominos-pe | 2019-04-09 20:48:02.503 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).circuitBreaker.forceOpen to use NEXT property: hystrix.command.default.circuitBreaker.forceOpen = false dominos-pe | 2019-04-09 20:48:02.517 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).circuitBreaker.forceClosed to use NEXT property: hystrix.command.default.circuitBreaker.forceClosed = false dominos-pe | 2019-04-09 20:48:02.569 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).execution.isolation.strategy to use NEXT property: hystrix.command.default.execution.isolation.strategy = THREAD dominos-pe | 2019-04-09 20:48:02.607 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).execution.isolation.thread.timeoutInMilliseconds to use NEXT property: hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 1000 dominos-pe | 2019-04-09 20:48:02.621 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).execution.timeout.enabled to use NEXT property: hystrix.command.default.execution.timeout.enabled = true dominos-pe | 2019-04-09 20:48:02.637 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).execution.isolation.thread.interruptOnTimeout to use NEXT property: hystrix.command.default.execution.isolation.thread.interruptOnTimeout = true dominos-pe | 2019-04-09 20:48:02.650 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).execution.isolation.thread.interruptOnFutureCancel to use NEXT property: hystrix.command.default.execution.isolation.thread.interruptOnFutureCancel = false dominos-pe | 2019-04-09 20:48:02.664 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).execution.isolation.semaphore.maxConcurrentRequests to use NEXT property: hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests = 10 dominos-pe | 2019-04-09 20:48:02.677 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).fallback.isolation.semaphore.maxConcurrentRequests to use NEXT property: hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests = 10 dominos-pe | 2019-04-09 20:48:02.689 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).fallback.enabled to use NEXT property: hystrix.command.default.fallback.enabled = true dominos-pe | 2019-04-09 20:48:02.701 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).metrics.rollingStats.timeInMilliseconds to use NEXT property: hystrix.command.default.metrics.rollingStats.timeInMilliseconds = 10000 dominos-pe | 2019-04-09 20:48:02.713 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).metrics.rollingStats.numBuckets to use NEXT property: hystrix.command.default.metrics.rollingStats.numBuckets = 10 dominos-pe | 2019-04-09 20:48:02.727 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).metrics.rollingPercentile.enabled to use NEXT property: hystrix.command.default.metrics.rollingPercentile.enabled = true dominos-pe | 2019-04-09 20:48:02.741 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).metrics.rollingPercentile.timeInMilliseconds to use NEXT property: hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds = 60000 dominos-pe | 2019-04-09 20:48:02.754 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).metrics.rollingPercentile.numBuckets to use NEXT property: hystrix.command.default.metrics.rollingPercentile.numBuckets = 6 dominos-pe | 2019-04-09 20:48:02.767 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).metrics.rollingPercentile.bucketSize to use NEXT property: hystrix.command.default.metrics.rollingPercentile.bucketSize = 100 dominos-pe | 2019-04-09 20:48:02.781 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).metrics.healthSnapshot.intervalInMilliseconds to use NEXT property: hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds = 500 dominos-pe | 2019-04-09 20:48:02.793 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).requestCache.enabled to use NEXT property: hystrix.command.default.requestCache.enabled = true dominos-pe | 2019-04-09 20:48:02.804 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.IProductService#queryCategoriesCodeByProductCode2(String).requestLog.enabled to use NEXT property: hystrix.command.default.requestLog.enabled = true dominos-pe | 2019-04-09 20:48:08.879 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.threadpool.dominos-im.coreSize to use NEXT property: hystrix.threadpool.default.coreSize = 10 dominos-pe | 2019-04-09 20:48:08.881 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.threadpool.dominos-im.keepAliveTimeMinutes to use NEXT property: hystrix.threadpool.default.keepAliveTimeMinutes = 1 dominos-pe | 2019-04-09 20:48:08.882 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.threadpool.dominos-im.maxQueueSize to use NEXT property: hystrix.threadpool.default.maxQueueSize = -1 dominos-pe | 2019-04-09 20:48:08.884 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.threadpool.dominos-im.queueSizeRejectionThreshold to use NEXT property: hystrix.threadpool.default.queueSizeRejectionThreshold = 5 dominos-pe | 2019-04-09 20:48:08.886 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.threadpool.dominos-im.metrics.rollingStats.timeInMilliseconds to use NEXT property: hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds = 10000 dominos-pe | 2019-04-09 20:48:08.887 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.threadpool.dominos-im.metrics.rollingStats.numBuckets to use NEXT property: hystrix.threadpool.default.metrics.rollingStats.numBuckets = 10 dominos-pe | 2019-04-09 20:48:09.269 | hystrix-dominos-im-1 | DEBUG | o.s.c.s.i.h.SleuthHystrixConcurrencyStrategy$HystrixTraceCallable.call(142) | Creating new span [Trace: 028f61299d65af5d, Span: 028f61299d65af5d, Parent: null, exportable:false] dominos-pe | 2019-04-09 20:48:09.293 | hystrix-dominos-im-1 | DEBUG | o.s.b.f.s.DefaultListableBeanFactory.doGetBean(251) | Returning cached instance of singleton bean 'messageConverters' dominos-pe | 2019-04-09 20:48:09.293 | hystrix-dominos-im-1 | DEBUG | o.s.c.n.feign.support.SpringEncoder.encode(77) | Writing [123132] using [org.springframework.http.converter.StringHttpMessageConverter@2b540131] dominos-pe | 2019-04-09 20:48:09.298 | hystrix-dominos-im-1 | DEBUG | o.s.b.f.s.DefaultListableBeanFactory.doGetBean(251) | Returning cached instance of singleton bean 'sleuthTracer' dominos-pe | 2019-04-09 20:48:09.298 | hystrix-dominos-im-1 | DEBUG | o.s.c.s.i.w.c.feign.TraceFeignClient.execute(69) | Created new Feign span [Trace: 028f61299d65af5d, Span: 82dbfcc8430fe741, Parent: 028f61299d65af5d, exportable:false] dominos-pe | 2019-04-09 20:48:09.299 | hystrix-dominos-im-1 | DEBUG | o.s.b.f.s.DefaultListableBeanFactory.doGetBean(251) | Returning cached instance of singleton bean 'httpTraceKeysInjector' dominos-pe | 2019-04-09 20:48:09.299 | 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: 82dbfcc8430fe741 X-B3-ParentSpanId: 028f61299d65af5d X-B3-Sampled: 0 X-B3-TraceId: 028f61299d65af5d Content-Length: 6 Content-Type: text/plain;charset=UTF-8 123132 dominos-pe | 2019-04-09 20:48:10.251 | HystrixTimer-1 | DEBUG | o.s.c.s.i.h.SleuthHystrixConcurrencyStrategy$HystrixTraceCallable.call(142) | Creating new span [Trace: 2e2ae3d5c65ee320, Span: 2e2ae3d5c65ee320, Parent: null, exportable:false] dominos-pe | 2019-04-09 20:48:10.268 | HystrixTimer-1 | DEBUG | o.s.c.s.i.h.SleuthHystrixConcurrencyStrategy$HystrixTraceCallable.call(159) | Closing span since it was created[Trace: 2e2ae3d5c65ee320, Span: 2e2ae3d5c65ee320, Parent: null, exportable:false] dominos-pe | 2019-04-09 20:48:10.269 | HystrixTimer-1 | DEBUG | o.s.c.s.zipkin.ZipkinSpanListener.report(216) | The span [Trace: 2e2ae3d5c65ee320, Span: 2e2ae3d5c65ee320, Parent: null, exportable:false] will not be sent to Zipkin due to sampling =====null 從日誌種可以看出幾個關鍵類 org.springframework.cloud.sleuth.instrument.web.client.feign.TraceFeignClient(feign具體的實現類) org.springframework.cloud.sleuth.instrument.web.client.feign.TraceFeignClientAutoConfiguration 以及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 {
@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);
}
}