Spring Cloudは、最近では人気のある分散サービスフレームワークであり、多くの有用なコンポーネントを提供しています。例:構成センター、ユーレカサービスディスカバリー、メッセージバス、ヒューズメカニズムなど。
構成センターは、Spring Cloudの多くのコンポーネントの中で比較的基本的です。構成ファイルの統一された管理を提供し、途切れない環境に簡単に切り替えることができます。
その特定の構造は次のとおりです。
Spring CloudはSpring Bootに基づいており、Spring CloudはSpring Bootと分離できないため、プロジェクトはすべてSpringブーツに基づいています。
通常、gitに構成ファイルを配置しますが、これはバージョン制御に使いやすいです。次に、構成センターの構築について話しましょう。
構成センター構造
まず、スプリングブーツとスプリングクラウドの依存関係を紹介します。
<DependencyManagement> <Dependencies> <Dependency> <groupId> org.springframework.cloud </groupid> <artifactid> spring-cloud-dependencies </artifactid> <bersion> edgware.sr3 </version> <type> pom </type> <scope> scope> <depenty> <redency> <depenty> <depenty> <depenty> <decondency> <depryency> <GroupId> org.springframework.boot </groupid> <artifactid> spring-boot-dependencies </artifactid> <version> 1.5.9.Release </version> <type> pom </type> <scope> import </scope> </dependency> </sependencies> </despencymanagemation>
ここでは、公式の文書を詳しく調べる必要があります。 Spring CloudのFinchleyバージョンはSpring Boot 2.0に基づいており、Spring Boot 1.5で動作できません。 EDGwareバージョンは1.5に基づいており、2.0で適切に機能しません。不必要なトラブルを避けるために、誰もがこれに注意を払う必要があります。
ここでは、Spring CloudのEdgwareとSpring Bootバージョン1.5を使用します。
次に、次のように必要な依存関係を注ぎます。
<Dependencies> <Dependency> <groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-web </artifactid> </dependency> <dependency> <groupid> org.springframework.cloud </groupid> <artifactid> edectidid>
最後に、次のように、構成センターのスタートクラスを書きます。
@SpringBootApplication@enableconfigserverpublic class configserverApplication {public static void main(string [] args){springApplication.run(configserverApplication.class、args); }}@enableconfigserverは、このサービスが構成センターサービスであるとマークしています。特定の情報は、application.propertiesファイルで構成されています。
#serverポートサーバー= 9000#設定ファイルgitアドレスspring.cloud.config.server.git.uri = https://github.com/liubo-tech/spring-cloud-properties# configurationファイル一時ファイルディレクトリspring.cloud.config.server.git.baseir
誰もがサービスポートとgitアドレスを理解しており、3番目は一時ファイルが構成されているディレクトリです。構成センターが呼び出された後、構成センターはgitから構成ファイルをプルし、ローカルにキャッシュします。これは構成キャッシュのディレクトリであり、構成なしで構成し、システムのデフォルトを使用することもできます。このようにして、構成センターがセットアップされ、クラスターをNginxを介して構築して、高度に利用できるようにすることができます。
アクセス構成ファイル形式は次のとおりです。
/{Application }/ {label} ]/ {application} - {profile }.yml/ {label }/ {application }- {application }- {profile }.yml/ {application }- {profile} .properties/ {label }/ {application.propertiesで:
クライアントコール
過去には、構成ファイルがプロジェクトに配置されていたため、さまざまな環境を切り替えると非常に面倒になり、構成の機密情報も開発者にさらされました。
これは、Unified Configuration Centerを使用して回避できます。クライアントがどのように呼び出すかを見てみましょう。
まず、次のように、依存しなければならないJARパッケージをインポートします。
<Dependencies> <Dependency> <GroupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-web </artifactid> </dependency> <dependency> <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-clud-clud-cltig
Spring Cloud ConfigクライアントのJARがプロジェクトのクラスパスの下にある限り、プロジェクトの開始時に構成センターから構成を取得し、BootStrap構成ファイルのSpring.cloud.config.uriプロパティを介して構成センターを指定します。
これにより、クライアントのプロジェクトにBootstrap.ymlまたはbootstrap.propertiesファイルが必要になります。そうしないと、クライアントは構成センターから構成ファイルをロードしません。
Bootstrap.Propertiesを次のように作成します。
#configureセンターアドレスspring.cloud.config.uri = http:// localhost:9000
構成センターのアドレスを指定します。上記の例では、構成センターのポートの9000を指定します。 Application.Propertiesファイルでアプリケーションの名前を構成します。
spring.application.name = eg-config
アプリケーションは「EG-Config」と呼ばれます。プロジェクトが開始されると、EG-configファイルは構成センターからロードされます。次に、例を作成し、構成センターから豆と注入値を作成しましょう
@componentpublic class mybean {@value( "$ {my.name}")private string name; public string getName(){return name; } public void setName(string name){this.name = name; }}名前は、構成センターからプロパティをロードしますmy.name。スタートアップクラスは次のとおりです。
@SpringBootApplicationPublic class Application {public static void main(string [] args){configureableapplicationContext applicationContext = springApplication.run(application.class、args); //豆を取得して名前を印刷しますmybean bean = applicationContext.getBean(mybean.class); System.out.println(bean.getName()); }}起動後、コンソール印刷の結果はtestのとおりです。
これは、構成センターで導入されます。詳細については、プロジェクトの例を参照してください:https://github.com/liubo-tech/spring-cloud-config。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。