
Spring Cloud Feign 之Hystrix
环境信息: java 1.8、Spring boot 1.5.10.RELEASE、spring cloud-Edgware.SR3、maven 3.3+
本章节只针对Hystrix在Feign中的简单实用,和一些简单的源码分析,更详细的请参考GitHub官网
Fallback章节已经简单的介绍了Hystrix的熔断保护使用,这章将介绍HystrixCommand、Observable、Single、Completable这四种异步HTTP请求方式。
那么Hystrix为什么可以支持者四种异步HTTP请求方式,其实很简单通过一段源码就可以看出
HystrixDelegatingContract.parseAndValidatateMetadata方法的四个if else判断分别对应四个类
package feign.hystrix;
import static feign.Util.resolveLastTypeParameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import com.netflix.hystrix.HystrixCommand;
import feign.Contract;
import feign.MethodMetadata;
import rx.Completable;
import rx.Observable;
import rx.Single;
/**
* This special cases methods that return {@link HystrixCommand}, {@link Observable}, or {@link Single} so that they
* are decoded properly.
*
* <p>For example, {@literal HystrixCommand<Foo>} and {@literal Observable<Foo>} will decode {@code Foo}.
*/
// Visible for use in custom Hystrix invocation handlers
public final class HystrixDelegatingContract implements Contract {
private final Contract delegate;
public HystrixDelegatingContract(Contract delegate) {
this.delegate = delegate;
}
@Override
public List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType) {
List<MethodMetadata> metadatas = this.delegate.parseAndValidatateMetadata(targetType);
for (MethodMetadata metadata : metadatas) {
Type type = metadata.returnType();
if (type instanceof ParameterizedType && ((ParameterizedType) type).getRawType(

本文介绍了Spring Cloud Feign中如何利用Hystrix进行服务间的容错处理,包括HystrixCommand的execute和observe/toObservable方法,以及Observable、Single和Completable在Feign中的应用,讲解了它们的工作原理和异步请求的实现方式。此外,还总结了Feign使用Hystrix时的配置和超时设置。
最低0.47元/天 解锁文章
167万+

被折叠的 条评论
为什么被折叠?



