自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Cloud構(gòu)建微服務(wù)架構(gòu)(四)分布式配置中心

云計(jì)算 分布式
Spring Cloud Config為服務(wù)端和客戶端提供了分布式系統(tǒng)的外部化配置支持。配置服務(wù)器為各應(yīng)用的所有環(huán)境提供了一個(gè)中心化的外部配置。它實(shí)現(xiàn)了對(duì)服務(wù)端和客戶端對(duì)Spring Environment和PropertySource抽象的映射,所以它除了適用于Spring構(gòu)建的應(yīng)用程序,也可以在任何其他語(yǔ)言運(yùn)行的應(yīng)用程序中使用。

[[198438]]

Spring Cloud Config為服務(wù)端和客戶端提供了分布式系統(tǒng)的外部化配置支持。配置服務(wù)器為各應(yīng)用的所有環(huán)境提供了一個(gè)中心化的外部配置。它實(shí)現(xiàn)了對(duì)服務(wù)端和客戶端對(duì)Spring Environment和PropertySource抽象的映射,所以它除了適用于Spring構(gòu)建的應(yīng)用程序,也可以在任何其他語(yǔ)言運(yùn)行的應(yīng)用程序中使用。作為一個(gè)應(yīng)用可以通過(guò)部署管道來(lái)進(jìn)行測(cè)試或者投入生產(chǎn),我們可以分別為這些環(huán)境創(chuàng)建配置,并且在需要遷移環(huán)境的時(shí)候獲取對(duì)應(yīng)環(huán)境的配置來(lái)運(yùn)行。

配置服務(wù)器默認(rèn)采用git來(lái)存儲(chǔ)配置信息,這樣就有助于對(duì)環(huán)境配置進(jìn)行版本管理,并且可以通過(guò)git客戶端工具來(lái)方便的管理和訪問(wèn)配置內(nèi)容。當(dāng)然他也提供本地化文件系統(tǒng)的存儲(chǔ)方式,下面從這兩方面介紹如何使用分布式配置來(lái)存儲(chǔ)微服務(wù)應(yīng)用多環(huán)境的配置內(nèi)容。

構(gòu)建Config Server

