溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-08-26 08:54:18 來(lái)源:腳本之家 閱讀:154 作者:云天 欄目:編程語(yǔ)言

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

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

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

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

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

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

Spring Cloud Config 特點(diǎn)

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

代碼構(gòu)建

server端實(shí)現(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 #因?yàn)間ithub有時(shí)候不穩(wěn)定,我這里改到了碼云倉(cāng)
   searchPaths: /cloud-config/config-repo/   #配置文件目錄
   force-pull: true

3.CloudConfigServerApplication.java啟動(dòng)類

@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication {

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

client端實(shí)現(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:對(duì)應(yīng){application}部分
  • spring.cloud.config.profile:對(duì)應(yīng){profile}部分
  • spring.cloud.config.label:對(duì)應(yīng)git的分支。如果配置中心使用的是本地存儲(chǔ),則該參數(shù)無(wú)用
  • spring.cloud.config.uri:配置中心的具體地址(sever端地址)
  • spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于擴(kuò)展為高可用配置集群。

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

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

3.application.properties配置文件

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

運(yùn)行示例

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

 // 開發(fā)環(huán)境
 easy-config-dev.properties 內(nèi)容為:easy.hello=dev config
 // 測(cè)試環(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)建的代碼指定的項(xiàng)目地址為:https://gitee.com/tqlin/spring-boot-demo.git 目錄為: /cloud-config/config-repo/

2.分別運(yùn)行server端和client端

找到CloudConfigServerApplication.java、CloudConfigClientApplication.java分別運(yùn)行

3.測(cè)試server端

直接訪問(wèn):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"
}
}
]
}

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

倉(cāng)庫(kù)中的配置文件會(huì)被轉(zhuǎn)換成web接口,訪問(wèn)可以參照以下的規(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會(huì)根據(jù)填寫的參數(shù)來(lái)選擇讀取對(duì)應(yīng)的配置。

4.測(cè)試client端

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

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

資料

Spring Cloud Config 示例源碼

官網(wǎng)文檔

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI