feign 回退原因_0503-Hystrix保护应用-feign的hystrix支持

一、概述

1.1、基础【示例一】

如果Hystrix在类路径上并且feign.hystrix.enabled = true,Feign将用断路器包装所有方法。还可以返回com.netflix.hystrix.HystrixCommand。这可让您使用响应模式(调用.toObservable()或.observe()或异步使用(调用.queue())。

要以每个客户端为基础禁用Hystrix支持,请创建一个具有“prototype”范围。

在Spring Cloud Dalston发布之前,如果Hystrix在类路径上,Feign默认情况下会将所有方法封装在断路器中。 Spring Cloud Dalston改变了这种默认行为,以支持选择加入方式。

@Configurationpublic classFooConfiguration {

@Bean

@Scope("prototype")publicFeign.Builder feignBuilder() {returnFeign.builder();

}

}

1.2、Fallbacks【示例一、示例二】

Hystrix支持回退的概念:当电路断开或出现错误时执行的默认代码路径。要[email protected],[email protected]要将您的实现声明为Spring bean。

@FeignClient(name = "hello", fallback = HystrixClientFallback.class)protected interfaceHystrixClient {

@RequestMapping(method= RequestMethod.GET, value = "/hello")

Hello iFailSometimes();

}static class HystrixClientFallback implementsHystrixClient {

@OverridepublicHello iFailSometimes() {return new Hello("fallback");

}

}

1.3、回退触发器的原因fallbackFactory属性[示例三]

如果需要访问作为回退触发器的原因,[email protected]

[email protected]�工厂必须产生回退类的实例,这些实例实现由FeignClient注释的接口。

如果同时设置fallback和fallbackfactory不可以有冲突,fallback生效,fallbackfactory不能使用,fallbackFactory 是fallback的一个升级版,注释fallback设置即可

@FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)protected interfaceHystrixClient {

@RequestMapping(method= RequestMethod.GET, value = "/hello")

Hello iFailSometimes();

}

@Componentstatic class HystrixClientFallbackFactory implements FallbackFactory{

@OverridepublicHystrixClient create(Throwable cause) {return newHystrixClient() {

@OverridepublicHello iFailSometimes() {return new Hello("fallback; reason was: " +cause.getMessage());

}

};

}

}

查看FallbackFactory

public interface FallbackFactory{/*** Returns an instance of the fallback appropriate for the given cause

*

*@paramcause corresponds to {@linkcom.netflix.hystrix.AbstractCommand#getExecutionException()}

* often, but not always an instance of {@linkFeignException}.*/T create(Throwable cause);/**Returns a constant fallback after logging the cause to FINE level.*/

final class Default implements FallbackFactory{//jul to not add a dependency

finalLogger logger;finalT constant;publicDefault(T constant) {this(constant, Logger.getLogger(Default.class.getName()));

}

Default(T constant, Logger logger) {this.constant = checkNotNull(constant, "fallback");this.logger = checkNotNull(logger, "logger");

}

@OverridepublicT create(Throwable cause) {if(logger.isLoggable(Level.FINE)) {

logger.log(Level.FINE,"fallback due to: " +cause.getMessage(), cause);

}returnconstant;

}

@OverridepublicString toString() {returnconstant.toString();

}

}

}

View Code

注意事项:在Feign中实施回退以及Hystrix回退的工作方式存在限制。目前,com.netflix.hystrix.HystrixCommand和rx.Observable的方法不支持回退。

二、示例区

示例一、feign使用hystrix

1、增加引用

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

2、增加配置启用

feign.hystrix.enabled=true

3、启动类增加注解

@EnableCircuitBreaker

4、增加UserFeignClient业务接口,并配置fallback

@FeignClient(name = "microservice-provider-user", fallback = HystrixClientFallback.class)public interfaceUserFeignClient {//@GetMapping("/sample/{id}")

@RequestMapping(method = RequestMethod.GET, value = "/sample/{id}")public User findById(@PathVariable("id") Long id);

}

5、增加HystrixClientFallback类

@Componentpublic class HystrixClientFallback implementsUserFeignClient {

@OverridepublicUser findById(Long id) {

User user= newUser();

user.setId(0L);returnuser;

}

}

示例二、如何禁用单个FegionClient的Hystrix的支持

1、设置一遍如同上面

2、新增一个业务接口FeignClient2

@FeignClient(name = "xxxx", url = "http://localhost:8761/", configuration = Configuration2.class,fallback = FeignClient2Fallback.class)public interfaceFeignClient2 {

@RequestMapping(value= "/eureka/apps/{serviceName}")public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);

}

3、使用fallback

@Componentpublic class FeignClient2Fallback implementsFeignClient2 {

@OverridepublicString findServiceInfoFromEurekaByServiceName(String serviceName) {return "haha";

}

}

4、使用的配置类

@Configurationpublic classConfiguration2 {

@BeanpublicBasicAuthRequestInterceptor basicAuthRequestInterceptor() {return new BasicAuthRequestInterceptor("user", "a123");

}

@Bean

@Scope("prototype")

public Feign.Builder feignBuilder() {

returnFeign.builder();

}

}

可以看到这里主要增加了feignBuilder创建

示例三、 回退触发器的原因fallbackFactory属性

参考代码:

1、基本配置略

2、配置UserFeignClient

@FeignClient(name = "microservice-provider-user", fallbackFactory = HystrixClientFallbackFactory.class)public interfaceUserFeignClient {//@GetMapping("/sample/{id}")

@RequestMapping(method = RequestMethod.GET, value = "/sample/{id}")public User findById(@PathVariable("id") Long id);

}

注意:配置了fallbackFactory ,如果同时设置fallback和fallbackfactory不可以有冲突,只能设置一个,fallbackFactory 是fallback的一个升级版

3、fallbackFactory 的类设置HystrixClientFallbackFactory

@Componentpublic class HystrixClientFallbackFactory implements FallbackFactory{private static final Logger logger = LoggerFactory.getLogger(HystrixClientFallbackFactory.class);

@OverridepublicUserFeignClient create(Throwable arg0) {

HystrixClientFallbackFactory.logger.info("fallback reason was:{}", arg0.getMessage());return newUserFeignClientWithFactory() {

@OverridepublicUser findById(Long id) {

User user= newUser();

user.setId(-1L);returnuser;

}

};

}

}

4、UserFeignClientWithFactory设置

public interface UserFeignClientWithFactory extendsUserFeignClient {

}

原文:https://www.cnblogs.com/bjlhx/p/8909577.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值