Spring Cloud遠端の構成属性の例を上書きします。


アプリケーションの構成ソースは通常、リモートエンドのConfig Serverサーバであり、デフォルトではローカルの構成優先度がリモートエンドの設定倉庫より低い。ローカルアプリケーションを実行するシステム変数とconfigファイルがリモート倉庫の属性値をカバーする場合、以下のように設定できます。

spring:
cloud:
config:
allowOverride: true
overrideNone: true
overrideSystemProperties: false
  • overrideNone:allowOverrideがtrueである場合、overrideNoneはtrueに設定され、外部の構成優先度はより低く、存在する属性ソースは一切カバーできない。デフォルトはfalse
  • です。
  • allowOverride:overrideSystem Properties属性が有効かどうかを識別します。デフォルトはtrueで、falseとして設定されています。ユーザを禁止する設定
  • です。
  • overrideSystem Properties:外部構成がシステム属性をカバーできるかどうかを識別するために使用され、デフォルトはtrue
  • です。
    クライアントは、上記のように構成することにより、ローカル構成の優先度がより高く、上書きできないようにすることができる。Spring Cloudの現在のバージョンはEdgware.RELEASEに基づいていますので、上記の設定は機能しないで、PropertySourceBootstrapProperties のデフォルト値を使用しています。具体的な状況はissue:https://github.com/spring-cloud/spring-cloud-commons/pull/250に見られます。以下の分析において具体的なbug源について説明します。
    ソース分析
    CofigServicePropertySourceLocator
    上書き遠端の設定属性は、あくまでもクライアントの起動時に設定を取得することに関連していますが、設定を取得したらどう処理しますか?spring cloud configの資源取得類ConfigServicePropertySourceLocatorの類図を見ます。
     
    ConfigServicePropertySourceLocatorは実質的に属性リソースポジショナであり、その主な方法はlocateである。まず、現在のアプリケーションを実行している環境のaplication、profile、labelでconfigClientProptiesの中のプレースホルダを置換して、RestTemplateを初期化し、その後、有効な設定情報を取得するまでlabels配列を遍歴して、最後にまた急速な失敗によって再試行します。主な流れは以下の通りです。
     
    locate(Evironment environment)呼び出しget Remoten Evironment(rets Template、properties、label、state)メソッドはhttpを通じてリモートサーバ上の配置データを取得します。実現も簡単です。代替要求パスパスパスパスパスパスパスパスパスパスパスパスパスパスパスパスパスパスパスパスパスパスパスパスパスのプレースホルダを表示し、ヘッドヘッドヘアースを組み立てて、組み立てたら要求を送り、結果を返します。
    上記の実装において、私達は得られた配置情報をCompossitePropertySourceに保存しているのを見ましたが、それはどうやって使うものですか?ここでもう一つの重要なクラスを補充します。これはアプリのコンテキストがリフレッシュされる前にrefsh()がリセットされ、初期化操作を実行します。アプリケーション起動後の呼び出しスタックは以下の通りです。
    
    SpringApplicationBuilder.run() -> SpringApplication.run() -> SpringApplication.createAndRefreshContext() -> SpringApplication.applyInitializers() -> PropertySourceBootstrapConfiguration.initialize()
    PropertySourceBootstrapConfiguration
    上記のConfigServicePropertySourceLocatorのlocate方法はinitializeで呼び出され、コンテキストがリフレッシュする前に必要な設定情報を得ることができるようにする。initializeの方法を具体的に見てください。
    
    public class PropertySourceBootstrapConfigurationimplements
     ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
     private int order = Ordered.HIGHEST_PRECEDENCE + 10;
     @Autowired(required = false)
     private List<PropertySourceLocator> propertySourceLocators = new ArrayList<>();
     @Override
     public void initialize(ConfigurableApplicationContext applicationContext){
     CompositePropertySource composite = new CompositePropertySource(
     BOOTSTRAP_PROPERTY_SOURCE_NAME);
     // propertySourceLocators      ,     AnnotationAwareOrderComparator
     AnnotationAwareOrderComparator.sort(this.propertySourceLocators);
     boolean empty = true;
     //          
     ConfigurableEnvironment environment = applicationContext.getEnvironment();
     for (PropertySourceLocator locator : this.propertySourceLocators) {
     //  this.propertySourceLocators
     PropertySource<?> source = null;
     source = locator.locate(environment);
     if (source == null) {
     continue;
     }
     logger.info("Located property source: " + source);
     // source   PropertySource    
     composite.addPropertySource(source);
     empty = false;
     }
     //  source      ,     environment 
     if (!empty) {
     //  Environment     ,       addFirst、addLast
     MutablePropertySources propertySources = environment.getPropertySources();
     String logConfig = environment.resolvePlaceholders("${logging.config:}");
     LogFile logFile = LogFile.get(environment);
     if (propertySources.contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
     //  bootstrapProperties
     propertySources.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME);
     }
     //  config server     ,  propertySources
     insertPropertySources(propertySources, composite);
     reinitializeLoggingSystem(environment, logConfig, logFile);
     setLogLevels(environment);
     //    active profiles     
     handleIncludedProfiles(environment);
     }
     }
     //...
    }
    以下では、initialize方法でどのような操作が行われているかを確認します。
  • デフォルトのAnnotationAwareOrder Comprator規則に従って、propertySourceLocators配列を並べ替えます。
  • 実行環境コンテキストCofigrable Evironment
  • を取得する。
  • propertySourceLocatorsを巡回した時
  •             locate方法を呼び出し、取得されたコンテキストenvironment
  • に着信する。
  •              sourceをProptySourceのチェーンに追加します。
  •              sourceが空かどうかの表示スカラempty
  • を設定します。
  • sourceが空でない場合は、environmentに
  • を設定します。
    Evironmentの可変形式を返します。addFirst、addLastなどの操作ができます。
    propertySourcesのブックレットを削除します。
    config server上書きのルールにより、propertySourcesを設定します。
    複数のactive profilesを扱う構成情報
    初期化方法initialize処理時には、すべてのPropertySourceLocatorタイプのオブジェクトのlocate方法を巡回し、様々な方式で得られた属性値をComppositePropertySourceに入れて、最後にinsertPropertySourcesを呼び出します。Spring Cloud Contectでは、遠端属性を上書きするPropertySourceBootstraphPropertiesを提供し、この構成クラスを利用して属性ソースの優先度を判断します。
    
    private void insertPropertySources(MutablePropertySources propertySources,
     CompositePropertySource composite) {
     MutablePropertySources incoming = new MutablePropertySources();
     incoming.addFirst(composite);
     PropertySourceBootstrapProperties remoteProperties = new PropertySourceBootstrapProperties();
     new RelaxedDataBinder(remoteProperties, "spring.cloud.config")
     .bind(new PropertySourcesPropertyValues(incoming));
     //         
     if (!remoteProperties.isAllowOverride() || (!remoteProperties.isOverrideNone()
     && remoteProperties.isOverrideSystemProperties())) {
     propertySources.addFirst(composite);
     return;
     }
     //overrideNone true,         
     if (remoteProperties.isOverrideNone()) {
     propertySources.addLast(composite);
     return;
     }
     if (propertySources
     .contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
     //  overrideSystemProperties,          
     if (!remoteProperties.isOverrideSystemProperties()) {
     propertySources.addAfter(
      StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
      composite);
     }
     else {
     propertySources.addBefore(
      StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
      composite);
     }
     }
     else {
     propertySources.addLast(composite);
     }
    }
    上記の実装は、主にPropertySourceBootstraphPropertiesの属性に基づいて、複数の構成元の優先度を調整する。その実装から、PropertySourceBootstraphPropertiesオブジェクトが直接初期化され、デフォルトの属性値を使用していますが、設定ファイルには入力されていません。
    修復後の実現:
    
    @Autowired(required = false)
     private PropertySourceBootstrapProperties remotePropertiesForOverriding;
     @Override
     public int getOrder(){
     return this.order;
     private void insertPropertySources(MutablePropertySources propertySources,
     CompositePropertySource composite) {
     MutablePropertySources incoming = new MutablePropertySources();
     incoming.addFirst(composite);
     PropertySourceBootstrapProperties remoteProperties = remotePropertiesForOverriding == null
     ? new PropertySourceBootstrapProperties()
     : remotePropertiesForOverriding;
    締め括りをつける
    以上は小编が绍介したSpring Cloudに远端の配置属性の実例を书いて详しく说明しました。皆さんに助けてほしいです。もし何か疑问があれば、メッセージをください。小编はすぐに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。