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

  

 

SpringCloud--feign的配置加载的更多相关文章

  1. SpringBoot SpringCloud 热部署 热加载 热调试

    疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] Crazy-Sp ...

  2. 关于flume配置加载(二)

    为什么翻flume的代码,一方面是确实遇到了问题,另一方面是想翻一下flume的源码,看看有什么收获,现在收获还谈不上,因为要继续总结.不够已经够解决问题了,而且确实有好的代码,后续会继续慢慢分享,这 ...

  3. 关于flume配置加载

    最近项目在用到flume,因此翻了下flume的代码, 启动脚本: nohup bin/flume-ng agent -n tsdbflume -c conf -f conf/配置文件.conf -D ...

  4. Java实现配置加载机制

    前言 现如今几乎大多数Java应用,例如我们耳熟能详的tomcat, struts2, netty…等等数都数不过来的软件,要满足通用性,都会提供配置文件供使用者定制功能. 甚至有一些例如Netty这 ...

  5. 异常处理之IIS配置加载出错

    问题详情:  一台部署在海外服务器,在管理IIS过程中,出现问题 There was an error when trying to connect. Do you want > to rety ...

  6. 深入理解 Laravel 中 config 配置加载原理

    Laravel的配置加载其实就是加载config目录下所有文件配置.如何过使用php artisan config:cache则会把加载的配置合并到一个配置文件中,下次请求就不会再去加载config目 ...

  7. Springboot学习01- 配置文件加载优先顺序和本地配置加载

    Springboot学习01-配置文件加载优先顺序和本地配置加载 1-项目内部配置文件加载优先顺序 spring boot 启动会扫描以下位置的application.properties或者appl ...

  8. Unity3d通用工具类之数据配置加载类-ini配置文件加载

    Unity3d通用工具类之数据配置加载类-ini配置文件加载 上次我们讲过xml文件的加载配置管理,今天我们换个配置文件,也是比较常见的配置文件.ini格式的数据. 按照国际管理先贴一张啥是.ini文 ...

  9. Unity3d通用工具类之数据配置加载类

    今天,我们来讲讲游戏中的数据配置加载. 什么是游戏数据加载呢?一般来说游戏中会有场景地图. 按照国际惯例,先贴一张游戏场景的地图: 在这张地图上,我们可以看到有很多正六边形,正六边形上有树木.岩石等. ...

  10. 3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)

    1.外部配置加载顺序 SpringBoot也可以从以下位置加载配置: 优先级从高到低 高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置  1.命令行参数 所有的配置都可以在命令行上进行指定 ...

随机推荐

  1. powershell操作excel

    https://blog.csdn.net/u010288731/article/details/83120205 如何创建一个Excel 应用程序对象? $xl = new-object -como ...

  2. [ vue ] xxxProject项目杂记

    2020.4.9 加入eCharts 2020.4.8 完成article的显示,其间碰到全局路由守卫写的有错误,导致跳转报错.已修复. 加入keep-alive功能,缓存视图数据 疑问:如果在全局组 ...

  3. Node.js 模块之【passport】

    什么是passport passport是Nodejs的一个中间键,用于用户名和密码的验证登陆.在项目中我用它来验证后台用户名和密码,但passport更多用在第三方登录,功能强大. 安装与配置 本项 ...

  4. ​第3届云原生技术实践峰会(CNBPS 2020)重磅开启,“原”力蓄势待发!

    CNBPS 2020将在11月19-21日全新启动!作为国内最有影响力的云原生盛会之一,云原生技术实践峰会(CNBPS)至今已举办三届. 在2019年的CNBPS上,灵雀云CTO陈恺喊出"云 ...

  5. sql创建表格时出现乱码

    1.新建数据库时,第一次只填写了数据库名称保存数据库,如下图: 2.创建一个Student表格,代码如下,其中有数据有中文,创建完后查看表格数据,发现中文为乱码 create table Studen ...

  6. Android官方文档翻译 八 2.1Setting Up the Action Bar

    Setting Up the Action Bar 建立Action Bar This lesson teaches you to 这节课教给你 Support Android 3.0 and Abo ...

  7. python极简教程08:对象的方法

    测试奇谭,BUG不见. 讲解之前,我先说说我的教程和网上其他教程的区别: 1 我分享的是我在工作中高频使用的场景,是精华内容: 2 我分享的是学习方法,亦或说,是指明你该学哪些.该重点掌握哪些内容: ...

  8. Exception in thread “main“ java.net.ConnectException: Call From

    问题描述:#报错语句:FileSystem fs = FileSystem.get(new URI("hdfs://hadoop000:8020"),new Configurati ...

  9. How to mount Windows network disk in WSL

    Backgroud Mount samba directly in wsl like linux is difficult Password for root@//filesystem.domain/ ...

  10. python+fastdfs+nginx实现打包下载功能

    环境介绍:生产服务器开发人员需要给client下发数据,主要是图片及视频:图片服务器用fastdfs,下载由nginx 来提供: java 程序来调用此脚本,传递参数来决定打包文件内容: #!/usr ...