2019独角兽企业重金招聘Python工程师标准>>>
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.netflix.feign;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.support.ResponseEntityDecoder;
import org.springframework.cloud.netflix.feign.support.SpringDecoder;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.cloud.netflix.feign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;
import com.netflix.hystrix.HystrixCommand;
import feign.Contract;
import feign.Feign;
import feign.Logger;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.hystrix.HystrixFeign;
/**
* @author Dave Syer
* @author Venil Noronha
*/
@Configuration
public class FeignClientsConfiguration {
@Autowired
private ObjectFactory messageConverters;
@Autowired(required = false)
private List parameterProcessors = new ArrayList<>();
@Autowired(required = false)
private List feignFormatterRegistrars = new ArrayList<>();
@Autowired(required = false)
private Logger logger;
@Bean
@ConditionalOnMissingBean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(this.messageConverters));
}
@Bean
@ConditionalOnMissingBean
public Encoder feignEncoder() {
return new SpringEncoder(this.messageConverters);
}
@Bean
@ConditionalOnMissingBean
public Contract feignContract(ConversionService feignConversionService) {
return new SpringMvcContract(this.parameterProcessors, feignConversionService);
}
@Bean
public FormattingConversionService feignConversionService() {
FormattingConversionService conversionService = new DefaultFormattingConversionService();
for (FeignFormatterRegistrar feignFormatterRegistrar : feignFormatterRegistrars) {
feignFormatterRegistrar.registerFormatters(conversionService);
}
return conversionService;
}
@Configuration
@ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
protected static class HystrixFeignConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "feign.hystrix.enabled", matchIfMissing = true)
public Feign.Builder feignHystrixBuilder() {
return HystrixFeign.builder();
}
}
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
public Feign.Builder feignBuilder() {
return Feign.builder();
}
@Bean
@ConditionalOnMissingBean(FeignLoggerFactory.class)
public FeignLoggerFactory feignLoggerFactory() {
return new DefaultFeignLoggerFactory(logger);
}
}
@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)// 超时配置
);
}
});
}
默认超时时间是1秒
com.netflix.hystrix.HystrixCommandProperties.default_executionTimeoutInMilliseconds
https://my.oschina.net/xiaominmin/blog/3034313
和上一篇差不多,就多了一个feignHystrixBuilder配置,设置了hystrix的配置。
请求是线hystrix (默认超时1秒)> ribbon > feign (默认 this(10 * 1000, 60 * 1000))
log:
dominos-pe | 2019-04-09 20:52:16.596 | main | DEBUG | c.n.hystrix.strategy.HystrixPlugins.resolveDynamicProperties(386) | Created HystrixDynamicProperties. Using class : com.netflix.hystrix.strategy.properties.archaius.HystrixDynamicPropertiesArchaius
dominos-pe | 2019-04-09 20:52:17.261 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.circuitBreaker.enabled to use NEXT property: hystrix.command.default.circuitBreaker.enabled = true
dominos-pe | 2019-04-09 20:52:17.323 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.circuitBreaker.requestVolumeThreshold to use NEXT property: hystrix.command.default.circuitBreaker.requestVolumeThreshold = 20
dominos-pe | 2019-04-09 20:52:17.337 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(90) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.circuitBreaker.sleepWindowInMilliseconds to use its current value: 12000
dominos-pe | 2019-04-09 20:52:17.350 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.circuitBreaker.errorThresholdPercentage to use NEXT property: hystrix.command.default.circuitBreaker.errorThresholdPercentage = 50
dominos-pe | 2019-04-09 20:52:17.363 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.circuitBreaker.forceOpen to use NEXT property: hystrix.command.default.circuitBreaker.forceOpen = false
dominos-pe | 2019-04-09 20:52:17.375 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.circuitBreaker.forceClosed to use NEXT property: hystrix.command.default.circuitBreaker.forceClosed = false
dominos-pe | 2019-04-09 20:52:17.427 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.execution.isolation.strategy to use NEXT property: hystrix.command.default.execution.isolation.strategy = THREAD
dominos-pe | 2019-04-09 20:52:17.462 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(90) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.execution.isolation.thread.timeoutInMilliseconds to use its current value: 12000
dominos-pe | 2019-04-09 20:52:17.474 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.execution.timeout.enabled to use NEXT property: hystrix.command.default.execution.timeout.enabled = true
dominos-pe | 2019-04-09 20:52:17.487 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.execution.isolation.thread.interruptOnTimeout to use NEXT property: hystrix.command.default.execution.isolation.thread.interruptOnTimeout = true
dominos-pe | 2019-04-09 20:52:17.501 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.execution.isolation.thread.interruptOnFutureCancel to use NEXT property: hystrix.command.default.execution.isolation.thread.interruptOnFutureCancel = false
dominos-pe | 2019-04-09 20:52:17.513 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.execution.isolation.semaphore.maxConcurrentRequests to use NEXT property: hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests = 10
dominos-pe | 2019-04-09 20:52:17.528 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.fallback.isolation.semaphore.maxConcurrentRequests to use NEXT property: hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests = 10
dominos-pe | 2019-04-09 20:52:17.539 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.fallback.enabled to use NEXT property: hystrix.command.default.fallback.enabled = true
dominos-pe | 2019-04-09 20:52:17.550 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.metrics.rollingStats.timeInMilliseconds to use NEXT property: hystrix.command.default.metrics.rollingStats.timeInMilliseconds = 10000
dominos-pe | 2019-04-09 20:52:17.562 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.metrics.rollingStats.numBuckets to use NEXT property: hystrix.command.default.metrics.rollingStats.numBuckets = 10
dominos-pe | 2019-04-09 20:52:17.575 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.metrics.rollingPercentile.enabled to use NEXT property: hystrix.command.default.metrics.rollingPercentile.enabled = true
dominos-pe | 2019-04-09 20:52:17.588 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.metrics.rollingPercentile.timeInMilliseconds to use NEXT property: hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds = 60000
dominos-pe | 2019-04-09 20:52:17.600 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.metrics.rollingPercentile.numBuckets to use NEXT property: hystrix.command.default.metrics.rollingPercentile.numBuckets = 6
dominos-pe | 2019-04-09 20:52:17.612 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.metrics.rollingPercentile.bucketSize to use NEXT property: hystrix.command.default.metrics.rollingPercentile.bucketSize = 100
dominos-pe | 2019-04-09 20:52:17.625 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.metrics.healthSnapshot.intervalInMilliseconds to use NEXT property: hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds = 500
dominos-pe | 2019-04-09 20:52:17.639 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.requestCache.enabled to use NEXT property: hystrix.command.default.requestCache.enabled = true
dominos-pe | 2019-04-09 20:52:17.653 | main | DEBUG | c.n.h.s.p.HystrixPropertiesChainedProperty.checkAndFlip(93) | Flipping property: hystrix.command.queryCategoriesCodeByProductCode2.requestLog.enabled to use NEXT property: hystrix.command.default.requestLog.enabled = true
dominos-pe | 2019-04-09 20:52:23.254 | 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:52:23.257 | 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:52:23.259 | 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:52:23.261 | 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:52:23.263 | 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:52:23.265 | 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:52:23.889 | 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:52:23.889 | hystrix-dominos-im-1 | DEBUG | o.s.c.n.feign.support.SpringEncoder.encode(77) | Writing [123132] using [org.springframework.http.converter.StringHttpMessageConverter@cf08c97]
dominos-pe | 2019-04-09 20:52:33.984 | hystrix-dominos-im-1 | DEBUG | com.netflix.hystrix.AbstractCommand.handleFailureViaFallback(1010) | Error executing HystrixCommand.run(). Proceeding to fallback logic ...
feign.RetryableException: Read timed out executing POST http://localhost:9005/product/queryCategoriesCodeByProductCode
at feign.FeignException.errorExecuting(FeignException.java:67)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:104)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76)
at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:108)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:301)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:297)
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10211)
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51)
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
at rx.Observable.unsafeSubscribe(Observable.java:10211)
at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41)
at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10211)
at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:56)
at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:47)
at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction.call(HystrixContexSchedulerAction.java:69)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at okio.Okio$2.read(Okio.java:140)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:355)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:227)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:221)
at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:775)
at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:86)
at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:760)
at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:613)
at okhttp3.RealCall.getResponse(RealCall.java:244)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
at okhttp3.RealCall.execute(RealCall.java:57)
at feign.okhttp.OkHttpClient.execute(OkHttpClient.java:157)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:97)
... 31 common frames omitted
=====null
从log种可以看到关键类:SynchronousMethodHandler
还有Hystrix的指标