前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >扩展Spring Cloud Feign 实现自动降级

扩展Spring Cloud Feign 实现自动降级

原创
作者头像
冷冷
发布2019-02-22 16:57:51
2.3K0
发布2019-02-22 16:57:51
举报
文章被收录于专栏:冷冷冷冷冷冷

自动降级目的

在Spring Cloud 使用feign 的时候,需要明确指定fallback 策略,不然会提示错误

先来看默认的feign service 是要求怎么做的。feign service 定义一个 factory 和 fallback 的类

@FeignClient(value = ServiceNameConstants.UMPS_SERVICE, fallbackFactory = RemoteLogServiceFallbackFactory.class)
public interface RemoteLogService {}

但是我们大多数情况的feign 降级策略为了保证幂等都会很简单,输出错误日志即可。

类似如下代码,在企业中开发非常不方便

@Slf4j
@Component
public class RemoteLogServiceFallbackImpl implements RemoteLogService {
	@Setter
	private Throwable cause;


	@Override
	public R<Boolean> saveLog(SysLog sysLog, String from) {
		log.error("feign 插入日志失败", cause);
		return null;
	}
}

自动降级效果

@FeignClient(value = ServiceNameConstants.UMPS_SERVICE)
public interface RemoteLogService {}
  • Feign Service 完成同样的降级错误输出
  • FeignClient 中无需定义无用的fallbackFactory
  • FallbackFactory 也无需注册到Spring 容器中
代码变化,去掉FeignClient 指定的降级工厂
代码变化,删除降级相关的代码

核心源码

  1. 注入我们个性化后的Feign
@Configuration
@ConditionalOnClass({HystrixCommand.class, HystrixFeign.class})
protected static class HystrixFeignConfiguration {
	@Bean
	@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
	@ConditionalOnProperty("feign.hystrix.enabled")
	public Feign.Builder feignHystrixBuilder(FeignContext feignContext) {
		return PigxHystrixFeign.builder(feignContext)
				.decode404()
				.errorDecoder(new PigxFeignErrorDecoder());
	}
}
  1. PigxHystrixFeign.target 方法是根据@FeignClient 注解生成代理类的过程,注意注释
@Override
public <T> T target(Target<T> target) {
	Class<T> targetType = target.type();
	FeignClient feignClient = AnnotatedElementUtils.getMergedAnnotation(targetType, FeignClient.class);
	String factoryName = feignClient.name();
	SetterFactory setterFactoryBean = this.getOptional(factoryName, feignContext, SetterFactory.class);
	if (setterFactoryBean != null) {
		this.setterFactory(setterFactoryBean);
	}
	
	// 以下为获取降级策略代码,构建降级,这里去掉了降级非空的非空的校验
	Class<?> fallback = feignClient.fallback();
	if (fallback != void.class) {
		return targetWithFallback(factoryName, feignContext, target, this, fallback);
	}
	Class<?> fallbackFactory = feignClient.fallbackFactory();
	if (fallbackFactory != void.class) {
		return targetWithFallbackFactory(factoryName, feignContext, target, this, fallbackFactory);
	}
	return build().newInstance(target);
}
  1. 构建feign 客户端执行PigxHystrixInvocationHandler的增强
Feign build(@Nullable final FallbackFactory<?> nullableFallbackFactory) {
		super.invocationHandlerFactory((target, dispatch) ->
				new PigxHystrixInvocationHandler(target, dispatch, setterFactory, nullableFallbackFactory));
		super.contract(new HystrixDelegatingContract(contract));
		return super.build();
	}
  1. PigxHystrixInvocationHandler.getFallback() 获取降级策略
	@Override
	@Nullable
	@SuppressWarnings("unchecked")
	protected Object getFallback() {
	        // 如果 @FeignClient  没有配置降级策略,使用动态代理创建一个
			if (fallbackFactory == null) {
				fallback = PigxFeignFallbackFactory.INSTANCE.create(target.type(), getExecutionException());
			} else {
			  // 如果 @FeignClient配置降级策略,使用配置的
				fallback = fallbackFactory.create(getExecutionException());
			}
	}
  1. PigxFeignFallbackFactory.create 动态代理逻辑
	public T create(final Class<?> type, final Throwable cause) {
		return (T) FALLBACK_MAP.computeIfAbsent(type, key -> {
			Enhancer enhancer = new Enhancer();
			enhancer.setSuperclass(key);
			enhancer.setCallback(new PigxFeignFallbackMethod(type, cause));
			return enhancer.create();
		});
	}
  1. PigxFeignFallbackMethod.intercept, 默认的降级逻辑,输出降级方法信息和错误信息,并且把错误格式
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) {
	log.error("Fallback class:[{}] method:[{}] message:[{}]",
			type.getName(), method.getName(), cause.getMessage());

	if (R.class == method.getReturnType()) {
		final R result = cause instanceof PigxFeignException ?
				((PigxFeignException) cause).getResult() : R.builder()
				.code(CommonConstants.FAIL)
				.msg(cause.getMessage()).build();
		return result;
	}
	return null;
}

关注我们

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 自动降级目的
  • 自动降级效果
    • 代码变化,去掉FeignClient 指定的降级工厂
      • 代码变化,删除降级相关的代码
      • 核心源码
      • 关注我们
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档