说明,该源码部分只是个人总结,随手记录,不保证正确性;
该源码关注的不是底层Feign是如何完成远程调用的具体细节,而关注在Feign在完成远程调用之前的准备工作,他的一些配置是如何生效的;看完之后对Spring的ImportBeanDefinitionRegistrar接口比之前的理解更加深了,而且想玩自定义注解提供扩展功能的,熟悉了Feign的几个流程之后还是能够提供很大的指导意见的;
1. Feign
特别说明一下,是在使用了Ribbon的基础上加入了Feign的研读,不确定Ribbon是否会对Feign有影响
1.1 配置类:ApiConfiguration.java
@Configuration
@EnableAspectJAutoProxy
@EnableFeignClients(basePackages = "com.sinotrans.hd.microservice.api.feign")
public class ApiConfiguration {
}
重点来看一下@EnableFeignClients做了哪些事情,除了该注解本身提供的属性配置外,可以看到还导入了一个配置类FeignClientsRegistrar
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(FeignClientsRegistrar.class)
public @interface EnableFeignClients {
}
1.2 FeignClientsRegistrar
现在来看一下FeignClientsRegistrar做了什么事情,该类实现了Spring的众多接口,ImportBeanDefinitionRegistrar接口,简单点说该接口提供了可以给容器动态注入Bean的功能,ResourceLoaderAware可以获得容器资源依赖,BeanClassLoaderAware提供Bean的回调功能,EnvironmentAware获得当前应用的环境变量信息
class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware {
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) {
registerDefaultConfiguration(metadata, registry);
registerFeignClients(metadata, registry);
}
}
先看一下第一个方法registerDefaultConfiguration(),代码如下,
private void registerDefaultConfiguration(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) {
Map<String, Object> defaultAttrs = metadata
.getAnnotationAttributes(EnableFeignClients.class.getName(), true);
if (defaultAttrs != null && defaultAttrs.containsKey("defaultConfiguration")) {
String name;
if (metadata.hasEnclosingClass()) {
name = "default." + metadata.getEnclosingClassName();
}
else {
name = "default." + metadata.getClassName();
}
registerClientConfiguration(registry, name,
defaultAttrs.get("defaultConfiguration"));
}
}
defaultAttrs,先获得当前配置类的注解@EnableFeignClients类的全部属性,目前能够获取到在前面配置的属性basePackages = "com.sinotrans.hd.microservice.api.feign",再往下判断属性是否为空,是否包含defaultConfiguration,程序往下走,目前属性不为空且包含defaultConfiguration,hasEnclosingClass()判断当前注解类是否是内部类,如果是内部类,则使用default. + 顶级类名,否则使用default. + 自己的类名,当前name=default.com.sinotrans.hd.microservice.api.config.ApiConfiguration
registerClientConfiguration()方法,内部代码如下
private void registerClientConfiguration(BeanDefinitionRegistry registry, Object name, Object configuration) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder
.genericBeanDefinition(FeignClientSpecification.class);
builder.addConstructorArgValue(name);
builder.addConstructorArgValue(configuration);
registry.registerBeanDefinition(
name + "." + FeignClientSpecification.class.getSimpleName(),
builder.getBeanDefinition());
}
第一行首先预定义一个org.springframework.cloud.netflix.feign.FeignClientSpecification类型的Bean信息,通过构造方法设置FeignClientSpecification的name和configuration类,结合上面name属性的设置,定义的这个Bean的名称为default.com.sinotrans.hd.microservice.api.config.ApiConfiguration.FeignClientSpecification,调用FeignClientSpecification的构造方法来初始化这个类
FeignClientSpecification.java
class FeignClientSpecification implements NamedContextFactory.Specification {
public FeignClientSpecification(String name, Class<?>[] configuration) {
this.name = name;
this.configuration = configuration;
}
}
现在来看一下registerFeignClients(metadata, registry);方法源码如下:
public void registerFeignClients(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) {
ClassPathScanningCandidateComponentProvider scanner = getScanner();
scanner.setResourceLoader(this.resourceLoader);
Set<String> basePackages;
Map<String, Object> attrs = metadata
.getAnnotationAttributes(EnableFeignClients.class.getName());
AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(
FeignClient.class);
final Class<?>[] clients = attrs == null ? null
: (Class<?>[]) attrs.get("clients");
if (clients == null || clients.length == 0) {
scanner.addIncludeFilter(annotationTypeFilter);
basePackages = getBasePackages(metadata);
}
else {
final Set<String> clientClasses = new HashSet<>();
basePackages = new HashSet<>();
for (Class<?> clazz : clients) {
basePackages.add(ClassUtils.getPackageName(clazz));
clientClasses.add(clazz.getCanonicalName());
}
AbstractClassTestingTypeFilter filter = new AbstractClassTestingTypeFilter() {
@Override
protected boolean match(ClassMetadata metadata) {
String cleaned = metadata.getClassName().replaceAll("\\$", ".");
return clientClasses.contains(cleaned);
}
};
scanner.addIncludeFilter(
new AllTypeFilter(Arrays.asList(filter, annotationTypeFilter)));
}
for (String basePackage : basePackages) {
Set<BeanDefinition> candidateComponents = scanner
.findCandidateComponents(basePackage);
for (BeanDefinition candidateComponent : candidateComponents) {
if (candidateComponent instanceof AnnotatedBeanDefinition) {
// verify annotated class is an interface
AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) candidateComponent;
AnnotationMetadata annotationMetadata = beanDefinition.getMetadata();
Assert.isTrue(annotationMetadata.isInterface(),
"@FeignClient can only be specified on an interface");
Map<String, Object> attributes = annotationMetadata
.getAnnotationAttributes(
FeignClient.class.getCanonicalName());
String name = getClientName(attributes);
registerClientConfiguration(registry, name,
attributes.get("configuration"));
registerFeignClient(registry, annotationMetadata, attributes);
}
}
}
}
① 这个方法的代码有点长,首先获得包扫描类,获得系统资源加载类,然后获得配置类的@EnableFeignClients注解的所有属性,定义一个匹配FeignClient的过滤器,clients属性,则是判断当前@EnableFeignClients是否有配置过clients属性,该属性的作用是明确指定标注了@FeignClient注解的接口类,如果配置了这个属性,则类路径扫描会被禁用,则basePackages扫描包路径的值会将clients属性的接口类所在的包加入扫描路径,否则使用类路径扫描。当前使用类路径扫描;clients的值一旦为空或长度为0,那么则包扫描规则加入一个includeFilters规则为只扫描带@FeignClient注解的类,packageSearchPath=classpath*:com/sinotrans/hd/microservice/api/feign*.class
② findCandidateComponents()方法循环包扫描路径,查找指定包路径下符合条件的class,然后作为BeanDefinition集合返回,代码如下
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
Set<BeanDefinition> candidates = new LinkedHashSet<BeanDefinition>();
try {
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
resolveBasePackage(basePackage) + '/' + this.resourcePattern;
Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);
boolean traceEnabled = logger.isTraceEnabled();
boolean debugEnabled = logger.isDebugEnabled();
for (Resource resource : resources) {
if (traceEnabled) {
logger.trace("Scanning " + resource);
}
if (resource.isReadable()) {
try {
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);
if (isCandidateComponent(metadataReader)) {
ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
sbd.setResource(resource);
sbd.setSource(resource);
if (isCandidateComponent(sbd)) {
if (debugEnabled) {
logger.debug("Identified candidate component class: " + resource);
}
candidates.add(sbd);
}
else {
if (debugEnabled) {
logger.debug("Ignored because not a concrete top-level class: " + resource);
}
}
}
else {
if (traceEnabled) {
logger.trace("Ignored because not matching any filter: " + resource);
}
}
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException(
"Failed to read candidate component class: " + resource, ex);
}
}
else {
if (traceEnabled) {
logger.trace("Ignored because not readable: " + resource);
}
}
}
}
catch (IOException ex) {
throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
}
return candidates;
}
③ 循环返回的candidateComponents,而且类型必须为AnnotatedBeanDefinition并且必须是接口,然后获得该接口上的@FeignClient注解的属性,包含服务名,和请求上下文(包含上下文和控制层的RequestMapping),内容如下
④ 通过方法getClientName()获取服务名,可以看到服务名的规则是value > name > serviceId依次去取,直到取不到抛出异常
private String getClientName(Map<String, Object> client) {
if (client == null) {
return null;
}
String 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());
}
⑤ registerClientConfiguration()方法将服务名注册成FeignClientSpecification类型的Bean放入预定义Bean容器,名称为服务名"." + FeignClientSpecification.class.getSimpleName(),同时也将服务名和配置类分别通过构造方法赋值给FeignClientSpecification的name和configuration属性,每个服务所需要引用的接口类有多个,所以这里可能会重复注册registerClientConfiguration,因为这里只是定义信息,所以应该是hi后来的会覆盖之前的吧。所以最终注入的应当是服务名去重后的数量,注入的时候也应当使用集合来接收注入,这个在后面会碰到;所以到了这里加上之前定义的默认的配置类生成的FeignClientSpecification,目前一共会有()服务数 + 配置类默认生成的)个FeignClientSpecification
private void registerClientConfiguration(BeanDefinitionRegistry registry, Object name,
Object configuration) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder
.genericBeanDefinition(FeignClientSpecification.class);
builder.addConstructorArgValue(name);
builder.addConstructorArgValue(configuration);
registry.registerBeanDefinition(
name + "." + FeignClientSpecification.class.getSimpleName(),
builder.getBeanDefinition());
}
⑥ registerFeignClient()方法,首先通过BeanDefinitionBuilder定义FeignClientFactoryBean类型的Bean,然后将@FeignClient里的所有属性都加入到BeanDefinitionBuilder的propertyValues里,通过这种方式给FeignClientFactoryBean的属性赋值,定义注入方式为AbstractBeanDefinition.AUTOWIRE_BY_TYPE,通过BeanDefinitionHolder对象将Bean的alias定义为服务名+“FeignClient”,beanName=类的全路径,注册beanName的alias,这一块存疑,每个接口不同,但服务相同,alias会相同,不知道这个alias的作用是什么?
FeignClientFactoryBean.java属性如下
class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean,
ApplicationContextAware {
private Class<?> type;
private String name;
private String url;
private String path;
private boolean decode404;
private ApplicationContext applicationContext;
private Class<?> fallback = void.class;
private Class<?> fallbackFactory = void.class;
}
private void registerFeignClient(BeanDefinitionRegistry registry,
AnnotationMetadata annotationMetadata, Map<String, Object> attributes) {
String className = annotationMetadata.getClassName();
BeanDefinitionBuilder definition = BeanDefinitionBuilder
.genericBeanDefinition(FeignClientFactoryBean.class);
validate(attributes);
definition.addPropertyValue("url", getUrl(attributes));
definition.addPropertyValue("path", getPath(attributes));
String name = getName(attributes);
definition.addPropertyValue("name", name);
definition.addPropertyValue("type", className);
definition.addPropertyValue("decode404", attributes.get("decode404"));
definition.addPropertyValue("fallback", attributes.get("fallback"));
definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory"));
definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
String alias = name + "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)) {
alias = qualifier;
}
BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className,
new String[] { alias });
BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
}
1.3 FeignAutoConfiguration
先看一下该类的定义,@ConditionalOnClass(Feign.class)一旦类路径下引入了Feign的包,则该配置类会自动生效,然后导入配置属性类信息
@Configuration
@ConditionalOnClass(Feign.class)
@EnableConfigurationProperties({FeignClientProperties.class, FeignHttpClientProperties.class})
public class FeignAutoConfiguration {
@Autowired(required = false)
private List<FeignClientSpecification> configurations = new ArrayList<>();
@Bean
public FeignContext feignContext() {
FeignContext context = new FeignContext();
context.setConfigurations(this.configurations);
return context;
}
}
① 注入一个名为feignContext类型为FeignContext的bean,使用默认的配置类FeignClientsConfiguration通过父类NamedContextFactory来构建,,将所有feign相关的配置设置进去,包含了Feign的上下文信息,FeignClientsConfiguration通过实现ApplicationContextAware来注入ApplicationContext, 并将ApplicationContext作为FeignContext的父容器,关于FeignClientsConfiguration在后面章节讲述
FeignContext.java
public FeignContext() {
super(FeignClientsConfiguration.class, "feign", "feign.client.name");
}
NamedContextFactory.java
public abstract class NamedContextFactory<C extends NamedContextFactory.Specification>
implements DisposableBean, ApplicationContextAware {
public interface Specification {
String getName();
Class<?>[] getConfiguration();
}
private Map<String, AnnotationConfigApplicationContext> contexts = new ConcurrentHashMap<>();
private Map<String, C> configurations = new ConcurrentHashMap<>();
private ApplicationContext parent;
private Class<?> defaultConfigType;
private final String propertySourceName;
private final String propertyName;
public NamedContextFactory(Class<?> defaultConfigType, String propertySourceName,
String propertyName) {
this.defaultConfigType = defaultConfigType;
this.propertySourceName = propertySourceName;
this.propertyName = propertyName;
}
public void setConfigurations(List<C> configurations) {
for (C client : configurations) {
this.configurations.put(client.getName(), client);
}
}
}
② FeignContext创建完成之后,下一步context.setConfigurations(this.configurations); 通过代码可以看到this.configurations指向的是本类的一个属性,通过@Autowired注入,然后我们看到注入的这个类型,FeignClientSpecification在前面我们看到了,这个是根据@FeignContext上的服务名来进行创建的类型,详见org.springframework.cloud.netflix.feign.FeignClientsRegistrar#registerClientConfiguration方法,所以在之前我们注入的FeignClientSpecification,也解决了之前的疑惑,既然会注入多个同类型的Bean,所以这里只能通过集合来接收注入,根据NamedContextFactory的源码可以看到它的configurations属性是一个ConcurrentHashMap,ConcurrentHashMap的key是FeignClientSpecification的name属性,关于name属性的值的规则前面也已经看到了, ConcurrentHashMap的value就是每个FeignClientSpecification对象本身
@Autowired(required = false)
private List<FeignClientSpecification> configurations = new ArrayList<>();
1.4 FeignClientFactoryBean的定义
该类部分源码如下:
实现了FactoryBean接口来完成Bean的注入,最终注入的对象通过getObject()方法返回,实现了
InitializingBean接口通过afterPropertiesSet()方法来检查name属性的赋值,实现了ApplicationContextAware接口来获得ApplicationContext容器,其中在前面也已经看到该类的属性赋值过程是如何实现的,这里不再细述。
class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean,
ApplicationContextAware {
private Class<?> type;
private String name;
private String url;
private String path;
private boolean decode404;
private ApplicationContext applicationContext;
private Class<?> fallback = void.class;
private Class<?> fallbackFactory = void.class;
@Override
public void afterPropertiesSet() throws Exception {
Assert.hasText(this.name, "Name must be set");
}
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
this.applicationContext = context;
}
@Override
public Object getObject() throws Exception {
FeignContext context = applicationContext.getBean(FeignContext.class);
Feign.Builder builder = feign(context);
}
}
① 现在重点来看一下getObject()方法,首先从ApplicationContext容器中获得FeignContext对象,该对象在上一步已经看到如何注入的,下一步调用feign()方法,该方法代码如下
class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean,
ApplicationContextAware {
protected Feign.Builder feign(FeignContext context) {
FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);
Logger logger = loggerFactory.create(this.type);
// @formatter:off
Feign.Builder builder = get(context, Feign.Builder.class)
// required values
.logger(logger)
.encoder(get(context, Encoder.class))
.decoder(get(context, Decoder.class))
.contract(get(context, Contract.class));
// @formatter:on
configureFeign(context, builder);
// 省略其它代码
}
protected <T> T get(FeignContext context, Class<T> type) {
T instance = context.getInstance(this.name, type);
if (instance == null) {
throw new IllegalStateException("No bean found of type " + type + " for "
+ this.name);
}
return instance;
}
protected void configureFeign(FeignContext context, Feign.Builder builder) {
FeignClientProperties properties = applicationContext.getBean(FeignClientProperties.class);
// 省略其它代码
}
}
首先第一步FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);点开get()方法,最终执行org.springframework.cloud.context.named.NamedContextFactory#createContext,传入的name即FeignClientFactoryBean的name属性,也就是服务名,创建一个空的基于注解的容器类,先判断configuration属性的Map里是否包含当前name,之前已经看到configuration的属性来源就是之前注入的FeignClientSpecification的name属性也就是服务名,所以传入的服务名是包含在这里的,判断获得当前name对应的FeignClientSpecification注册到新创建的容器类中,将NamedContextFactory的defaultConfigType属性注入到容器中类型为PropertyPlaceholderAutoConfiguration,当前defaultConfigType具体实现类是通过FeignContext的构造方法调用super也就是NamedContextFactory传参复制为FeignClientSpecification对象,propertySourceName属性添加到当前新创建的服务容器的MutablePropertySources中,并且规定读取的name是当前propertySourceName,的就是说每个服务名所创建的子容器是不同的,如果不特殊指定父容器,则他们的父容器是相同的,都是ApplicationContext,关于FeignClientSpecification在下一节详述
NamedContextFactory.java,getInstance() --> getContext() --> createContext()
public abstract class NamedContextFactory<C extends NamedContextFactory.Specification>
implements DisposableBean, ApplicationContextAware {
private Map<String, C> configurations = new ConcurrentHashMap<>();
public <T> T getInstance(String name, Class<T> type) {
AnnotationConfigApplicationContext context = getContext(name);
if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
type).length > 0) {
return context.getBean(type);
}
return null;
}
protected AnnotationConfigApplicationContext getContext(String name) {
if (!this.contexts.containsKey(name)) {
synchronized (this.contexts) {
if (!this.contexts.containsKey(name)) {
this.contexts.put(name, createContext(name));
}
}
}
return this.contexts.get(name);
}
protected AnnotationConfigApplicationContext createContext(String name) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
if (this.configurations.containsKey(name)) {
for (Class<?> configuration : this.configurations.get(name)
.getConfiguration()) {
context.register(configuration);
}
}
for (Map.Entry<String, C> entry : this.configurations.entrySet()) {
if (entry.getKey().startsWith("default.")) {
for (Class<?> configuration : entry.getValue().getConfiguration()) {
context.register(configuration);
}
}
}
context.register(PropertyPlaceholderAutoConfiguration.class,
this.defaultConfigType);
context.getEnvironment().getPropertySources().addFirst(new MapPropertySource(
this.propertySourceName,
Collections.<String, Object> singletonMap(this.propertyName, name)));
if (this.parent != null) {
// Uses Environment from parent as well as beans
context.setParent(this.parent);
}
context.refresh();
return context;
}
}
现在来看Feign.Builder builder = get(context, Feign.Builder.class)这一行代码,其实这一行代码是在FeignClientsConfiguration这个类完成创建并完成Bean对象的注入之后才会执行的,关于具体注入的对象在后面一个章节讲述,这里先大致说一下这一块代码的功能,创建Feign.Builder对象,并将容器中(FeignClientsConfiguration注入的几个Bean)对应的Bean调用setter方法来完成对Feign.Builder的logger-encoder, decoder, contract属性赋值
1.5 FeignClientsConfiguration
接着上面的代码,org.springframework.cloud.netflix.feign.FeignClientFactoryBean#feign里的FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);代码,会去创建每个服务自己的容器,并且会去实例化当前配置类,下面就来看下该类的作用
@Configuration
public class FeignClientsConfiguration {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Autowired(required = false)
private List<AnnotatedParameterProcessor> parameterProcessors = new ArrayList<>();
@Autowired(required = false)
private List<FeignFormatterRegistrar> feignFormatterRegistrars = new ArrayList<>();
@Autowired(required = false)
private Logger logger;
@Bean
@ConditionalOnMissingBean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(this.messageConverters));
}
@Bean
@ConditionalOnMissingBean
public Encoder feignEncoder() {
return new SpringEncoder(this.messageConverters);
}
@Bean
@ConditionalOnMissingBean
public Contract feignContract(ConversionService feignConversionService) {
return new SpringMvcContract(this.parameterProcessors, feignConversionService);
}
@Bean
public FormattingConversionService feignConversionService() {
FormattingConversionService conversionService = new DefaultFormattingConversionService();
for (FeignFormatterRegistrar feignFormatterRegistrar : feignFormatterRegistrars) {
feignFormatterRegistrar.registerFormatters(conversionService);
}
return conversionService;
}
@Configuration
@ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
protected static class HystrixFeignConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "feign.hystrix.enabled", matchIfMissing = false)
public Feign.Builder feignHystrixBuilder() {
return HystrixFeign.builder();
}
}
@Bean
@ConditionalOnMissingBean
public Retryer feignRetryer() {
return Retryer.NEVER_RETRY;
}
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
public Feign.Builder feignBuilder(Retryer retryer) {
return Feign.builder().retryer(retryer);
}
@Bean
@ConditionalOnMissingBean(FeignLoggerFactory.class)
public FeignLoggerFactory feignLoggerFactory() {
return new DefaultFeignLoggerFactory(logger);
}
}
① 该类为一个配置类,被实例化后,识别当前类下的注入的Bean,messageConverters,parameterProcessors,feignFormatterRegistrars,logger等允许注入,除messageConverters系统有默认值外,其它无默认值,但应该都可以自定义并注入容器,然后使之生效。同时下面默认也会像容器中注入几个Bean,前提是用户没有自定义的时候,如 feignDecoder()注入Decoder, feignEncoder注入Encoder, feignContract()注入Contract, feignConversionService注入FormattingConversionService,同样不细究作用;
② 有一个内部类,用来判断如果当前类路径下有Hystrix的包,则该配置类生效,并且如果配置了feign.hystrix.enabled属性,则使用Hystrix来构建HystrixFeign`
@Configuration
@ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
protected static class HystrixFeignConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "feign.hystrix.enabled", matchIfMissing = false)
public Feign.Builder feignHystrixBuilder() {
return HystrixFeign.builder();
}
}
③ feignRetryer,可以看到Feign的重试机制默认是关闭的,该接口有一个内部类,目前调用的是空参的构造函数
@Bean
@ConditionalOnMissingBean
public Retryer feignRetryer() {
return Retryer.NEVER_RETRY;
}
④ feignBuilder()方法,构建一个默认的的Feign.Builder对象,入参的retryer会从容器中获取注入的Retryer来覆盖默认的builder中的Retryer没有任何属性,目前容器中已经通过③的方法feignRetryer()来注入了一个Retryer.NEVER_RETRY类型的Retryer,所以会覆盖默认的Feign.builder()构建出来的重试机制,即不提供重试支持,默认值详见⑤
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
public Feign.Builder feignBuilder(Retryer retryer) {
return Feign.builder().retryer(retryer);
}
这里执行结束后,各个参数的 值如下图
⑤ Feign.Builder对象,看一下内部类Builder,这一块的步骤往下细分一下,其实会覆盖某些之前设置的属性,下面来详细看一下每个方法的默认实现,某些方法不再贴里面的具体实现,到时候可以自行进入某些方法内部查看源码
public abstract class Feign {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private final List<RequestInterceptor> requestInterceptors =
new ArrayList<RequestInterceptor>();
// 默认的日志级别,可选值有NONE, BASIC, HEADERS, FULL
private Logger.Level logLevel = Logger.Level.NONE;
// Defines what annotations and values are valid on interfaces.
private Contract contract = new Contract.Default();
// 提交一个feign.Request的http请求,该实现是线程安全的
private Client client = new Client.Default(null, null);
// 默认的重试机制,有几个属性period为100,maxPeriod为1000,maxAttempts为5,attempt为1,sleptForMillis为0
private Retryer retryer = new Retryer.Default();
// 没有任何属性的logger
private Logger logger = new NoOpLogger();
// 编码
private Encoder encoder = new Encoder.Default();
// 解码
private Decoder decoder = new Decoder.Default();
// 允许自定义对响应异常的处理
private ErrorDecoder errorDecoder = new ErrorDecoder.Default();
// 默认的Request.Options,connectTimeoutMillis为10 * 1000, readTimeoutMillis为60 * 1000
private Options options = new Options();
// Controls reflective method dispatch.
private InvocationHandlerFactory invocationHandlerFactory =
new InvocationHandlerFactory.Default();
private boolean decode404;
}
1.6 FeignClientProperties
① 配置前缀feign.client
@ConfigurationProperties("feign.client")
public class FeignClientProperties {
private boolean defaultToProperties = true;
private String defaultConfig = "default";
private Map<String, FeignClientConfiguration> config = new HashMap<>();
}
② 该类有一个内部类FeignClientConfiguration,通过config属性的setter/getter方法来将该内部类赋值给该类的属性,而且该属性是一个map形式,value才是内部类,所以再配置属性的时候,可以指定一个Key,所以如果需要配置FeignClientConfiguration下的属性,经后面分析,为什么使用map形式存储属性对象,是因为当前项目需要调用多个项目的Feign接口,所以可以使用注册的服务名为每个服务单独设置不同的属性,而如果需要所有的服务公用的配置,则配置在default这个key下,为什么是default,是因为取值属性defaultConfig,需要使用feign.client.key.config,可配置属性如下
feign:
client:
myFeign:
readTimeout: 5000
connectTimeout: 2000
default:
readTimeout: 6000
connectTimeout: 3000
public static class FeignClientConfiguration {
private Logger.Level loggerLevel;
private Integer connectTimeout;
private Integer readTimeout;
private Class<Retryer> retryer;
private Class<ErrorDecoder> errorDecoder;
private List<Class<RequestInterceptor>> requestInterceptors;
private Boolean decode404;
}
1.7 再看FeignClientFactoryBean
接之前已经露过面的一次configureFeign()方法,这个方法获取了上面FeignClientProperties这个bean,在这里会初始化FeignClientProperties的各种属性,FeignClientProperties有一个属性defaultToProperties默认为true,所以走的是if里的方法,代码如下,
class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean,
ApplicationContextAware {
@Override
public Object getObject() throws Exception {
FeignContext context = applicationContext.getBean(FeignContext.class);
Feign.Builder builder = feign(context);
if (!StringUtils.hasText(this.url)) {
String url;
if (!this.name.startsWith("http")) {
url = "http://" + this.name;
}
else {
url = this.name;
}
url += cleanPath();
return loadBalance(builder, context, new HardCodedTarget<>(this.type,
this.name, url));
}
if (StringUtils.hasText(this.url) && !this.url.startsWith("http")) {
this.url = "http://" + this.url;
}
String url = this.url + cleanPath();
Client client = getOptional(context, Client.class);
if (client != null) {
if (client instanceof LoadBalancerFeignClient) {
// not lod balancing because we have a url,
// but ribbon is on the classpath, so unwrap
client = ((LoadBalancerFeignClient)client).getDelegate();
}
builder.client(client);
}
Targeter targeter = get(context, Targeter.class);
return targeter.target(this, builder, context, new HardCodedTarget<>(
this.type, this.name, url));
}
protected Feign.Builder feign(FeignContext context) {
FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);
Logger logger = loggerFactory.create(this.type);
// @formatter:off
Feign.Builder builder = get(context, Feign.Builder.class)
// required values
.logger(logger)
.encoder(get(context, Encoder.class))
.decoder(get(context, Decoder.class))
.contract(get(context, Contract.class));
// @formatter:on
configureFeign(context, builder);
return builder;
}
protected void configureFeign(FeignContext context, Feign.Builder builder) {
FeignClientProperties properties = applicationContext.getBean(FeignClientProperties.class);
if (properties != null) {
if (properties.isDefaultToProperties()) {
configureUsingConfiguration(context, builder);
configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
configureUsingProperties(properties.getConfig().get(this.name), builder);
} else {
configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
configureUsingProperties(properties.getConfig().get(this.name), builder);
configureUsingConfiguration(context, builder);
}
} else {
configureUsingConfiguration(context, builder);
}
}
protected void configureUsingConfiguration(FeignContext context, Feign.Builder builder) {
Logger.Level level = getOptional(context, Logger.Level.class);
if (level != null) {
builder.logLevel(level);
}
Retryer retryer = getOptional(context, Retryer.class);
if (retryer != null) {
builder.retryer(retryer);
}
ErrorDecoder errorDecoder = getOptional(context, ErrorDecoder.class);
if (errorDecoder != null) {
builder.errorDecoder(errorDecoder);
}
Request.Options options = getOptional(context, Request.Options.class);
if (options != null) {
builder.options(options);
}
Map<String, RequestInterceptor> requestInterceptors = context.getInstances(
this.name, RequestInterceptor.class);
if (requestInterceptors != null) {
builder.requestInterceptors(requestInterceptors.values());
}
if (decode404) {
builder.decode404();
}
}
protected void configureUsingProperties(FeignClientProperties.FeignClientConfiguration config, Feign.Builder builder) {
if (config == null) {
return;
}
if (config.getLoggerLevel() != null) {
builder.logLevel(config.getLoggerLevel());
}
if (config.getConnectTimeout() != null && config.getReadTimeout() != null) {
builder.options(new Request.Options(config.getConnectTimeout(), config.getReadTimeout()));
}
if (config.getRetryer() != null) {
Retryer retryer = getOrInstantiate(config.getRetryer());
builder.retryer(retryer);
}
if (config.getErrorDecoder() != null) {
ErrorDecoder errorDecoder = getOrInstantiate(config.getErrorDecoder());
builder.errorDecoder(errorDecoder);
}
if (config.getRequestInterceptors() != null && !config.getRequestInterceptors().isEmpty()) {
// this will add request interceptor to builder, not replace existing
for (Class<RequestInterceptor> bean : config.getRequestInterceptors()) {
RequestInterceptor interceptor = getOrInstantiate(bean);
builder.requestInterceptor(interceptor);
}
}
if (config.getDecode404() != null) {
if (config.getDecode404()) {
builder.decode404();
}
}
}
}
① 先看方法configureUsingConfiguration,从FeignContext中获取这些bean如果不为空的话,就覆盖之前做的默认值,所以如果我们自定义这些bean的放入到容器的时候,则从FeignContext中一旦能够获取到这些bean,就可以覆盖到系统默认的处理,这里给我们自定义留下了支持
configureUsingConfiguration(context, builder);
protected void configureUsingConfiguration(FeignContext context, Feign.Builder builder) {
// 目前容器没有注入`Logger.Level`,所以这里使用的还是Feign.Builder的默认值
Logger.Level level = getOptional(context, Logger.Level.class);
if (level != null) {
builder.logLevel(level);
}
// 参考FeignClientsConfiguration,容器中默认注入了一个`Retryer.NEVER_RETRY`
Retryer retryer = getOptional(context, Retryer.class);
if (retryer != null) {
builder.retryer(retryer);
}
// 没有注入`ErrorDecoder`,所以使用的还是Feign.Builder的默认值
ErrorDecoder errorDecoder = getOptional(context, ErrorDecoder.class);
if (errorDecoder != null) {
builder.errorDecoder(errorDecoder);
}
// 默认通过`FeignRibbonClientAutoConfiguration`的`feignRequestOptions()`方 // 注入了一个Request.Options
// 详见下一节FeignRibbonClientAutoConfiguration,拿到这个`bean`,覆盖原属性
Request.Options options = getOptional(context, Request.Options.class);
if (options != null) {
builder.options(options);
}
// 未细究
Map<String, RequestInterceptor> requestInterceptors = context.getInstances(
this.name, RequestInterceptor.class);
if (requestInterceptors != null) {
builder.requestInterceptors(requestInterceptors.values());
}
// 未细究
if (decode404) {
builder.decode404();
}
}
② configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder)方法,作用是应用配置文件中的默认的配置,properties的类型为FeignClientProperties,config形式为Map,相关细节在FeignClientProperties这一节已详细讲解,所以这里是把配置文件下的feign.client.default下的属性应用起来,可以配置的属性有如下方法内部,可以看到按照顺序,默认配置会覆盖第一步里的配置,配置文件的优先级高于配置类的优先级(包括使用配置类的方法注入自定义的bean)
configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder)
protected void configureUsingProperties(FeignClientProperties.FeignClientConfiguration config, Feign.Builder builder) {
if (config == null) {
return;
}
if (config.getLoggerLevel() != null) {
builder.logLevel(config.getLoggerLevel());
}
if (config.getConnectTimeout() != null && config.getReadTimeout() != null) {
builder.options(new Request.Options(config.getConnectTimeout(), config.getReadTimeout()));
}
if (config.getRetryer() != null) {
Retryer retryer = getOrInstantiate(config.getRetryer());
builder.retryer(retryer);
}
if (config.getErrorDecoder() != null) {
ErrorDecoder errorDecoder = getOrInstantiate(config.getErrorDecoder());
builder.errorDecoder(errorDecoder);
}
if (config.getRequestInterceptors() != null && !config.getRequestInterceptors().isEmpty()) {
// this will add request interceptor to builder, not replace existing
for (Class<RequestInterceptor> bean : config.getRequestInterceptors()) {
RequestInterceptor interceptor = getOrInstantiate(bean);
builder.requestInterceptor(interceptor);
}
}
if (config.getDecode404() != null) {
if (config.getDecode404()) {
builder.decode404();
}
}
}
③ configureUsingProperties(properties.getConfig().get(this.name), builder);作用是应用当前Feign应用特有的属性配置,可配置的属性与上面一样,但是属性类放入config属性Map的key为Feign接口应用的名称
④ properties.isDefaultToProperties(),defaultToProperties的默认值为true,如果为true,则应用配置的顺序是先应用属性类的key和自己应用一样名称的配置,然后再应用default的配置,最后应用配置类的属性;而如果这个属性的值为false,则应用顺序正好相反
⑤ feign()方法执行完成之后,回到getObject()方法,该类的type属性是每个标注了@FeignClient接口类,判断注解中是否明确了url地址,如果没有的话,下面判断来定义url的规则为http://name/path即服务名和注解指定的path属性,即应用的ContextPath和每个接口类的具体实现类的@RequestMapping,new HardCodedTarget<>(this.type, this.name, url)生成调用目标地址信息的代理类
1.8 FeignRibbonClientAutoConfiguration
该类位于Feign包下的ribbon包下,Feign的负载均衡是基于ribbon的,该类的全路径为org.springframework.cloud.netflix.feign.ribbon.FeignRibbonClientAutoConfiguration,
该类代码如下:
@ConditionalOnClass({ ILoadBalancer.class, Feign.class })
@Configuration
@AutoConfigureBefore(FeignAutoConfiguration.class)
@EnableConfigurationProperties({ FeignHttpClientProperties.class })
//Order is important here, last should be the default, first should be optional
// see https://github.com/spring-cloud/spring-cloud-netflix/issues/2086#issuecomment-316281653
@Import({ HttpClientFeignLoadBalancedConfiguration.class,
OkHttpFeignLoadBalancedConfiguration.class,
DefaultFeignLoadBalancedConfiguration.class })
public class FeignRibbonClientAutoConfiguration {
@Bean
@Primary
@ConditionalOnMissingClass("org.springframework.retry.support.RetryTemplate")
public CachingSpringLoadBalancerFactory cachingLBClientFactory(
SpringClientFactory factory) {
return new CachingSpringLoadBalancerFactory(factory);
}
@Bean
@Primary
@ConditionalOnClass(name = "org.springframework.retry.support.RetryTemplate")
public CachingSpringLoadBalancerFactory retryabeCachingLBClientFactory(
SpringClientFactory factory,
LoadBalancedRetryPolicyFactory retryPolicyFactory,
LoadBalancedBackOffPolicyFactory loadBalancedBackOffPolicyFactory,
LoadBalancedRetryListenerFactory loadBalancedRetryListenerFactory) {
return new CachingSpringLoadBalancerFactory(factory, retryPolicyFactory, loadBalancedBackOffPolicyFactory, loadBalancedRetryListenerFactory);
}
@Bean
@ConditionalOnMissingBean
public Request.Options feignRequestOptions() {
return LoadBalancerFeignClient.DEFAULT_OPTIONS;
}
}
① 先看方法feignRequestOptions(), @ConditionalOnMissingBean注解,如果当前项目中还没有Request.Options这个Bean则注入这个Bean,属于默认配置,可以看到如果自定义这个Bean的注入,则这里的代码会失效。然后参考上一节的FeignClientFactoryBean的configureUsingConfiguration()方法,则我们注入的bean会生效。来看一下系统的默认配置,可以看到最终请求Request.Options.的 connectTimeoutMillis的默认值为10 * 1000, readTimeoutMillis的默认值为60 * 1000
@Bean
@ConditionalOnMissingBean
public Request.Options feignRequestOptions() {
return LoadBalancerFeignClient.DEFAULT_OPTIONS;
}
// 如上方法指向了这里
public class LoadBalancerFeignClient implements Client {
static final Request.Options DEFAULT_OPTIONS = new Request.Options();
}
// 如上方法指向了这里
public final class Request {
public static class Options {
private final int connectTimeoutMillis;
private final int readTimeoutMillis;
public Options(int connectTimeoutMillis, int readTimeoutMillis) {
this.connectTimeoutMillis = connectTimeoutMillis;
this.readTimeoutMillis = readTimeoutMillis;
}
public Options() {
this(10 * 1000, 60 * 1000);
}
}
1.9 LoadBalancerFeignClient
客户端调用Feign接口通过反射最终执行如下方法
@Override
public Response execute(Request request, Request.Options options) throws IOException {
try {
URI asUri = URI.create(request.url());
String clientName = asUri.getHost();
URI uriWithoutHost = cleanUrl(request.url(), clientName);
FeignLoadBalancer.RibbonRequest ribbonRequest = new FeignLoadBalancer.RibbonRequest(
this.delegate, request, uriWithoutHost);
IClientConfig requestConfig = getClientConfig(options, clientName);
return lbClient(clientName).executeWithLoadBalancer(ribbonRequest,
requestConfig).toResponse();
}
catch (ClientException e) {
IOException io = findIOException(e);
if (io != null) {
throw io;
}
throw new RuntimeException(e);
}
}
request包含当前请求信息url,head,body,charset,如下图
options包含连接connectTimeoutMillis和readTimeoutMillis,这个在前面已经看到默认分别是10000和60000,关于如何zi自定义配置前面也已经说过
方法体内代码asUri为完整请求地址,包含请求协议://服务名/服务上下文/请求映射路径+参数,clientName为解析请求中的服务名,uriWithoutHost解析请求地址去除服务名,下一步构建FeignLoadBalancer.RibbonRequest对象ribbonRequest,其中this.delegate的类型为feign.Client,默认使用的是它的实现类Client.Default,构建步骤具体为下,直接贴代码看一眼就行,其中Uri往下看似乎已经是经过UTF-8编码过了,但是body没有经过编码,总体而言该对象包含了当前请求所需要的重要信息 this.delegate的赋值通过以下类指定
@Configuration
class DefaultFeignLoadBalancedConfiguration {
@Bean
@ConditionalOnMissingBean
public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
SpringClientFactory clientFactory) {
return new LoadBalancerFeignClient(new Client.Default(null, null),
cachingFactory, clientFactory);
}
}
构建Request请求信息
RibbonRequest(Client client, Request request, URI uri) {
this.client = client;
setUri(uri);
this.request = toRequest(request);
}
private Request toRequest(Request request) {
Map<String, Collection<String>> headers = new LinkedHashMap<>(
request.headers());
return Request.create(request.method(),getUri().toASCIIString(),headers,request.body(),request.charset());
}
下面来看下面的代码调用了一个方法getClientConfig(),可以看到这里配置IClientConfig对象的时候如果options使用的是系统默认的对象时,则会触发方法getClientConfig(),而如果不是由系统默认的这个对象,而是我们自己自定义注入过这个对象(无论是配置类还是配置文件),则会触发代码new FeignOptionsClientConfig(options);
IClientConfig requestConfig = getClientConfig(options, clientName);
// 方法内部
IClientConfig getClientConfig(Request.Options options, String clientName) {
IClientConfig requestConfig;
if (options == DEFAULT_OPTIONS) {
requestConfig = this.clientFactory.getClientConfig(clientName);
} else {
requestConfig = new FeignOptionsClientConfig(options);
}
return requestConfig;
}
先看简单的requestConfig = new FeignOptionsClientConfig(options);该方法内部如下,则可以看到最终IClientConfig 对象只会有两个属性,一个CommonClientConfigKey.ConnectTimeout,一个CommonClientConfigKey.ReadTimeout,而且两个值的属性使我们自定义的;
public FeignOptionsClientConfig(Request.Options options) {
setProperty(CommonClientConfigKey.ConnectTimeout,
options.connectTimeoutMillis());
setProperty(CommonClientConfigKey.ReadTimeout, options.readTimeoutMillis());
}
现在来看如果没有修改过默认的请求属性options == DEFAULT_OPTIONS,这一块看的有点晕乎,在之前看到Feign如果没有任何配置,系统已经默认了connectTimeoutMillis和readTimeoutMillis,这个在前面已经看到默认分别是10000和60000,但是代码在这里处理判断如果使用的是默认的,加载的属性列表如下,会对之前所有的默认操作进行覆盖
2.0 FeignLoadBalancer
@Override
public RibbonResponse execute(RibbonRequest request, IClientConfig configOverride)
throws IOException {
Request.Options options;
if (configOverride != null) {
options = new Request.Options(
configOverride.get(CommonClientConfigKey.ConnectTimeout,
this.connectTimeout),
(configOverride.get(CommonClientConfigKey.ReadTimeout,
this.readTimeout)));
}
else {
options = new Request.Options(this.connectTimeout, this.readTimeout);
}
Response response = request.client().execute(request.toRequest(), options);
return new RibbonResponse(request.getUri(), response);
}
如果在之前没有对Feign进行过任何配置,那么这里就会加载默认的属性,一旦加载默认的属性,则目前调试下来会有40个属性,默认的ReadTimeout=1000, ConnectTimeout=1000,如下图所示
如果我们自定义过当前请求Feign的属性,那么IClientConfig对象则会有我们设置的属性以及值,比如我们设置了如下配置则,当前configOverride就会有这两个属性的值,而不是默认的40个。目前还没搞清楚其余字段的意思
feign:
client:
config:
default:
readTimeout: 3333
connectTimeout: 4444
依然是上面的execute()方法,代码从入参之后继续往下走,现在看到new 了一个新的Request.Options对象,下面判断configOverride是否为空,经过上面的描述,这个对象不为空,如果我们自定义过,则会有两个属性,如果没有自定义过,则会有默认的属性,通过configOverride来构建Request.Options对象的代码,可以看到其实仅仅用到了ConnectTimeout和ReadTimeout两个属性,然后调用Request.Options的构造方法来进行赋值,构造方法如下:
public Options(int connectTimeoutMillis, int readTimeoutMillis) {
this.connectTimeoutMillis = connectTimeoutMillis;
this.readTimeoutMillis = readTimeoutMillis;
}
自此Request.Options对象的两个属性connectTimeoutMillis和readTimeoutMillis的属性处理完成
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。