feign 重试机制 在异常里面决定是否重试 重试次数
怎么实现默认bean和用户可拓展bean呢?
思路:加载顺序 如果用户定义了 然后在加载jar包的默认bean
条件加载 类似FeignClientsConfiguration feign默认的配置类
@Bean
@ConditionalOnMissingBean//用户未自定义 则IOC加载这个默认bean
public Contract feignContract(ConversionService feignConversionService) {
return new SpringMvcContract(this.parameterProcessors, feignConversionService);
}
@Configuration
@ConditionalOnClass({HystrixCommand.class, HystrixFeign.class})
protected static class HystrixFeignConfiguration {
protected HystrixFeignConfiguration() {
}
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
@ConditionalOnProperty(
name = {"feign.hystrix.enabled"}
)//定义这个属性的情况下加载这个bean
public Builder feignHystrixBuilder() {
return HystrixFeign.builder();
}
}
参考:https://blog.youkuaiyun.com/forezp/article/details/73480304
//重试
while(true) {
try {
return this.executeAndDecode(template);
} catch (RetryableException var5) {
retryer.continueOrPropagate(var5);
if (this.logLevel != Level.NONE) {
this.logger.logRetry(this.metadata.configKey(), this.logLevel);
}
}
}
public void continueOrPropagate(RetryableException e) {
//如果重试次数到了 就直接抛出去
if (this.attempt++ >= this.maxAttempts) {
throw e;
} else {
//..休眠了继续
try {
Thread.sleep(interval);
} catch (InterruptedException var5) {
Thread.currentThread().interrupt();
}
this.sleptForMillis += interval;
}
}
- 首先通过@EnableFeignCleints注解开启FeignCleint
- 根据Feign的规则实现接口,并加@FeignCleint注解
- 程序启动后,会进行包扫描,扫描所有的@ FeignCleint的注解的类,并将这些信息注入到ioc容器中。
- 当接口的方法被调用,通过jdk的代理,来生成具体的RequesTemplate
- RequesTemplate在生成Request
- Request交给Client去处理,其中Client可以是HttpUrlConnection、HttpClient也可以是Okhttp
- 最后Client被封装到LoadBalanceClient类,这个类结合类Ribbon做到了负载均衡。