SpringBootマイクロサービスの理解と自動設定

それは何であるmicroService

全体のアプリケーションと比較して、マイクロサービスは、過去にこの大規模なプログラムから引き出された小型モジュールとすることができ、私は電気事業プロジェクト、順序モジュール内のユーザモジュールのような形を、なぜそれを行う、などとして抽出しますアプリケーションは、複数のサーバーに展開されてきた、と後でプログラムを変更することなく、すべてのサーバーで再手術でフィニッシュラインを変更すること、としても知られているように人的・物的資源の浪費が、プログラムが複数のモジュールに分割することができ、モジュール、必要な場合は、マイクロサービス、複数のサーバ上のマイクロダイナミックなサービスの展開、複数の機能を変更する必要がある場合は、単に棚マイクロサービスを指定し、他のサービスが影響を受けたマイクロではありません。HTTP相互作用のマイクロ間のサービス付き;開発者はまた、より明確なチームとの間で分割、独自の開発言語やデータベースを選択することができ、開発のマイクロサービスのメンバーのセットに対応してもよいです。

SpringBoot自動設定の原則

直接の主な方法@注釈から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 {};
}

@参照インポート({ AutoConfigurationImportSelector.classその中に})自動コンフィギュレーションセレクタ開くクラスを、その後に表示することを指すAutoConfigurationImportSelectorのクラス有するselectImportsを)(オプション、および方法依存アセンブリ導入getAutoConfigurationEntryを()この方法は、次のコードブロック内の6行目:

    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);
        }
    }

6行目は、コードブロックの持っていることがわかる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;
    }

この方法では、緑の言葉の裏を見て、本当にこのリソースが空であるかどうかを判断するようだアサートnotEmpty、していないクラスは、コンフィギュレーションの自動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のファイルが終了する工場を、次に方法を入力します。

	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);
		}
	}

ClassLoader.getResourcesは、try(FACTORIES_RESOURCE_LOCATION)で見ることができます:
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));など、Aコード、クラスローダがロードFACTORIES_RESOURCE_LOCATIONからのリソース:

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

SpringFactoriesLoaderのクラスは、リソースFACTORIES_RESOURCE_LOCATION META-INF /この文書をspring.factoriesで定義された位置を有し、原稿を自動的springbootジャー内に配置されている。
ここに画像を挿入説明
このような各クラスXXXAutoConfigurationはSpringboot自動構成が最後Springコンテナに追加されます上記。

公開された12元の記事 ウォンの賞賛3 ビュー247

おすすめ

転載: blog.csdn.net/qq_38599840/article/details/104138942