springcloud config配置中心初級篇

2022-06-25 22:36:14 字數 4189 閱讀 7524

本篇文章將介紹什麼是配置中心;配置中心使用git遠端倉庫實現的基本方式;使用bus自動重新整理配置等;

由於微服務的應用過多,每個模組都有乙個配置檔案,當業務變多的時候,在配置檔案中讀取或者修改自定義的屬性就非常不方便,springcloud官方推出了config配置中心,用於統一管理配置檔案;配置中心實現的大體流場如下:

客戶端從服務端獲取配置檔案,通常服務端需要負載均衡和高可用;

服務端從遠端倉庫獲取配置檔案

org.springframework.cloud

spring-cloud-config-server

org.springframework.boot

spring-boot-starter-web

git伺服器的配置如下;其中的search-paths目錄可以配置多個,使用逗號隔開;

spring:

cloud:

config:

server:

git:

# git 伺服器位址

uri:

# git倉庫 賬號

username: zszxz

# git倉庫 密碼

password:

# 搜尋zszxz-config目錄下的所有配置

search-paths: zszxz-config

name: zszxz-config # 應用名稱

server:

port: 9000 # 暴露埠

其中@enableconfigserver表示開啟config配置

/**

* @author lsc

* springloud server 配置

*/@enableconfigserver

public static void main(string args)

}

應用名稱盡量與配置檔案名稱一致;

在伺服器上建立倉庫spring-cloud-config

在倉庫下建立資料夾zszxz-config

在zszxz-config資料夾下建立檔案zszxz-config-dev.yml, 內容為 zszxz.springcloud.config: from dev

在zszxz-config資料夾下建立檔案zszxz-config-pro.yml, 內容為 zszxz.springcloud.config: from pro

在zszxz-config資料夾下建立檔案zszxz-config-test.yml, 內容為 zszxz.springcloud.config: from test

啟動專案後可以按照如下方式訪問配置資訊,當然控制台視窗也有列印下面資訊;

瀏覽器訪問位址 http://localhost:9000/zszxz-config/dev/master

內容如下

}]}

搭建client的目的就是從server端獲取想要的配置檔案

org.springframework.cloud

spring-cloud-config-client

org.springframework.boot

spring-boot-starter-web

bootstrap.yml 配置如下

# cloud屬性相關配置

spring:

cloud:

config:

# 遠端分支

label: master

# server位址

# 請求的遠端檔名稱

name: zszxz-config

# 啟用的配置檔案

profile: dev

server:

port: 9001

spring:

name: config-client

controller定義乙個欄位value,採用 @value註解獲取從server端傳送過來的配置內容;使用瀏覽器的方式更方便於測試;

@restcontroller

public class configcontroller ")

private string value;

public string getconfig()

}

public static void main(string args)

}啟動工程後訪問http://localhost:9001/zszxz/config 會輸出from dev

以上的內容學習完就已經可以使用springcloud的配置中心功能,但缺點是每次在git遠端倉庫修改配置檔案都需要重啟應用,這很不方便,官方推出了使用訊息中介軟體的方式進行自動重新整理配置,相當於專案中的熱部署;以下是對上面的工程的乙個改造,使用的是actuator的方式。

修改了git遠端倉庫配置檔案內容

server端會收到乙個倉庫變動內容,通過訊息匯流排bus傳送給客戶端;

客戶端接收到訊息後會向服務端拉取最新配置內容

在 原有的工程的client端和server端都加上如下配置

org.springframework.boot

spring-boot-starter-actuator

服務端新增訊息佇列依賴

org.springframework.cloud

spring-cloud-starter-bus-amqp

在client暴露端點;

management:

endpoint:

shutdown:

enabled: false

endpoints:

web:

exposure:

include: "*"

在server端需要連線rabbitmq,故在原有即配置上加上rabbitmq配置即可

spring:

cloud:

config:

server:

git:

# git 伺服器位址

uri:

# git倉庫 賬號

username:

# git倉庫 密碼

password:

# 搜尋zszxz-config目錄下的所有配置

search-paths: zszxz-config

name: zszxz-config # 應用名稱

## 配置rabbitmq 資訊

rabbitmq:

host: 127.0.0.1

port: 5672

username: zszxz

password:

server:

port: 9000 # 暴露埠

在client端需要加上@refreshscope註解用於端點重新整理

@restcontroller

@refreshscope

public class configcontroller ")

private string value;

public string getconfig()

}

首先先修改git遠端倉庫配置檔案內容,比如from dev 修改 為 from dev the author

其次傳送請求post 請求至客戶端http://localhost:9001/actuator/refresh 會的到如下結果

[

"zszxz.springcloud.config",

"config.client.version"

]

最後重新訪問 localhost:9001/zszxz/config 會得到修改後的配置

from dev the author

SpringCloud config 配置中心

spring cloud config為服務端和客戶端提供了分布式系統的外部化配置支援。配置伺服器為各應用的所有環境提供了乙個中心化的外部配置。一般外部配置檔案存在github.com上。下面是config的 實現 新建配置中心模組 配置pom org.springframework.cloud s...

Spring Cloud Config 配置中心

比如使用 yml 或者 properties yml testconfig testvalueproperties testconfig testvalue伺服器配置檔案 呼叫config server url後返回的json資料 紅框中為配置正確後解析樣子 否則無法解析!在分布式系統中,由於服務數...

Spring Cloud Config 配置重新整理

客戶端進行重新整理操作。1 新增 actuator包,這樣 refresh url才處於可用狀態。org.springframework.boot spring boot starter actuator 2 手動測試時,關閉許可權認證,否則訪問時會出現無許可權。3 在controller上新增 r...