欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring cloud config 配置中心快速實現(xiàn)過程解析

 更新時間:2019年08月24日 09:24:23   作者:云天  
這篇文章主要介紹了spring cloud config 配置中心快速實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

spring-cloud-config 配置中心實現(xiàn)

Spring Cloud Config 用于為分布式系統(tǒng)中的基礎(chǔ)設(shè)施和微服務(wù)應(yīng)用提供集中化的外部配置支持,分為server端和client端。

server端為分布式配置中心,是一個獨立的微服務(wù)應(yīng)用;client端為分布式系統(tǒng)中的基礎(chǔ)設(shè)置或微服務(wù)應(yīng)用,通過指定配置中心來管理相關(guān)的配置。

Spring Cloud Config 構(gòu)建的配置中心,除了適用于 Spring 構(gòu)建的應(yīng)用外,也可以在任何其他語言構(gòu)建的應(yīng)用中使用。
Spring Cloud Config 默認(rèn)采用 Git 存儲配置信息,支持對配置信息的版本管理。

本示例主要內(nèi)容

  • 配置中心演示client端和server端實現(xiàn)
  • 配置文件放在git(因github有時候不太穩(wěn)定,我放到了國內(nèi)服務(wù)器)
  • 版本切換(test、pro、dev)

Spring Cloud Config 特點

  • 提供server端和client端支持(Spring Cloud Config Server和Spring Cloud Config Client);
  • 集中式管理分布式環(huán)境下的應(yīng)用配置;
  • 基于Spring環(huán)境,實現(xiàn)了與Spring應(yīng)用無縫集成;
  • 可用于任何語言開發(fā)的程序;
  • 默認(rèn)實現(xiàn)基于Git倉庫(也支持SVN),從而可以進(jìn)行配置的版本管理;同時也支持配置從本地文件或數(shù)據(jù)庫讀取。

代碼構(gòu)建

server端實現(xiàn)

1.pom.xml添加maven依賴

 <dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-config-server</artifactId>
  </dependency>
 </dependencies>

2.application.yml配置

server:
 port: 8001
spring:
 application:
 name: cloud-config-server
 cloud:
 config:
  server:
  git:
   uri: https://gitee.com/tqlin/spring-boot-demo.git #因為github有時候不穩(wěn)定,我這里改到了碼云倉
   searchPaths: /cloud-config/config-repo/   #配置文件目錄
   force-pull: true

3.CloudConfigServerApplication.java啟動類

@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication {

 public static void main(String[] args) {
  SpringApplication.run(CloudConfigServerApplication.class, args);
 }
}

client端實現(xiàn)

1.pom.xml添加maven依賴

 <dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</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>

2.bootstrap.properties配置文件

spring.cloud.config.name=easy-config
spring.cloud.config.profile=test
spring.cloud.config.uri=http://localhost:8001/
spring.cloud.config.label=master
  • spring.application.name:對應(yīng){application}部分
  • spring.cloud.config.profile:對應(yīng){profile}部分
  • spring.cloud.config.label:對應(yīng)git的分支。如果配置中心使用的是本地存儲,則該參數(shù)無用
  • spring.cloud.config.uri:配置中心的具體地址(sever端地址)
  • spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于擴展為高可用配置集群。

特別注意:Spring Cloud 構(gòu)建于 Spring Boot 之上,在 Spring Boot 中有兩種上下文,一種是 bootstrap, 另外一種是 application, bootstrap 是應(yīng)用程序的父上下文,也就是說 bootstrap 加載優(yōu)先于 applicaton。bootstrap 主要用于從額外的資源來加載配置信息,還可以在本地外部配置文件中解密屬性。

這兩個上下文共用一個環(huán)境,它是任何Spring應(yīng)用程序的外部屬性的來源。bootstrap 里面的屬性會優(yōu)先加載,它們默認(rèn)也不能被本地相同配置覆蓋。

3.application.properties配置文件

spring.application.name=cloud-config-client
server.port=8002

運行示例

