spring cloud feign使用的几个注意点

配置优先级问题

  • FeignFactoryBean在构建feign的时候一段配置feign的代码,主要是配置重试器,超时等
protected void configureFeign(FeignContext context, Feign.Builder builder) {
  // 优先获取配置类
  // 在不显式排除的情况下,这里不会为null
  FeignClientProperties properties = this.applicationContext
      .getBean(FeignClientProperties.class);
  if (properties != null) {
    // 默认属性是true
    if (properties.isDefaultToProperties()) {
      // 第一顺序:按Configuration也就是FeignClientsConfiguration来配置
      configureUsingConfiguration(context, builder);
      // 第二顺序:按org.springframework.cloud.openfeign.FeignClientProperties#config中key为defaultConfig的value来配置
      // 不配置默认为null
      configureUsingProperties(
          properties.getConfig().get(properties.getDefaultConfig()),
          builder);
      // 第三顺序:获取contextId为key的value,不配置默认为null    
      configureUsingProperties(properties.getConfig().get(this.contextId),
          builder);
    }
    else {
      // 这种情况就是就是对FeignClientProperties 有配置了,至少是修改了默认的bool值
      // 优先按照默认key的value来配置
      configureUsingProperties(
          properties.getConfig().get(properties.getDefaultConfig()),
          builder);
      // 接着是contextId为key的value
      configureUsingProperties(properties.getConfig().get(this.contextId),
          builder);
      // 最后使用FeignClientsConfiguration
      configureUsingConfiguration(context, builder);
    }
  }
  else {
    // 如果显示排除了FeignClientProperties,那么只使用FeignClientsConfiguration
    configureUsingConfiguration(context, builder);
  }
}
复制代码
  • 上面的代码是越晚配置代表优先级越大,因为他是覆盖式的
  • 为什么单独提这个东西出来讲呢?因为生产环境遇到过因为配置优先级问题,导致一些低级错误

组件获取优先级问题

  • @FeignClient提供我们设置配置的方式
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FeignClient {
  Class<?>[] configuration() default {};
}
复制代码
  • 如果给FeignClient使用了配置,那么子组件会优先使用指定的配置
  • 如果一个FeignClient没指定使用configuration,并且当前存在全局配置,那么这个使用情况是怎么样呢?我们构造2个对比试验来看看
    • 观察方法:DEBUG观察一下Feign Decoder的实例是什么?可以Debug org.springframework.cloud.openfeign.FeignClientFactoryBean#feign这个方法
    • 对照实验【1】: 构造一个Demo例子,建立一个全局Configuration和给FeignClient也指定一份配置,看看最后会使用谁的?
// 这是我们的全局配置
@Configuration
public class MyConfiguration {
    @Bean
    public Decoder myDecoder(){
        return new feign.codec.Decoder.Default();
    }
}

// 注意指定配置不能使用@Configuration,否则也会被当作全局配置
public class BaiduFeignConfiguration {
    @Bean
    public Decoder baiduDecorder() {
        return new BaiduDecoder();
    }
    public static class BaiduDecoder implements Decoder {
        @Override
        public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
            return null;
        }
    }
}

// 修改我们的Feign
@FeignClient(name = "baidu", url = "https://www.baidu.com",
  configuration = BaiduFeignConfiguration.class)
public interface BaiduFeignClient {
    @GetMapping("/")
    String get();
}
复制代码
  • 答案是:BaiduFeignClient 使用了BaiduDecoder
  • 对照实验【2】: 构造一个Demo例子,建立一个全局Configuration,feignClient不指定,看看最后会使用谁的?
@FeignClient(name = "baidu", url = "https://www.baidu.com")
public interface BaiduFeignClient {
    @GetMapping("/")
    String get();
}
复制代码
  • 答案是:BaiduFeignClient 使用了feign.codec.Decoder.Default()而不是FeignClientsConfiguration中的OptionalDecoder
  • 简单看下为什么会有对照实验2的情况?答案其实是Springboot的条件机制
@Configuration
public class FeignClientsConfiguration {
  @Bean
  // 只有没有存在Decoder的bean实例才会使用
  @ConditionalOnMissingBean
  public Decoder feignDecoder() {
    return new OptionalDecoder(
        new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)));
  }
}
复制代码
  • 所以在getBean的时候(org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveBean)就会走BeanFactory parent = getParentBeanFactory(),这里的parent当然可以读到我们的全局配置


作者:SAM2021
链接:https://juejin.cn/post/7044811910386024485
 

如果觉得本文对你有帮助的话,麻烦点赞关注支持一下 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值