通過(guò)Spring Cloud構(gòu)建一個(gè)Config Server,非常簡(jiǎn)單,只需要三步:

  • pom.xml中引入spring-cloud-config-server依賴,完整依賴配置如下:
  1. <parent> 
  2.     <groupId>org.springframework.boot</groupId> 
  3.     <artifactId>spring-boot-starter-parent</artifactId> 
  4.     <version>1.3.5.RELEASE</version> 
  5.     <relativePath/> <!-- lookup parent from repository --> 
  6. </parent> 
  7. <dependencies> 
  8.     <dependency> 
  9.         <groupId>org.springframework.boot</groupId> 
  10.         <artifactId>spring-boot-starter-test</artifactId> 
  11.         <scope>test</scope> 
  12.     </dependency> 
  13.     <dependency> 
  14.         <groupId>org.springframework.cloud</groupId> 
  15.         <artifactId>spring-cloud-config-server</artifactId> 
  16.     </dependency> 
  17. </dependencies> 
  18. <dependencyManagement> 
  19.     <dependencies> 
  20.         <dependency> 
  21.             <groupId>org.springframework.cloud</groupId> 
  22.             <artifactId>spring-cloud-dependencies</artifactId> 
  23.             <version>Brixton.RELEASE</version> 
  24.             <type>pom</type> 
  25.             <scope>import</scope> 
  26.         </dependency> 
  27.     </dependencies> 
  28. </dependencyManagement> 
  • 創(chuàng)建Spring Boot的程序主類,并添加@EnableConfigServer注解,開(kāi)啟Config Server
  1. @EnableConfigServer 
  2. @SpringBootApplication 
  3. public class Application { 
  4.     public static void main(String[] args) { 
  5.         new SpringApplicationBuilder(Application.class).web(true).run(args); 
  6.     } 
  • application.properties中配置服務(wù)信息以及git信息,例如:
  1. spring.application.name=config-server 
  2. server.port=7001 
  3. # git管理配置 
  4. spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/SpringBoot-Learning/ 
  5. spring.cloud.config.server.git.searchPaths=Chapter9-1-4/config-repo 
  6. spring.cloud.config.server.git.username=username 
  7. spring.cloud.config.server.git.password=password 
  • spring.cloud.config.server.git.uri:配置git倉(cāng)庫(kù)位置
  • spring.cloud.config.server.git.searchPaths:配置倉(cāng)庫(kù)路徑下的相對(duì)搜索位置,可以配置多個(gè)
  • spring.cloud.config.server.git.username:訪問(wèn)git倉(cāng)庫(kù)的用戶名
  • spring.cloud.config.server.git.password:訪問(wèn)git倉(cāng)庫(kù)的用戶密碼

到這里,使用一個(gè)通過(guò)Spring Cloud Config實(shí)現(xiàn),并使用git管理內(nèi)容的配置中心已經(jīng)完成了,啟動(dòng)該應(yīng)用,成功后開(kāi)始下面的內(nèi)容。

Spring Cloud Config也提供本地存儲(chǔ)配置的方式。我們只需要設(shè)置屬性spring.profiles.active=native,Config Server會(huì)默認(rèn)從應(yīng)用的src/main/resource目錄下檢索配置文件。也可以通過(guò)spring.cloud.config.server.native.searchLocations=file:F:/properties/屬性來(lái)指定配置文件的位置。雖然Spring Cloud Config提供了這樣的功能,但是為了支持更好的管理內(nèi)容和版本控制的功能,還是推薦使用git的方式。

服務(wù)端驗(yàn)證

為了驗(yàn)證上面完成的配置服務(wù)器,在http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/ 下創(chuàng)建了一個(gè)config-repo目錄作為配置倉(cāng)庫(kù),并根據(jù)不同環(huán)境新建了下面四個(gè)配置文件:

  • didispace.properties
  • didispace-dev.properties
  • didispace-test.properties
  • didispace-prod.properties

其中設(shè)置了一個(gè)from屬性,為每個(gè)配置文件分別設(shè)置了不同的值,如:

  • from=git-default-1.0
  • from=git-dev-1.0
  • from=git-test-1.0
  • from=git-prod-1.0

為了測(cè)試版本控制,在master中,我們都加入1.0的后綴,同時(shí)創(chuàng)建一個(gè)config-label-test分支,并將各配置文件中的值用2.0作為后綴。

完成了這些準(zhǔn)備工作之后,我們就可以通過(guò)瀏覽器或POSTMAN等工具直接來(lái)訪問(wèn)到我們的配置內(nèi)容了。

URL與配置文件的映射關(guān)系如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

上面的url會(huì)映射{application}-{profile}.properties對(duì)應(yīng)的配置文件,{label}對(duì)應(yīng)git上不同的分支,默認(rèn)為master。

我們可以嘗試構(gòu)造不同的url來(lái)訪問(wèn)不同的配置內(nèi)容,比如:要訪問(wèn)config-label-test分支,didispace應(yīng)用的prod環(huán)境,可以通過(guò)這個(gè)url:http://localhost:7001/didispace/prod/config-label-test

  1.   "name""didispace"
  2.   "profiles": [ 
  3.     "prod" 
  4.   ], 
  5.   "label""config-label-test"
  6.   "version""19de8a25575a7054a34230f74a22aa7f5575a9d1"
  7.   "propertySources": [ 
  8.     { 
  9.       "name""http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/config-repo/didispace-prod.properties"
  10.       "source": { 
  11.         "from""git-prod-2.0" 
  12.       } 
  13.     }, 
  14.     { 
  15.       "name""http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/config-repo/didispace.properties"
  16.       "source": { 
  17.         "from""git-default-2.0" 
  18.       } 
  19.     } 
  20.   ] 

微服務(wù)端映射配置

在完成并驗(yàn)證了配置服務(wù)中心之后,下面看看我們?nèi)绾卧谖⒎?wù)應(yīng)用中獲取配置信息。

  • 創(chuàng)建一個(gè)Spring Boot應(yīng)用,在pom.xml中引入spring-cloud-starter-config依賴,完整依賴關(guān)系如下:
  1. <parent> 
  2.     <groupId>org.springframework.boot</groupId> 
  3.     <artifactId>spring-boot-starter-parent</artifactId> 
  4.     <version>1.3.5.RELEASE</version> 
  5.     <relativePath/> <!-- lookup parent from repository --> 
  6. </parent> 
  7. <dependencies> 
  8.     <dependency> 
  9.         <groupId>org.springframework.boot</groupId> 
  10.         <artifactId>spring-boot-starter-test</artifactId> 
  11.         <scope>test</scope> 
  12.     </dependency> 
  13.     <dependency> 
  14.         <groupId>org.springframework.boot</groupId> 
  15.         <artifactId>spring-boot-starter-web</artifactId> 
  16.     </dependency> 
  17.     <dependency> 
  18.         <groupId>org.springframework.cloud</groupId> 
  19.         <artifactId>spring-cloud-starter-config</artifactId> 
  20.     </dependency> 
  21. </dependencies> 
  22. <dependencyManagement> 
  23.     <dependencies> 
  24.         <dependency> 
  25.             <groupId>org.springframework.cloud</groupId> 
  26.             <artifactId>spring-cloud-dependencies</artifactId> 
  27.             <version>Brixton.RELEASE</version> 
  28.             <type>pom</type> 
  29.             <scope>import</scope> 
  30.         </dependency> 
  31.     </dependencies> 
  32. </dependencyManagement> 
  • 創(chuàng)建最基本的Spring Boot啟動(dòng)主類
  1. @SpringBootApplication 
  2. public class Application { 
  3.     public static void main(String[] args) { 
  4.         new SpringApplicationBuilder(Application.class).web(true).run(args); 
  5.     } 
  • 創(chuàng)建bootstrap.properties配置,來(lái)指定config server,例如:
  1. spring.application.name=didispace 
  2. spring.cloud.config.profile=dev 
  3. spring.cloud.config.label=master 
  4. spring.cloud.config.uri=http://localhost:7001/ 
  5. server.port=7002 
  • spring.application.name:對(duì)應(yīng)前配置文件中的{application}部分
  • spring.cloud.config.profile:對(duì)應(yīng)前配置文件中的{profile}部分
  • spring.cloud.config.label:對(duì)應(yīng)前配置文件的git分支
  • spring.cloud.config.uri:配置中心的地址

這里需要格外注意:上面這些屬性必須配置在bootstrap.properties中,config部分內(nèi)容才能被正確加載。因?yàn)閏onfig的相關(guān)配置會(huì)先于application.properties,而bootstrap.properties的加載也是先于application.properties。

  • 創(chuàng)建一個(gè)Rest Api來(lái)返回配置中心的from屬性,具體如下:
  1. @RefreshScope 
  2. @RestController 
  3. class TestController { 
  4.     @Value("${from}"
  5.     private String from
  6.     @RequestMapping("/from"
  7.     public String from() { 
  8.         return this.from
  9.     } 

通過(guò)@Value("${from}")綁定配置服務(wù)中配置的from屬性。

啟動(dòng)該應(yīng)用,并訪問(wèn):http://localhost:7002/from ,我們就可以根據(jù)配置內(nèi)容輸出對(duì)應(yīng)環(huán)境的from內(nèi)容了。

完整示例:Chapter9-1-4

責(zé)任編輯:武曉燕 來(lái)源: 51CTO專欄
相關(guān)推薦

2018-05-23 15:58:27

Spring Clou微服務(wù)架構(gòu)

2018-07-19 14:58:14

Spring Clou微服務(wù)架構(gòu)

2018-03-02 16:11:29

Spring Clou分布式服務(wù)跟蹤

2018-04-09 13:56:13

微服務(wù)架構(gòu)分布式

2018-03-13 16:42:26

分布式服務(wù)跟蹤

2018-04-02 15:01:31

微服務(wù)架構(gòu)分布式服務(wù)

2018-04-16 14:56:56

微服務(wù)架構(gòu)分布式服務(wù)

2018-04-18 16:07:49

Spring Clou微服務(wù)分布式

2017-06-26 09:06:10

Spring Clou微服務(wù)架構(gòu)

2019-10-24 11:17:57

架構(gòu)運(yùn)維技術(shù)

2017-09-04 16:15:44

服務(wù)網(wǎng)關(guān)架構(gòu)

2017-09-09 23:15:20

Spring Clou微服務(wù)架構(gòu)路由

2023-08-25 16:26:49

微服務(wù)架構(gòu)

2023-09-12 22:58:51

分布式架構(gòu)微服務(wù)

2017-08-10 11:15:05

Spring Clou微服務(wù)架構(gòu)

2017-08-09 15:50:47

Spring Clou微服務(wù)架構(gòu)

2018-07-09 09:27:10

Spring Clou微服務(wù)架構(gòu)

2017-07-03 09:50:07

Spring Clou微服務(wù)架構(gòu)

2023-11-20 15:32:29

2023-05-29 14:07:00

Zuul網(wǎng)關(guān)系統(tǒng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)