1.首先在碼云上面創(chuàng)建一個文件夾config-repo用來存放配置文件,我們創(chuàng)建以下三個配置文件:

 // 開發(fā)環(huán)境
 easy-config-dev.properties 內(nèi)容為:easy.hello=dev config
 // 測試環(huán)境
 easy-config-test.properties 內(nèi)容為:easy.hello=test config
 // 生產(chǎn)環(huán)境
 easy-config-pro.properties 內(nèi)容為:easy.hello=pro config

根據(jù)上面構(gòu)建的代碼指定的項目地址為:https://gitee.com/tqlin/spring-boot-demo.git 目錄為: /cloud-config/config-repo/

2.分別運行server端和client端

找到CloudConfigServerApplication.java、CloudConfigClientApplication.java分別運行

3.測試server端

直接訪問:http://localhost:8001/easy-config/dev

我們看到成功返回了開發(fā)配置文件信息

{
name: "easy-config",
profiles: [
"dev"
],
label: null,
version: "6053b4c1c2343ac27e822b2a9b60c6343be72f96",
state: null,
propertySources: [
{
name: "https://gitee.com/tqlin/spring-boot-demo.git/cloud-config/config-repo/easy-config-dev.properties",
source: {
easy.hello: "dev config"
}
}
]
}

訪問:http://localhost:8001/easy-config/test、http://localhost:8001/easy-config/pro,相應(yīng)的會返回測試及正式環(huán)境的配置

倉庫中的配置文件會被轉(zhuǎn)換成web接口,訪問可以參照以下的規(guī)則:

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

以easy-config-dev.properties為例子,它的application是easy-config,profile是dev。client會根據(jù)填寫的參數(shù)來選擇讀取對應(yīng)的配置。

4.測試client端

訪問:http://localhost:8002/hello 我們發(fā)現(xiàn)界面成功返回了 test config,說明測試配置文件client端讀取成功了

我們修改bootstrap.properties配置的spring.cloud.config.profile的值為dev,重啟client端,訪問:http://localhost:8002/hello 這時候界面返回 dev config,表示開發(fā)配置訪問成功。

資料

Spring Cloud Config 示例源碼

官網(wǎng)文檔

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • struts2入門介紹及代碼實例

    struts2入門介紹及代碼實例

    這篇文章主要介紹了struts2入門介紹及代碼實例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • 如何解決springmvc文件下載,內(nèi)容損壞的問題

    如何解決springmvc文件下載,內(nèi)容損壞的問題

    這篇文章主要介紹了解決springmvc文件下載,內(nèi)容損壞的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java transient 關(guān)鍵字是干啥的

    Java transient 關(guān)鍵字是干啥的

    這篇文章主要介紹了Java transient 關(guān)鍵字是干啥的,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • java實現(xiàn)簡單計算器功能

    java實現(xiàn)簡單計算器功能

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)簡單計算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果

    Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果

    今天小編就為大家分享一篇關(guān)于Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)

    Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)

    這篇文章主要介紹了Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 解決@Scheduled定時器使用@Thransactional事物問題

    解決@Scheduled定時器使用@Thransactional事物問題

    這篇文章主要介紹了解決@Scheduled定時器使用@Thransactional事物問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 完美解決Logback configuration error detected的問題

    完美解決Logback configuration error detected的問題

    這篇文章主要介紹了完美解決Logback configuration error detected的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • JAVA實現(xiàn)異步調(diào)用實例代碼

    JAVA實現(xiàn)異步調(diào)用實例代碼

    在java平臺,實現(xiàn)異步調(diào)用的角色主要三種角色:調(diào)用者、取貨憑證、真實數(shù)據(jù)。本篇文章給大家介紹java實現(xiàn)異步調(diào)用實例代碼,需要的朋友可以參考下
    2015-09-09
  • 獲取Spring當(dāng)前配置的兩種方式

    獲取Spring當(dāng)前配置的兩種方式

    這篇文章主要給大家介紹了獲取Spring當(dāng)前配置的,兩種方式文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01

最新評論