zoukankan      html  css  js  c++  java
  • SpringCloudfeign的配置加载

         spring cloud feign我们使用 @FeignClient注解,其中有几个核心属性:

    @AliasFor("name")
    String value() default "";
    
    @Deprecated
    String serviceId() default "";
    
    /**
     * This will be used as the bean name instead of name if present, but will not be used as a service id.这个注释我理解的似乎有点问题,不知道理解的对不对
     */
    String contextId() default "";
    
    @AliasFor("value")
    String name() default "";
    
    String qualifier() default "";
    

      

    spring会用FeignClientsRegistrar这个类注册当前client,其中有一个很重要的属性就是clientName,因为clientName会影响到我们配置该client,clientName的定义看下面代码,可以看到contextId的优先级最高,当你定义了contextId后clientName取的就是contextId。


    org.springframework.cloud.openfeign.FeignClientsRegistrar.getClientName
    private String getClientName(Map<String, Object> client) {
       if (client == null) {
          return null;
       }
       String value = (String) client.get("contextId");//可以看到contextId的优先级最高>value>name>serviceId
       if (!StringUtils.hasText(value)) {
          value = (String) client.get("value");
       }
       if (!StringUtils.hasText(value)) {
          value = (String) client.get("name");
       }
       if (!StringUtils.hasText(value)) {
          value = (String) client.get("serviceId");
       }
       if (StringUtils.hasText(value)) {
          return value;
       }
    
       throw new IllegalStateException("Either 'name' or 'value' must be provided in @"
             + FeignClient.class.getSimpleName());
    }

    其中有一个很容易搞错的问题,就是client beanName(区别于clientName),首先看出qualifier的级别最高,如果没有定义qualifier但定义了contextId后实际的bean名称是contextId+"FeignClient"


    org.springframework.cloud.openfeign.FeignClientsRegistrar.registerFeignClient
    String alias = contextId + "FeignClient";//拼接了FeignClient
    AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
    
    boolean primary = (Boolean)attributes.get("primary"); // has a default, won't be null
    
    beanDefinition.setPrimary(primary);
    
    String qualifier = getQualifier(attributes);
    if (StringUtils.hasText(qualifier)) {//如果有qualifier就用qualifier
       alias = qualifier;
    }
    
    
    

    但是FeignClient注解contextId源码中的注释 This will be used as the bean name instead of name if present, but will not be used as a service id.我理解起来这个注释有点问题 

    spring cloud feign的配置都是基于clientName来识别的,例如

       feign.client.clientName.connect-timeout=100

    相关配置会被装载到FeignClientProperties,其中支持配置的属性如图

     最终通过FeignClientFactoryBean将FeignClientProperties中相应client的配置装配到feignclient中,核心方法为

    org.springframework.cloud.openfeign.FeignClientFactoryBean.configureFeign

    protected void configureFeign(FeignContext context, Feign.Builder builder) {
       FeignClientProperties properties = applicationContext.getBean(FeignClientProperties.class);
       if (properties != null) {
          if (properties.isDefaultToProperties()) {
              //三种配置依次装载
             configureUsingConfiguration(context, builder);//先配置Java代码中的配置
             configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);//配置文件中的默认配置
             configureUsingProperties(properties.getConfig().get(this.contextId), builder);//配置了clientName的配置
          } else {
             configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
             configureUsingProperties(properties.getConfig().get(this.contextId), builder);
             configureUsingConfiguration(context, builder);
          }
       } else {
          configureUsingConfiguration(context, builder);
       }
    }
    

      

     
  • 相关阅读:
    javascript 介绍
    命令提示符(命令行)
    node(1) hello world
    用Vgg16来提取特征
    VGG16学习
    注意力模型
    统计学习方法
    数字图像处理(五)
    数字图像处理(四)
    BN
  • 原文地址:https://www.cnblogs.com/minjay/p/15746223.html
Copyright © 2011-2022 走看看