扩展Spring Cloud Feign 实现自动降级

栏目: Java · 发布时间: 5年前

内容简介:在Spring Cloud 使用feign 的时候,先来看默认的feign service 是要求怎么做的。feign service 定义一个 factory 和 fallback 的类

扩展Spring Cloud Feign 实现自动降级

自动降级目的

在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 指定的降级工厂

扩展Spring Cloud Feign 实现自动降级

代码变化,删除降级相关的代码

扩展Spring Cloud Feign 实现自动降级

核心源码

  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;
}

关注我们


以上所述就是小编给大家介绍的《扩展Spring Cloud Feign 实现自动降级》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

天涯虚拟社区

天涯虚拟社区

刘华芹 / 民族出版社 / 2005-11 / 23.00元

网络空间很复杂,好多人并不完全了解或者只是了解到一些皮毛。比如说好多人对于见网友一事总是抱着浪漫或者暖昧的想法,而事实却并不总是想象的那样。作者在做虚拟社区研究甚至是在有这个想法之前并不常呆在网上,互联网对于作者来说就是查查资料、收发信年、看看新闻的工具。担是看着越来越多的人把时间花在网上,一处文化上的直觉告诉作者:有一种新的生活方式产生了。强烈的好奇心驱使着作者走到了网上,走到了天涯虚拟社区,并......一起来看看 《天涯虚拟社区》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

MD5 加密
MD5 加密

MD5 加密工具