Feign的@FeignClient详解--呕心沥血之作(2)


前置

1、这里这是贴出了部分示列代码, 具体代码已经上传到码云: 代码地址
2、本demo采用了feign的继承特性
3、版本: Spring Cloud: Hoxton.SR3、spring-cloud-openfeign: 2.2.2.RELEASE, 服务发现: nacos

@FeignClient:

  • 官方文档: https://docs.spring.io/spring-cloud-openfeign/docs/2.2.x-SNAPSHOT/reference/html/index.html#spring-cloud-feign

value:

  • 服务提供者的名称
  • 支持占位符 ${project.feign-prefix} 形式进行指定, 从配置文件中读取
  • 如果没有配置url, 将作为服务发现使用 http://value/

serviceId:

  • 现在不推荐使用该属性,而推荐使用name属性
  • 和value属性一样
  • 官网摘自:
    The serviceId attribute is now deprecated in favor of the name attribute.

contextId:

  • 用作bean名,但不会作为服务id
  • 使用情况: 一个微服务创建多个FeignClient, 因为服务名称相同, 但是每个client具有不同的自定义配置, 则必须使用contextId属性, 以避免配置Bean名称相同发生冲突.
    列: 就比如该代码, getFeignClient调用的是provider, HeaderFeignClient调用的也是provider, 但是他们两个的配置是不一样的, 这时就需要指定该值来进行区分
    如果不进行区分, 因为Bean名称相同, 会导致冲突, 就该项目而言, HeaderFeignClient配置了契约, 对应的消费者都要使用契约模式
  • 官网摘自:
    1、Using contextId attribute of the @FeignClient annotation in addition to changing the name of the ApplicationContext ensemble,
    it will override the alias of the client name and it will be used as part of the name of the configuration bean created for that client.
    2、If we want to create multiple feign clients with the same name or url so that they would point to the same server
    but each with a different custom configuration then we have to use contextId attribute of the @FeignClient in order to avoid name collision of these configuration beans
  • 可用于处理异常:
    1、A bean with that name has already been defined and overriding is disabled.
    2、Method GetFeignClient#getParameters(String,String) not annotated with HTTP method type (ex. GET, POST)
    第一种异常也可使用: spring.main.allow-bean-definition-overriding=true解决, 但是不推荐, 在某些情况下解决了第一种会在报第二种异常
    第二种就是使用contextId
  • 配置了该值, 进行细粒度配置, 需要已该值为准, 就不是具体的服务
    列: getProvider.loggerLevel=full 而不是 provider.loggerLevel=full

name:

  • 同value, 一般我们都是指定name属性

qualifier:

  • 指定别名
  • 当必须设置primary=false时 && 配置容错处理(fallback | fallbackFactory), 这时启动的时候会报错, 因为spring不知道要出入那个bean.
    我们可以通过指定qualifier=noAddressAlias
    注入方式:
    @Autowired
    @Qualifier(“noAddressAlias”)

url:

  • url指定服务的地址
    列: http://127.0.0.1:8080 | http://www.baidu.com
  • 不经过Ribbon的服务选择
  • 支持占位符 ${project.feign-url} 形式进行指定, 从配置文件中读取

decode404:

  • 调用服务提供者发生404, decode404 == true ? 执行decoder解码 : 抛出异常FeignException
  • {“timestamp”:“2021-04-24T08:03:49.128+0000”,“status”:404,“error”:“Not Found”,“message”:“No message available”,“path”:“/getNoUrl”}

configuration:

  • 为FeignClient指定配置类. 包含feign.codec.Decoder、link feign.codec.Encoder、feign.Contract
  • 如果同时创建配置类和配置属性, 配置属性的优先级高, 将覆盖bean配置属性. 更改优先级: feign.client.default-to-properties=false代码配置优先级将会高于属性配置
    官网摘自:
    If we create both @Configuration bean and configuration properties, configuration properties will win. It will override @Configuration values.
    But if you want to change the priority to @Configuration, you can change feign.client.default-to-properties to false.

fallback:

  • 用于指定FeignClient回退类(容错处理类), 回退类必须实现@FeignClient标记的接口,需配置为一个有效的spring bean
    官网摘自:
    Spring Cloud CircuitBreaker supports the notion of a fallback: a default code path that is executed when the circuit is open or there is an error.
    To enable fallbacks for a given @FeignClient set the fallback attribute to the class name that implements the fallback. You also need to declare your implementation as a Spring bean.
  • 当调用远程接口失败或者超时, 会调用对应的回退类的逻辑
  • 使用hystrix做降级, 需要配置: feign.hystrix.enabled=true(spring-cloud-starter-openfeign中集成了feign-hystrix)
    官网摘自:
    If Hystrix is on the classpath and feign.hystrix.enabled=true, Feign will wrap all methods with a circuit breaker
  • 使用circuitbreaker做降级, 需要配置: feign.circuitbreaker.enabled=true
    官网摘自:
    If Spring Cloud CircuitBreaker is on the classpath and feign.circuitbreaker.enabled=true, Feign will wrap all methods with a circuit breaker.
    问题是: 我目前该版本并不支持(spring-cloud-openfeign-core.META-INF.spring-configuration-metadata.json中并未找到该配置选项).
    简单的看了一下, spring-cloud得SR11才支持的样子, 或者openfeign得2.2.8.RELEASE

fallbackFactory:

  • 工厂类, 用于生成fallback类实例, 通过该属性可以实现每个接口通用的容错逻辑, 减少重复代码, 需配置为一个有效的spring bean
  • 当调用远程接口失败或者超时, 可以知道对应的错误信息
    官网摘自:
    If one needs access to the cause that made the fallback trigger, one can use the fallbackFactory attribute inside @FeignClient.
  • 基于fallback进行操作

path:

  • 该FeignClient所有方法的路径前缀
  • http://name/path/**

primary:

  • 是否将伪代理标记为主要bean, 默认值为true
    官网摘自:
    When using Feign with Hystrix fallbacks, there are multiple beans in the ApplicationContext of the same type.
    This will cause @Autowired to not work because there isn’t exactly one bean, or one marked as primary. To work around this,
    Spring Cloud OpenFeign marks all Feign instances as @Primary, so Spring Framework will know which bean to inject.
    In some cases, this may not be desirable. To turn off this behavior set the primary attribute of @FeignClient to false

注意点

1、以上都是我基于该版本对应的官网案例, 以及源码的说明, 进行的总结, 个人看法居多, 仅供大家参考

2、大家在使用的时候一定要注意自己的版本, 以及版本对应的官方文档, 这个很重要, 很重要,很重要

3、openfeign对应的其余文章:

代码地址以及对应的postman文件
Feign的使用示列–呕心沥血之作(1)
Feign的@FeignClient详解–呕心沥血之作(2)
Feign的默认契约注解详解–呕心沥血之作(3)
Feign异常–Request method ‘POST‘ not supported
Feign异常–A bean with that name | HTTP method type (ex. GET, POST)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值