Spring Cloud Config是Spring Cloud團隊創建的一個全新項目,用來為分布式系統中的基礎設施和微服務應用提供集中化的外部配置支持,它分為服務端與客戶端兩個部分。其中服務端也稱為分布式配置中心,它是一個獨立的微服務應用,用來連接配置倉庫并為客戶端提供獲取配置信息、加密/解密信息等訪問接口;而客戶端則是微服務架構中的各個微服務應用或基礎設施,它們通過指定的配置中心來管理應用資源與業務相關的配置內容,并在啟動的時候從配置中心獲取和加載配置信息。Spring Cloud Config實現了對服務端和客戶端中環境變量和屬性配置的抽象映射,所以它除了適用于Spring構建的應用程序之外,也可以在任何其他語言運行的應用程序中使用。由于Spring Cloud Config實現的配置中心默認采用Git來存儲配置信息,所以使用Spring Cloud Config構建的配置服務器,天然就支持對微服務應用配置信息的版本管理,并且可以通過Git客戶端工具來方便的管理和訪問配置內容。當然它也提供了對其他存儲方式的支持,比如:SVN倉庫、本地化文件系統。
在本文中,我們將學習如何構建一個基于Git存儲的分布式配置中心,并對服務進行改造,并讓其能夠從配置中心獲取配置信息并綁定到代碼中的整個過程。
配置倉庫
在碼云或github上創建一個倉庫,我這里是在碼云創建的,地址為
http://git.oschina.net/swlfly/config-repo-demo/
新建一個配置文件client2.yml,將client-two的配置內容復制過來
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka #注冊服務器地址
instance:
hostname: client2
spring:
application:
name: client2
server:
port: 8081
創建服務
IDEA新建一個config服務,使用注解@EnableConfigServer開啟一個配置中心
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
配置文件如下
spring:
application:
name: config
cloud:
config:
server:
git:
uri: http://git.oschina.net/swlfly/config-repo-demo/
username: xxxxx # git用戶名
password: xxxx #git密碼
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
server:
port: 8082 #端口號
需要認證的時候username和password分別輸入git的用戶名和密碼,啟動服務,訪問localhost:8082/client-a.yml,返回的內容如下
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
hostname: client2
server:
port: 8081
spring:
application:
name: client2
從IDEA的控制臺輸出可以看到有多種Mapped,如
{name}-{profiles}.yml
{label}/{name}-{profiles}.yml
這里name代表服務名,我們這里就是client2,profiles代表環境如dev,test,prod,label代表分支,默認為master,如訪問http://localhost:8082/master/client2-a.yml在這里實際和訪問localhost:8082/client-a.yml返回的內容一樣,接下來我們在config-repo-demo創建一個client2-dev.yml和client2-test.yml來驗證profiles
client2-dev.yml內容如下:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka #注冊服務器地址
instance:
hostname: client2
spring:
application:
name: client2
server:
port: 8081
env: dev
client2-test.yml內容如下
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka #注冊服務器地址
instance:
hostname: client2
spring:
application:
name: client2
server:
port: 8081
env: test
訪問http://localhost:8082/master/client2-dev.yml返回的內容為
env: dev
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
hostname: client2
server:
port: 8081
spring:
application:
name: client2
訪問http://localhost:8082/master/client2-test.yml返回的內容為
env: test
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
hostname: client2
server:
port: 8081
spring:
application:
name: client2
在config-repo-demo創建一個分支來驗證label,創建分支release
修改分支release里的client2-dev內容如下
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka #注冊服務器地址
instance:
hostname: client2
spring:
application:
name: client2
server:
port: 8081
env: dev
label: release
訪問http://localhost:8082/release/client2-dev.yml返回如下內容
env: dev
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
hostname: client2
label: release
server:
port: 8081
spring:
application:
name: client2
源碼地址: https://gitee.com/swlfly/SpringCloud_Learn/tree/1.5
更多技術文章可關注個人公眾號: 碼農Fly