SpringCloud–feign的配置加载

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

java;gutter:true; @AliasFor("name") String value() default "";</p> <p>@Deprecated String serviceId() default "";</p> <p>/*<em> * This will be used as the bean name instead of name if present, but will not be used as a service id.这个注释我理解的似乎有点问题,不知道理解的对不对 </em>/ String contextId() default "";</p> <p>@AliasFor("value") String name() default "";</p> <p>String qualifier() default "";</p> <pre><code> spring会用FeignClientsRegistrar这个类注册当前client,其中有一个很重要的属性就是clientName,因为clientName会影响到我们配置该client,clientName的定义看下面代码,可以看到contextId的优先级最高,当你定义了contextId后clientName取的就是contextId。 </code></pre> <p>org.springframework.cloud.openfeign.FeignClientsRegistrar.getClientName</p> <pre><code> ;gutter:true;
private String getClientName(Map 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

java;gutter:true; String alias = contextId + "FeignClient";//拼接了FeignClient AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();</p> <p>boolean primary = (Boolean)attributes.get("primary"); // has a default, won't be null</p> <p>beanDefinition.setPrimary(primary);</p> <p>String qualifier = getQualifier(attributes); if (StringUtils.hasText(qualifier)) {//如果有qualifier就用qualifier alias = qualifier; }</p> <pre><code> 但是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,其中支持配置的属性如图 ![SpringCloud--feign的配置加载](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230605/866987-20211229185849233-673640189.png) 最终通过FeignClientFactoryBean将FeignClientProperties中相应client的配置装配到feignclient中,核心方法为 _org.springframework.cloud.openfeign._ _FeignClientFactoryBean.configureFeign_ ;gutter:true;
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);
}
}

Original: https://www.cnblogs.com/minjay/p/15746223.html
Author: minjay26
Title: SpringCloud–feign的配置加载

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/579836/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 面试突击67:说一下 TCP/IP 协议?以及每层的作用?

    TCP/IP 包含四层模型,从上层往下层分别是:应用层、传输层、网络层、数据链路层。有些资料也会说 TCP/IP 是五层模型,所谓的五层模型指的是在数据链路层下面还有一个物理层,而…

    Java 2023年5月29日
    067
  • 深入理解Apollo核心机制之灰度发布——创建灰度

    概述 ApolloPortal创建灰度后都做了什么呢?Apollo是如何维护主版本与灰度版本关系的呢? 其实创建灰度非常简单,可以看到下图中”Cluster&#8221…

    Java 2023年6月6日
    064
  • FastAPI启用HTTPS

    前提: 你需要购买一个域名, 假如是国内的法服务器的话, 需要备案, 否则无法解析当你买了域名后, 一般可以免费生成证书 下载证书 由于我是在腾讯云购买的域名, 所以在腾讯云中下载…

    Java 2023年6月7日
    070
  • SpringBoot异步使用@Async原理及线程池配置

    前言 在实际项目开发中很多业务场景需要使用异步去完成,比如消息通知,日志记录,等非常常用的都可以通过异步去执行,提高效率,那么在 Spring框架中应该如何去使用异步呢 使用步骤 …

    Java 2023年6月13日
    076
  • Sharding-jdbc 5.1.2案例

    简介 sharding-jdbc案例,版本5.1.2 springboot + mybatis-plus + sharding-jdbc 项目地址:sharding-jdbc-ex…

    Java 2023年6月16日
    074
  • Fizz企业级微服务API网关进阶系列教程-服务编排处理列表数据(中)-数据提取与数据关联

    ​ 概述 服务编排是Fizz网关提供的一个强大的功能,能够基于现有的业务微服务通过在线配置的方式快速的生成一个聚合接口,减少中间层胶水代码以及降低编码投入。在服务编排中支持使用函数…

    Java 2023年6月9日
    065
  • 【微服务】- 服务调用-OpenFeign

    服务调用 – OpenFeign 😄生命不息,写作不止🔥 继续踏上学习之路,学之分享笔记👊 总有一天我也能像各位大佬一样🏆 一个有梦有戏的人 @怒放吧德德🌝分享学习心得…

    Java 2023年6月16日
    067
  • 【转】【OPenGL】理解OpenGL拾取模式(OpenGL Picking)

    在用OpenGL进行图形编程的时候,通常要用鼠标进行交互操作,比如用鼠标点选择画面中的物体,我们称之为拾取(Picking),在网上看了很多OpenGL拾取的文章,但大多是只是介绍…

    Java 2023年5月29日
    095
  • IO(字符流的使用操作)

    OutputStreamWriter(使用)—写数据–(一次写入一个字符) OutputStreamWriter(OutputStream out):根据默…

    Java 2023年6月5日
    071
  • VMware Workstation 15Pro 的安装与使用

    1,在VMware Workstation 15 Pro官网下载 2鼠标右键 以管理员身份运行 3,点击下一步 4,勾选[我接受许可协议中的条款],然后点击[下一步] 5点击[更改…

    Java 2023年6月7日
    079
  • 使用RabbitMQ实现延迟任务

    本文转自:https://www.cnblogs.com/haoxinyue/p/6613706.html 场景一:物联网系统经常会遇到向终端下发命令,如果命令一段时间没有应答,就…

    Java 2023年5月30日
    087
  • Redis的五种基本数据类型

    Redis的五种基本数据类型 1、概述 Redis是一个由C语言开发的基于key-value形式的非关系型数据库 key-value:键值对【键:String,值:五种数据类型】 …

    Java 2023年6月8日
    073
  • golang实现文件上传功能

    前端页面form表单 注意:实现文件上传的时候,form表单必须有enctype=”multipart/form-data”属性; 可以自己设置上传文件的限…

    Java 2023年6月13日
    063
  • Java Map 遍历史上最全

    Map 遍历: Map map = new HashMap(); map.put(1, "a"); map.put(2, "b"); map…

    Java 2023年5月29日
    076
  • Java调用C++动态链接库——Jni

    最近项目需要,将C++的算法工程编译成动态链接库,交给 Java后台当作函数库调用。就去了解了下Jni。使用起来还是比较方便的。 首先编写Java的调用类。例如: public c…

    Java 2023年6月16日
    089
  • Java 单例模式 饿汉式与懒汉式

    posted @2022-03-27 11:34 紫薇哥哥 阅读(5 ) 评论() 编辑 Original: https://www.cnblogs.com/ziweigege/p…

    Java 2023年6月5日
    082
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球