Spring Cloud Configの基本的な使用に関して、以前のブログで言及されています。わからない場合は、最初に前のブログを読んでください。
Spring Cloud ConfigはGitLabを統合して分散構成センターを構築します
スプリングクラウド構成分散構成センターの高可用性
今日、私たちの焦点は、データソースのホットな展開を達成する方法にあります。
1.クライアントのデータソースを構成します
@RefreshScope @configuration //データソースパブリッククラスDataSourceConfigure {@bean @refreshscope // refferity configuration file @configurationProperties(prefix = "spring.dataSource")//データソースパブリックデータソース(){return datasourcebuilder.create() }}上記の手順を通じて、gitlabの構成ファイルを変更できます。更新後、サーバーを再起動する必要はなく、新しいデータソースが有効になります。
2。カスタムデータソースのホット展開
Spring Bootを使用してDruidを統合する場合、データソースを手動で構成する必要があります。コードは次のとおりです。
パッケージcom.chhliu.springcloud.config; java.sql.sqlexceptionをインポートします。 javax.sql.datasourceをインポートします。 Import org.springframework.beans.factory.annotation.value; org.springframework.cloud.context.config.annotation.refreshscopeをインポートします。 org.springframework.context.annotation.beanをインポートします。 org.springframework.context.annotation.configurationをインポートします。 org.springframework.context.annotation.primaryをインポートします。 com.alibaba.druid.pool.druiddatasourceをインポートします。 Lombok.extern.slf4j.slf4jをインポートします。 / ** * *説明:データソースがコードを使用して手動で初期化されていない場合、監視インターフェイスのSQL監視にはデータがありません(「スプリングブートバグ???」) @value( "$ {spring.datasource.url}")private string dburl; @value( "$ {spring.datasource.username}")private string username; @value( "$ {spring.datasource.password}")private string password; @value( "$ {spring.datasource.driverclassname}")private string driverclassname; @value( "$ {spring.datasource.initialsize}")private int hientize; @value( "$ {spring.datasource.minidle}")private int minidle; @value( "$ {spring.datasource.maxactive}")private int maxactive; @value( "$ {spring.datasource.maxwait}")private int maxwait; @value( "$ {spring.datasource.timebetweenevictionrunsmillis}")private int timebetweenevictionrunsmillis; @value( "$ {spring.datasource.minevictableidletimemillis}")private int minevictableidletimemillis; @value( "$ {spring.datasource.validationQuery}")private string balidationquery; @value( "$ {spring.datasource.testhileidle}")private boolean testhiledle; @value( "$ {spring.datasource.testonborrow}")private boolean testonborrow; @value( "$ {spring.datasource.testonborrow}")private boolean testonreturn; @value( "$ {spring.datasource.testonborrow}")private boolean poolpreparedStatements; @value( "$ {spring.datasource.maxpoolpreparedstatementperconnectionsize}")private int maxpoolpreparedstatementperconnectionsize; @value( "$ {spring.datasource.filters}")プライベート文字列フィルター。 @value( "$ {spring.datasource.connectionproperties}")private string connectionProperties; @value( "$ {spring.datasource.useglobaldatasourcestat}")private boolean useglobaldatasourcestat; @bean //それをBean instance @primary //同じデータソースで宣言するには、最初にAnnotated DataSource @RefreshScope Public DataSource DataSource(){DruiddataSource DataSource = new Druiddatasource(); dataSource.seturl(this.dburl); DataSource.setUsername(username); dataSource.setPassWord(パスワード); DataSource.setDriverClassName(DriverClassName); //構成DataSource.setInitialSize(hientsize); DataSource.setMinidle(Minidle); DataSource.setMaxactive(maxactive); DataSource.setMaxWait(MaxWait); dataSource.settimebetweenevictionrunsmillis(timevethictionrunsmillis); dataSource.setMineVictableIdletimemillis(minevictableidletimemillis); dataSource.setValidationQuery(ValidationQuery); dataSource.settestwhileidle(testhiledle); dataSource.settestonborrow(testonborrow); DataSource.SetteStonreturn(TestOnreturn); DataSource.setPoolPreparedStatements(PoolPreparedStatements); DataSource.setMaxPoolPreparedStatementPerConnectionSize(MaxPoolPreparedStatementPerConnectionSize); dataSource.setuseglobaldatasourcestat(useglobaldatasourcestat); try {dataSource.setFilters(フィルター); } catch(sqlexception e){log.error( "Druid構成初期化フィルター:"+ e); } dataSource.setConnectionProperties(ConnectionProperties); DataSourceを返します。 }}上記の例により、データソースの動的な更新も実現できます。次に、Spring Cloud Configがデータソースのホット展開をどのように実装するかを見てみましょう。
以前のブログから、動的な更新を実現するために、キーポイントはPost Refreshのリクエストであるため、構成ファイルを更新することから始めます。
更新リクエストを投稿すると、リクエストはアクチュエータモジュールによって傍受されます。これは、起動ログファイルから見ることができます
コードコピーは次のとおりです。
マッピング "{[ /refresh || /refresh.json]、methods= [post]}" "to public java.lang.object org.springframework.cloud.endpoint.genericpostablemvendpoint.invoke()
次に、アクチュエータによって定義されたエンドポイントを見てみましょう。その後、ReshendPointクラスを見つけます。このクラスのソースコードは次のとおりです。
@configurationProperties(prefix = "endpoints.refresh"、Ingroreunknownfields = false)@managedresource public class refleshendpoint extends abstractendpoint <collection <string >> {private contextrefresher contextrefresher; public RefreshendPoint(contextrefresher contextrefresher){super( "refresh"); this.contextrefresher = contextrefresher; } @managedoperation public string [] refresh(){set <string> keys = contextrefresher.refresh(); return keys.toarray(new String [keys.size()]); } @override public collection <string> invoke(){return arrays.aslist(refresh()); }}上記のソースコードから、Contextrefresherクラスに焦点が当てられていることがわかります。このクラスは長すぎるため、以下はこのクラスの部分的なソースコードです。
プライベートリフレッシュスコープスコープ。 public contextrefresher(configureableapplicationContextコンテキスト、refreshScopeスコープ){this.context = context; this.scope = scope; } public synchronized set <string> refresh(){map <string、object> before =抽出(this.context.getenvironment()。getPropertySources()); //1。抽出(this.context.getEnvironment()。getPropertySources())。keyset(); // 3。元の環境変数の値を置き換えます。キーを返します。 }上記のコードから、キーポイントが4つのステップを経て、上記のコードでマークされていることを確認することは難しくありません。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。