配置 feign client 的超时时间, fallback 时返回空数据
spring 框架核心:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-introduction
https://github.com/OpenFeign/feign
结合 spring cloud 中使用 feign https://spring.io/projects/spring-cloud-openfeign
https://spring.io/projects/spring-cloud-circuitbreaker
Spring Cloud Circuit breaker 是什么:
Spring Cloud Circuit breaker provides an abstraction across different circuit breaker implementations. It provides a consistent API to use in your applications allowing you the developer to choose the circuit breaker implementation that best fits your needs for your app.
即 Spring Cloud 在各种熔断器的实现之上,加了一层抽象。 Spring Cloud 提供的抽象:spring-cloud-circuitbreaker-resilience4j-2.0.0.jar
熔断器的实际实现 resilience4j 提供了
feign 是一个对象,包含很多组件 (能力) 组成:
implementation "org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j"
@EnableFeignClients 注解 @Import(FeignClientsRegistrar.class)
FeignClientsRegistrar 做什么事情:
一、 支持@FeignClient 注解的 configuration 属性,可以用来指定 feign.codec.Decoder, feign.codec.Encoder, feign.Contract.
为每一个具体的 FeignClient 注册 FeignClientSpecification bean
FeignClientSpecification 其实就是 name 对应到一个明确的 configuration
class FeignClientSpecification implements NamedContextFactory.Specification
Specification with name and configuration.
翻译:
specifiy 明确地指定
specification 一种明确的指定的行为
二、注册 FeignClientFactoryBean
FeignClientFactoryBean 是一种 factorybean
factorybean
There are two kinds of beans in the Spring bean container: ordinary beans and factory beans. Spring uses the former directly, whereas latter can produce objects themselves, which are managed by the framework.
spring 直接使用前者,后者会自己生产对象,被框架管理的对象。
https://www.baeldung.com/spring-factorybean
Resilience4JAutoConfiguration、 FeignAutoConfiguration 做什么事情:
Resilience4JAutoConfiguration 来自:spring-cloud-circuitbreaker-resilience4j-2.0.0.jar
会自动创建并配置 Resilience4JCircuitBreakerFactory bean
我们可以自己实现 Customizer
Resilience4JCircuitBreakerFactory 有一个 断路器配置的 hashmap,断路器 id =》配置。定制化 Resilience4JCircuitBreakerFactory,主要是为了定制化这个 hashmap
FeignAutoConfiguration : 来自 spring-cloud-openfeign-core-3.0.1.jar,
创建出 FeignCircuitBreakerTargeter bean: 用来创建 target
Feign 是什么:Feign's purpose is to ease development against http apis that feign restfulness. In implementation, Feign is a factory for generating targeted http apis.
Target 是什么:a new instance of an HTTP API, target 其实就是定义的 @FeignClient 注解 interface 的实例。
流程:
当依赖 myFeignClient (带 FeignClient 注解的interface ) 发起一个请求时,会找到对应的 FeignClientFactoryBean。
FeignClientFactoryBean 目的创建一个 Target,走 FeignCircuitBreakerTargeter、FeignCircuitBreaker。 最终调用了 Feign.builder.build() => Feign.target()
创建了 Feign.Builder
配置 Feign.Builder(feign.client 属性也是在这里生效的, configureUsingProperties),还配置了 feign builder 的 invocationHandlerFactory 为 FeignCircuitBreakerInvocationHandler。 影响 build 过程,等同于是配置了 Feign(ReflectiveFeign)。
// build Feign
public Feign build(final FallbackFactory<?> nullableFallbackFactory) {
super.invocationHandlerFactory((target, dispatch) -> new FeignCircuitBreakerInvocationHandler(
circuitBreakerFactory, feignClientName, target, dispatch, nullableFallbackFactory));
return super.build();
}
Feign 创建 Target 时(newInstance), methodToHandler 携带了 feign.client 配置,作为 dispatch 参数传到 FeignCircuitBreakerInvocationHandler 里。并且创建了代理 Proxy.newProxyInstance。
最终 myFeignClient bean 即 Target, 请求会被代理到 FeignCircuitBreakerInvocationHandler 进行处理
feign 调用 http。最终在这里 SynchronousMethodHandler,其实是上面的 dispatch 参数。