javascript
Spring Cloud教程– Spring Cloud Config Server简介
問題
SpringBoot在通過屬性或YAML文件外部化配置屬性方面提供了很大的靈活性。 我們還可以使用特定于配置文件的配置文件(例如application.properties , application-dev.properties , application-prod.properties等)分別為每個環境(dev,qa,prod等)配置屬性。但是一旦應用程序啟動,我們就無法在運行時更新屬性。 如果更改屬性,則需要重新啟動應用程序以使用更新的配置屬性。
另外,在大量基于MicroService的應用程序的上下文中,我們希望能夠從一個集中的位置配置和管理所有MicroServices的配置屬性。
解
我們可以使用Spring Cloud Config Server ( http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_config )集中所有應用程序配置,并使用應用程序中的Spring Cloud Config Client模塊來使用配置配置服務器中的屬性。 我們還可以在運行時更新配置屬性,而無需重新啟動應用程序。
即使您不打算將應用程序部署在任何云平臺(例如AWS,Pivotal CloudFoundry等)中,許多Spring Cloud模塊也可以在SpringBoot應用程序中使用。
Spring Cloud Config服務器
Spring Cloud Config Server只是具有配置的配置屬性源的SpringBoot應用程序。 配置源可以是git存儲庫, svn存儲庫或Consul服務( https://www.consul.io/ )。
在本文中,我們將使用git存儲庫作為配置屬性源。
Git配置庫
創建一個git存儲庫來存儲屬性文件。 我已經在GitHub中創建了一個倉庫config-repo ,即https://github.com/sivaprasadreddy/config-repo.git 。
假設我們將開發兩個SpringBoot應用程序catalog-service和order-service 。 讓我們分別為目錄服務和訂單服務創建配置文件catalogservice.properties和orderservice.properties 。
config-repo / catalogservice.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/catalog spring.datasource.username=root spring.datasource.password=adminconfig-repo / orderservice.properties
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest我們還可以創建特定于配置文件的配置文件,例如catalogservice-dev.properties , catalogservice-prod.properties , orderservice-dev.properties , orderservice-prod.properties 。
config-repo / catalogservice-prod.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://appsrv1:3306/catalog spring.datasource.username=appuser46 spring.datasource.password=T(iV&#)X84@1!config-repo / orderservice-prod.properties
spring.rabbitmq.host=srv245.ind.com spring.rabbitmq.port=5672 spring.rabbitmq.username=admin23 spring.rabbitmq.password=uY7&%we@1!現在,將所有配置屬性文件提交到config-repo git存儲庫中。
Spring Cloud Config Server應用程序
讓我們通過選擇啟動器Config Server和Actuator從http://start.spring.io或從您最喜歡的IDE創建SpringBoot應用程序spring-cloud-config-server 。
這將使用以下pom.xml生成maven項目。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.sivalabs</groupId><artifactId>spring-cloud-config-server</artifactId><version>1.0.0-SNAPSHOT</version><packaging>jar</packaging><name>spring-cloud-config-server</name><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.6.RELEASE</version><relativePath/></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Dalston.SR2</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> </project>為了使我們的SpringBoot應用程序成為SpringCloud Config Server,我們只需要在主入口點類中添加@EnableConfigServer批注并配置指向git存儲庫的spring.cloud.config.server.git.uri屬性。
package com.sivalabs.configserver;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;@EnableConfigServer @SpringBootApplication public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);} }spring-cloud-config-server / src / main / resources / application.properties
server.port=8888 spring.cloud.config.server.git.uri=https://github.com/sivaprasadreddy/config-repo.git management.security.enabled=false除了配置git repo uri外,我們還將server.port配置為8888并禁用了執行器安全性 。 現在,您可以啟動將在端口8888上啟動的應用程序。
Spring Cloud Config Server公開以下REST端點以獲取特定于應用程序的配置屬性:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties這里{application}指的是spring.config.name屬性的值, {profile}是活動的概要文件, {label}是可選的git標簽(默認為“ master”)。
現在,如果您訪問URL http:// localhost:8888 / catalogservice / default,那么您將獲得以下響應以及catalogservice 默認配置詳細信息:
{"name": "catalogservice","profiles": ["default"],"label": null,"version": "8a06f25aeb3f28a8f06b5634eae01858b2c6465d","state": null,"propertySources": [{"name": "https://github.com/sivaprasadreddy/config-repo.git/catalogservice.properties","source": {"spring.datasource.username": "root","spring.datasource.driver-class-name": "com.mysql.jdbc.Driver","spring.datasource.password": "admin","spring.datasource.url": "jdbc:mysql://localhost:3306/catalog"}}] }如果訪問URL http:// localhost:8888 / catalogservice / prod,則將收到以下響應,其中包含catalogservice prod配置詳細信息。
{"name": "catalogservice","profiles": ["prod"],"label": null,"version": "8a06f25aeb3f28a8f06b5634eae01858b2c6465d","state": null,"propertySources": [{"name": "https://github.com/sivaprasadreddy/config-repo.git/catalogservice-prod.properties","source": {"spring.datasource.username": "appuser46","spring.datasource.driver-class-name": "com.mysql.jdbc.Driver","spring.datasource.password": "T(iV&#)X84@1!","spring.datasource.url": "jdbc:mysql://appsrv1:3306/catalog"}},{"name": "https://github.com/sivaprasadreddy/config-repo.git/catalogservice.properties","source": {"spring.datasource.username": "root","spring.datasource.driver-class-name": "com.mysql.jdbc.Driver","spring.datasource.password": "admin","spring.datasource.url": "jdbc:mysql://localhost:3306/catalog"}}] }除了特定于應用程序的配置文件(如catalogservice.properties , orderservice.properties)之外 ,您還可以創建application.properties文件以包含所有應用程序的通用配置屬性。 如您所料,您可以擁有特定于配置文件的文件,例如application-dev.properties,application-prod.properties 。
假設您在config-repo中有具有以下屬性的application.properties文件:
message=helloworld jdbc.datasource.url=jdbc:mysql://localhost:3306/defapp現在,如果您訪問http:// localhost:8888 / catalogservice / prod,則將收到以下響應:
{"name": "catalogservice","profiles": ["prod"],"label": null,"version": "8a06f25aeb3f28a8f06b5634eae01858b2c6465d","state": null,"propertySources": [{"name": "https://github.com/sivaprasadreddy/config-repo.git/catalogservice-prod.properties","source": {"spring.datasource.username": "appuser46","spring.datasource.driver-class-name": "com.mysql.jdbc.Driver","spring.datasource.password": "T(iV&#)X84@1!","spring.datasource.url": "jdbc:mysql://appsrv1:3306/catalog"}},{"name": "https://github.com/sivaprasadreddy/config-repo.git/catalogservice.properties","source": {"spring.datasource.username": "root","spring.datasource.driver-class-name": "com.mysql.jdbc.Driver","spring.datasource.password": "admin","spring.datasource.url": "jdbc:mysql://localhost:3306/catalog"}},{"name": "https://github.com/sivaprasadreddy/config-repo.git/application.properties","source": {"message": "helloworld","jdbc.datasource.url": "jdbc:mysql://localhost:3306/defapp"}}] }同樣,您可以訪問http:// localhost:8888 / orderservice / default以獲取orderservice配置詳細信息。
{"name": "orderservice","profiles": ["default"],"label": null,"version": "8a06f25aeb3f28a8f06b5634eae01858b2c6465d","state": null,"propertySources": [{"name": "https://github.com/sivaprasadreddy/config-repo.git/orderservice.properties","source": {"spring.rabbitmq.host": "localhost""spring.rabbitmq.port": "5672""spring.rabbitmq.username": "guest""spring.rabbitmq.password": "guest"}},{"name": "https://github.com/sivaprasadreddy/config-repo.git/application.properties","source": {"message": "helloworld","jdbc.datasource.url": "jdbc:mysql://localhost:3306/defapp"}}] }現在,我們已經了解了如何使用Spring Cloud Config Server創建配置服務器,以及如何使用REST API獲取應用程序特定的配置屬性。
讓我們看看如何創建SpringBoot應用程序并使用Config Server中的配置屬性,而不是將其放入應用程序中。
Spring Cloud Config客戶端(目錄服務)
使用Config Client, Web和Actuator啟動器創建SpringBoot應用程序目錄服務 。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.sivalabs</groupId><artifactId>catalog-service</artifactId><version>1.0.0-SNAPSHOT</version><packaging>jar</packaging><name>spring-cloud-config-client</name><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.6.RELEASE</version><relativePath/></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Dalston.SR2</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>通常在SpringBoot應用程序中,我們在application.properties中配置屬性。 但是,在使用Spring Cloud Config Server時,我們使用bootstrap.properties或bootstrap.yml文件配置Config Server的URL,并且Spring Cloud Config Client模塊將通過從Config Server中獲取應用程序屬性來啟動應用程序。
在src / main / resources / bootstrap.properties中配置以下屬性:
server.port=8181 spring.application.name=catalogservice spring.cloud.config.uri=http://localhost:8888 management.security.enabled=false我們已經使用spring.cloud.config.uri屬性配置了配置服務器的URL。 我們還使用spring.application.name屬性指定了應用程序名稱。
請注意, spring.application.name屬性的值應與config-repo中的基本文件名(catalogservice)匹配。
現在運行以下目錄服務主入口點類:
package com.sivalabs.catalogservice;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class CatalogServiceApplication {public static void main(String[] args) {SpringApplication.run(CatalogServiceApplication.class, args);} }我們可以訪問執行器端點http:// localhost:8181 / env以查看所有配置屬性。
{"profiles": [],"server.ports": {"local.server.port": 8080},"configService:configClient": {"config.client.version": "8a06f25aeb3f28a8f06b5634eae01858b2c6465d"},"configService:https://github.com/sivaprasadreddy/config-repo.git/catalogservice.properties": {"spring.datasource.username": "root","spring.datasource.driver-class-name": "com.mysql.jdbc.Driver","spring.datasource.password": "******","spring.datasource.url": "jdbc:mysql://localhost:3306/catalog"},"configService:https://github.com/sivaprasadreddy/config-repo.git/application.properties": {"message": "helloworld","jdbc.datasource.url": "jdbc:mysql://localhost:3306/defapp"},"servletContextInitParams": {},"systemProperties": {......},"systemEnvironment": {......},"springCloudClientHostInfo": {"spring.cloud.client.hostname": "192.168.0.101","spring.cloud.client.ipAddress": "192.168.0.101"},"applicationConfig: [classpath:/bootstrap.properties]": {"management.security.enabled": "false","spring.cloud.config.uri": "http://localhost:8888","spring.application.name": "catalogservice"},"defaultProperties": {} }您可以看到目錄服務應用程序在引導期間從Config Server獲取目錄服務屬性。 如果在應用程序內部定義了這些屬性,則可以使用@Value或@EnableConfigurationProperties綁定這些屬性。
屬性優先
現在我們知道有很多方法可以在許多文件中提供配置屬性,例如application src / main / resources和{application-name}-{profile} .properties中的 application.properties,bootstrap.properties及其配置文件變體, config-repo中的{profile} .properties 。
下圖顯示了來自各個屬性位置的配置屬性的優先級。
在運行時刷新屬性
讓我們看看如何在運行時更新目錄服務的配置屬性而無需重新啟動應用程序。
在config-repo git存儲庫中更新catalogservice.properties并提交更改。 現在,如果您訪問http:// localhost:8181 / env,您仍然會看到舊的屬性。
為了重新加載配置屬性,我們需要執行以下操作:
- 使用@RefreshScope標記要在配置更改時重新加載的Spring bean
- 使用POST方法發出http:// localhost:8181 / refresh請求
為了測試重載行為,讓我們在config-repo / catalogservice.properties中添加屬性name = Siva并提交。
創建一個簡單的RestController來顯示名稱值,如下所示:
@RestController @RefreshScope class HomeController {@Value("${name}")String name;@GetMapping("/name")public String name(){return name;} }現在訪問http:// localhost:8181 / name,它將顯示Siva 。 現在,在config-repo / catalogservice.properties中將屬性值更改為name = Prasad并提交。
為了重新加載配置更改,請使用POST方法觸發http:// localhost:8181 / refresh請求,然后再次訪問應顯示Prasad的 http:// localhost:8181 / name。
但是在大量應用程序和同一應用程序的多個實例的情況下,手動發出/刷新請求是乏味且不切實際的。 在下一篇文章中,我們將介紹如何使用Spring Cloud Bus處理此問題。
Spring Cloud教程–使用Spring Cloud Bus自動刷新配置( http://sivalabs.in/2017/08/spring-cloud-tutorials-auto-refresh-config-changes-using-spring-cloud-bus/ )。
本文的源代碼位于https://github.com/sivaprasadreddy/spring-cloud-tutorial
翻譯自: https://www.javacodegeeks.com/2017/08/spring-cloud-tutorials-introduction-spring-cloud-config-server.html
總結
以上是生活随笔為你收集整理的Spring Cloud教程– Spring Cloud Config Server简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 特斯拉计划今年从印度采购17亿至19亿美
- 下一篇: spring不自动下载_Spring:自