アプリケーションの構成ソースは、通常、リモート構成サーバーサーバーです。デフォルトでは、ローカル構成の優先度はリモート構成リポジトリよりも低くなります。リモートリポジトリのプロパティ値を上書きするようにローカルアプリケーションシステム変数と構成ファイルを実装する場合は、次のように設定できます。
スプリング:クラウド:config:aloldoverride:true voverridenone:true voverridesystemproperties:false
上記の構成により、クライアントはローカル構成でより高い優先度を達成でき、上書きできません。 Spring Cloudの現在のバージョンはEdgware.RELEASEに基づいているため、上記の設定は機能しませんが、 PropertySourceBootstrapPropertiesのデフォルト値を使用します。特定の状況については、https://github.com/spring-cloud/spring-commons/pull/250を参照してください。次の分析では、バグの特定のソースについて説明します。
ソースコード分析
configservicepropertysourcelocator
リモート構成プロパティを上書きすることは、最終的にクライアントの開始時に構成を取得することに関連しています。構成を取得した後にそれに対処する方法は? Spring Cloud Configのリソース取得クラスConfigServicePropertySourcelocatorのクラス図を見てみましょう。
configservicepropertysourcelocatorは基本的にプロパティリソースロケーターであり、その主な方法は場所(環境環境)です。まず、ConfigClientPropertiesのプレースホルダーを、アプリケーションが現在実行されている環境のアプリケーション、プロファイル、およびラベルに置き換え、RESTTEMPLATEを初期化し、有効な構成情報が取得されるまでラベル配列を繰り返し、最終的に迅速に失敗するかどうかに基づいて再試行します。主なプロセスは次のとおりです。
httpを介してリモートサーバー上の構成データを取得するために、getRemoteenVironment(RESTTEMPLATE、PROPERTIES、LABEL、STATE)メソッドを見つけます。実装も非常に簡単です。交換リクエストのパスにプレースホルダーを表示し、ヘッダーを組み立てます。組み立てたら、リクエストを送信でき、結果が返されます。
上記の実装では、取得した構成情報がCompositePropertySourceに保存されていることがわかります。どのように使用しますか?ここに追加するもう1つの重要なクラスは、ApplicationContextInitializerインターフェイスを実装するPropertySourceBootStrapConfigurationです。このインターフェイスは、アプリケーションのコンテキストが更新される前に()呼び戻される()更新され、それにより初期化操作が実行されます。アプリケーションの開始後のコールスタックは次のとおりです。
SpringApplicationBuilder.run() - > SpringApplication.run() - > SpringApplication.CreatENDREFRESHCONTEXT() - > SpringApplication.ApplyInitializers() - > PropertySourceBootStrapConfiguration.Initialize()PropertySourceBottrapContrapConturation
上記のConfigServicePropertySourcelocatorの位置的方法は、Contextが更新する前に必要な構成情報を取得できるように、初期化で呼び出されます。初期化方法を見てみましょう。
public class propertySourceBootStrapConfigurationImplements ApplicationContextInitializer <ConfigureAbleAppLicationContext>、Ordered {private int Order = Ordered.heist_precedence + 10; @autowired(必須= false)プライベートリスト<propertySourcelocator> propertySourcelocators = new ArrayList <>(); @Override public void initialize(configureableapplicationContext applicationContext){compositePropertySource composite = new compositepropertysource(bootstrap_property_source_name); // propertySourcelocatorsアレイを並べ替えて、デフォルトのAnnotationAwareOrderComparator AnnotationAwareOrderComparator.Sort(this.PropertySourcelocators)に従ってください。 boolean empty = true; //実行中の環境コンテキストConfigurableEnvironment Environment = applicationContext.getEnvironment(); for(propertySourcelocatorロケーター:this.propertysourcelocators){// transtraight this.propertysourcelocators propertySource <?> source = null; source = locator.locate(環境); if(source == null){続行; } logger.info( "emated property source:" + source); // propertySource composite.addpropertysource(source)のリンクリストにソースを追加します。 empty = false; } //ソースが空でない場合にのみ、(!空){// AddFirstおよびAddLast MutablePropertySourcesなどの実行できる環境の変数形式を返す場合、環境に設定されます。 string logconfig = Environment.ResolvePlaceHolders( "$ {logging.config:}"); logfile logfile = logfile.get(環境); if(propertySources.contains(bootstrap_property_source_name)){// bootstrapproperties propertySources.remove(bootstrap_property_source_name); } // propertySources insertPropertySources(propertySources、composite)を設定します。 Reinitializeloggingsystem(環境、logconfig、logfile); setloglevels(環境); //プロファイルの複数のアクティブ構成情報を処分します。 }} // ...}初期化方法で実行される操作を見てみましょう。
addfirst、addlastなど、実行できる可変環境形式を返します
PropertySourcesでBootStrAppropertiesを削除します
構成サーバーオーバーライドのルールに従って、PropertySourcesを設定します
複数のアクティブプロファイルの構成情報を処理します
初期化メソッドを初期化するとき、最初にすべてのPropertySourcelocatorタイプオブジェクトの[ロケートメソッドを通過し、次にさまざまな方法で取得したプロパティ値をComposityPropertySourceに配置し、最後にInsertPropertySources(PropertySources、Composite)メソッドを呼び出して環境に設定します。 Spring Cloud Contextは、リモートプロパティを上書きするPropertySourceBootStrAppropertiesを提供し、この構成クラスを使用してプロパティソースの優先度を決定します。
private void InsertPropertySources(MutablePropertySources PropertySources、CompositePropertySource Composite){MutablePropertySources coming = new MutablePropertySources(); Incoming.addfirst(composite); propertySourceBoOtStrApproperties RemoteProperties = new PropertySourceBootStrApproperties();新しいrackedDataBinder(RemoteProperties、 "spring.cloud.config").bind(new propertySourcesPropertyValues(incoming)); //ローカルオーバーライトが許可されていない場合(!RemoteProperties.isAlloWoverRide()||(!RemoteProperties.IsoverRidenone()&& RemoteProperties.IsoverRidesSystemProperties())){propertySources.Addfirst(composite);戻る; } //オーバーリデノンは真で、外部構成は(remoteproperties.isoverridenone()){propertySources.addlast(composite);戻る; } if(propertySources .contains(StandardEnvironment.system_environment_property_source_name)){// OverridesSystemPropertiesに従って外部構成の優先度を設定するif(!RemoteProperties.IsoverRidesExtystemProperties()){PropertySources.Adddafter StandardEnvironment.system_environment_property_source_name、composite); } else {propertySources.addbefore(StandardEnvironment.system_environment_property_source_name、composite); }} else {propertySources.addlast(composite); }}上記の実装は、主に、 PropertySourceBootStrAppropertiesのプロパティに基づいて、複数の構成ソースの優先度を調整します。その実装から、 PropertySourceBootStrAppropertiesオブジェクトは、デフォルトのプロパティ値を使用して直接初期化され、構成ファイルに設定されたものに注入されていないことがわかります。
修復された実装:
@autowired(必須= false)private propertySourceBoOtStrApproperties RemotePropertiesForoverRiding; @Override public int getOrder(){return this.order; private void InsertPropertySources(MutablePropertySources PropertySources、CompositePropertySource Composite){MutablePropertySources coming = new MutablePropertySources(); Incoming.addfirst(composite); PropertySourceBoOtStrApproperties RemoteProperties = RemotePropertiesForOverriding == null? new propertySourceBootStrApproperties():RemotePropertiesForoverRiding;要約します
上記は、エディターが導入したスプリングクラウドオーバーライドリモート構成プロパティの例の詳細な説明です。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!