第六篇: 分布式配置中心(Spring Cloud Config)
一、簡介
在分布式系統中,由於服務數量巨多,為了方便服務配置文件統一管理,實時更新,所以需要分布式配置中心組件。
在Spring Cloud中,有分布式配置中心組件spring cloud config ,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。
在spring cloud config 組件中,分兩個角色,一是config server,二是config client。
二、構建Config Server
跟前篇一樣,新建工程eureka-config-server。pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0"View Codexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sun</groupId> <artifactId>eureka-config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config-server</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.sun</groupId> <artifactId>springcloud-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>
在程序的入口Application類加上@EnableConfigServer註解開啟配置服務器的功能,代碼如下:
package com.sun; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }View Code
需要在程序的配置文件application.yml文件配置以下:
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/sunfengjiajia/SpringcloudConfig #配置git倉庫地址
searchPaths: respo #配置倉庫路徑
label: master #配置倉庫的分支
username:
password:
server:
port: 8888
View Code
如果Git倉庫為公開倉庫,可以不填寫用戶名和密碼,如果是私有倉庫需要填寫,本例子是公開倉庫,放心使用。
遠程倉庫https://gitee.com/sunfengjiajia/SpringcloudConfig 中有個文件config-client-dev.properties文件中有一個屬性:
foo = foo version 3
啟動程序:訪問http://localhost:8888/foo/dev,顯示:
{"name":"foo","profiles":["dev"],"label":null,"version":"eb06f7707a76ebaabba2ef930a8d0b463875daa5","state":null,"propertySources":[]}
證明配置服務中心可以從遠程程序獲取配置信息。
http請求地址和資源文件映射如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
三、構建一個config client
重新創建一個springboot項目,取名為eureka-config-client,其pom文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sun</groupId> <artifactId>eureka-config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.sun</groupId> <artifactId>springcloud-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> </project>View Code
其配置文件application.yml:
spring:
application:
name: config-client
cloud:
config:
label: master
profile: dev
uri: http://localhost:8888/
server:
port: 8881
View Code
- spring.cloud.config.label 指明遠程倉庫的分支
-
spring.cloud.config.profile
- dev開發環境配置文件
- test測試環境
- pro正式環境
-
spring.cloud.config.uri= http://localhost:8888/ 指明配置服務中心的網址。
程序的入口類,寫一個API接口“/hi”,返回從配置中心讀取的foo變量的值,代碼如下:
package com.sun; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${foo}") String foo; @RequestMapping(value = "/hi") public String hi(){ return foo; } }View Code
打開網址訪問:http://localhost:8881/hi,網頁顯示:
foo version 3
這就說明,config-client從config-server獲取了foo的屬性,而config-server是從git倉庫讀取的。
四、高可用的分布式配置中心(Spring Cloud Config)
1、改造eureka-server工程
前面講述了一個服務如何從配置中心讀取文件,配置中心如何從遠程git讀取配置文件,當服務實例很多時,都從配置中心讀取文件,這時可以考慮將配置中心做成一個微服務,將其集群化,從而達到高可用。
繼續前面的config-server工程,添加依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
此時的pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sun</groupId> <artifactId>eureka-config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config-server</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.sun</groupId> <artifactId>springcloud-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>View Code
在配置文件application.yml中需要添加服務中心基本配置,如下:
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
此時的application.yml如下:
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/sunfengjiajia/SpringcloudConfig #配置git倉庫地址
searchPaths: respo #配置倉庫路徑
label: master #配置倉庫的分支
username:
password:
server:
port: 8888
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
View Code
最後需要在程序的啟動類Application加上@EnableEurekaServer的註解。啟動類ConfigServerApplication:
package com.sun; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableConfigServer @EnableEurekaServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
2、改造config-client
將其註冊微到服務註冊中心,作為Eureka客戶端,需要pom文件加上起步依賴spring-cloud-starter-netflix-eureka-client,代碼如下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sun</groupId> <artifactId>eureka-config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.sun</groupId> <artifactId>springcloud-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>View Code
配置文件application.yml。加上服務註冊地址為http://localhost:8888/eureka/
application.yml如下:
spring:
application:
name: config-client
cloud:
config:
label: master
profile: dev
#uri: http://localhost:8888/
discovery:
enabled: true
serviceId: config-server
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8881
- spring.cloud.config.discovery.enabled 是從配置中心讀取文件。
- spring.cloud.config.discovery.serviceId 配置中心的servieId,即服務名。
這時發現,在讀取配置文件不再寫ip地址,而是服務名,這時如果配置服務部署多份,通過負載均衡,從而高可用。
給客戶端啟動類加上註解:@EnableEurekaClient
ConfigClientApplication啟動類如下:
package com.sun; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController @EnableEurekaClient public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${foo}") String foo; @RequestMapping(value = "/hi") public String hi(){ return foo; } }View Code
依次啟動config-server,config-client
訪問網址:http://localhost:8888/
訪問http://localhost:8881/hi,瀏覽器顯示:
foo version 3
第六篇: 分布式配置中心(Spring Cloud Config)