对SpringBoot微服务的理解与自动配置

微服务是什么呢

相比于在一个整体应用程序来说,微服务可以说是从这个大程序中抽出来的一个小模块,形如我在电商项目中抽取出用户模块、订单模块,为什么这样做呢,比如一个应用程序已经在多台服务器中部署好了,如果以后程序需要修改,修改完还要重新对所有服务器进行上线操作,这样浪费人力物力,不过一个程序可以分为多个模块,一个模块也称为一个微服务,将多个微服务动态的部署在多个服务器上,如果有功能需要修改,只需下架指定的微服务,其他微服务不受影响;微服务之间用http来进行交互;一套微服务可以对应一套开发成员,开发者可以选择适合自己的开发语言和数据库,也把团队之间划分的更为清晰。

SpringBoot自动配置原理

可以直接从main方法的注解@SpringBootApplication进入:

@SpringBootApplication
public class SpringbootConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootConfigApplication.class, args);
    }
}

点进去可以看到@EnableAutoConfiguration开启自动配置注解:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)

进入@EnableAutoConfiguration

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};
}

可以看到@Import({AutoConfigurationImportSelector.class})导入了开启自动配置选择器这个类,再点进去看到AutoConfigurationImportSelector类有一个selectImports()选择导入方法,导入了哪些组件要看方法中的getAutoConfigurationEntry()方法,在以下代码块的第六行:

    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!this.isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        } else {
            AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
            AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata);
            return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
        }
    }

再点入getAutoConfigurationEntry()获取自动配置实体这个方法:

    protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata annotationMetadata) {
        if (!this.isEnabled(annotationMetadata)) {
            return EMPTY_ENTRY;
        } else {
            AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
            List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
            configurations = this.removeDuplicates(configurations);
            Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
            this.checkExcludedClasses(configurations, exclusions);
            configurations.removeAll(exclusions);
            configurations = this.filter(configurations, autoConfigurationMetadata);
            this.fireAutoConfigurationImportEvents(configurations, exclusions);
            return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
        }
    }

可以看到这代码块的第六行有一个getCandidateConfigurations()获取候选配置方法,点入:

    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

这有一个Assert的notEmpty方法,在这里似乎实在判断这个资源是否为空,再看后面的绿字No auto configuration classes found in META-INF/spring.factories,在META-INF/spring.factories文件中并没有自动配置类,可以猜想要被选择加载的资源就在这个文件里面;点入loadFactoryNames()方法:

	public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
		String factoryTypeName = factoryType.getName();
		return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
	}

可以看到这里有一个loadSpringFactories()加载方法,之前看到的spring.factories文件以factories结尾,再点入该方法:

	private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
		MultiValueMap<String, String> result = cache.get(classLoader);
		if (result != null) {
			return result;
		}

		try {
			Enumeration<URL> urls = (classLoader != null ?
					classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
					ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
			result = new LinkedMultiValueMap<>();
			while (urls.hasMoreElements()) {
				URL url = urls.nextElement();
				UrlResource resource = new UrlResource(url);
				Properties properties = PropertiesLoaderUtils.loadProperties(resource);
				for (Map.Entry<?, ?> entry : properties.entrySet()) {
					String factoryTypeName = ((String) entry.getKey()).trim();
					for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
						result.add(factoryTypeName, factoryImplementationName.trim());
					}
				}
			}
			cache.put(classLoader, result);
			return result;
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Unable to load factories from location [" +
					FACTORIES_RESOURCE_LOCATION + "]", ex);
		}
	}

可以看到在try中有classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));这样一段代码,类加载器从FACTORIES_RESOURCE_LOCATION加载资源:

public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";

SpringFactoriesLoader类中就已经定义FACTORIES_RESOURCE_LOCATION 资源的位置就是META-INF/spring.factories这个文件,这个文件就在springboot自动配置jar中:
在这里插入图片描述
每一个这样的 XXXAutoConfiguration类最后都会加入到Spring容器中,Springboot的自动配置就是以上。

发布了12 篇原创文章 · 获赞 3 · 访问量 247

猜你喜欢

转载自blog.csdn.net/qq_38599840/article/details/104138942
今日推荐