2.1、簡介
在分布式系統(tǒng)中觉至,由于服務(wù)數(shù)量巨多,為了方便服務(wù)配置文件統(tǒng)一管理睡腿,實時更新语御,所以需要分布式配置中心組件。在Spring Cloud中席怪,有分布式配置中心組件Spring Cloud Config 应闯,它支持配置服務(wù)放在配置服務(wù)的內(nèi)存中,也支持放在遠程Git倉庫中挂捻。在Spring Cloud Config 組件中碉纺,分兩個角色,一是config server细层,二是config client惜辑。
2.2、構(gòu)建Config Server
創(chuàng)建一個叫congfig-server的Spring Boot項目疫赎,引入相關(guān)maven包盛撑。
<?xml version="1.0" encoding="UTF-8"?>
<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.voyer</groupId>
<artifactId>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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>aliyun-maven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
在pom.xml中增加阿里云maven庫,可以提高下載速度捧搞。
<repository>
<id>aliyun-maven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
在默認的啟動程序上增加congfigservier注解@EnableConfigServer
@SpringBootApplication
@EnableConfigServer
@RestController
public class ConfigServerApplication {
@RequestMapping("/")
public String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
接下來要在配置文件里配置一下git相關(guān)信息
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.default-application-name=config-server
# 配置git倉庫地址
spring.cloud.config.server.git.uri=https://gitee.com/voyer/config-repo
# 配置倉庫路徑抵卫,多個路徑用逗號分隔
#spring.cloud.config.server.git.search-paths=aliyun
# 配置倉庫的分支
spring.cloud.config.label=master
# 如果Git倉庫為公開倉庫,可以不填寫用戶名和密碼胎撇,如果是私有倉庫需要填寫
# 訪問git倉庫的用戶名
#spring.cloud.config.server.git.username=xxxxoooo
# 訪問git倉庫的用戶密碼
#spring.cloud.config.server.git.password=xxxxoooo
當然介粘,config也支持配置多個Repositories,也可以通過匹配相關(guān)格式來獲取配置信息晚树,多個匹配格式用逗號分隔姻采,例如:(可以參考官方文檔)。
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
repos:
simple: https://github.com/simple/config-repo
special:
pattern: special*/dev*,*special*/dev*
uri: https://github.com/special/config-repo
local:
pattern: local*
uri: file:/home/configsvc/config-repo
遠程倉庫中有幾個文件
啟動程序訪問http://localhost:8080/test/dev
返回結(jié)果:
{"name":"test","profiles":["dev"],"label":null,"version":"74cdd22f87198215007e8edf138eb59954b66778","state":null,"propertySources":[]}
證明config server可以從遠程git倉庫獲取配置信息爵憎。
http請求地址和資源文件映射關(guān)系如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
2.3慨亲、構(gòu)建一個config client
創(chuàng)建一個叫congfig-client的Spring Boot項目,引入相關(guān)maven包宝鼓。
<?xml version="1.0" encoding="UTF-8"?>
<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.voyer</groupId>
<artifactId>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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M9</spring-cloud.version>
</properties>
<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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
注意此處需要引入spring-boot-starter-web
刑棵,否則會無法啟動(但是可以編譯通過),接下來要引入配置文件application.properties
spring.application.name=config-client
#指明遠程倉庫的分支
spring.cloud.config.label=master
#配置文件環(huán)境
spring.cloud.config.profile=dev
# 指明配置服務(wù)中心的網(wǎng)址愚铡。
spring.cloud.config.uri=http://localhost:8888/
server.port=8889
# 總結(jié)一下,config-client項目啟動報錯,獲取不到配置文件里的值:
# 1蛉签、首先確定項目config-client與config-service的spring-boot,spring-cloud的依賴版本要一致
# 2、配置文件命名,要遵循一定規(guī)則,如果項目config-client里命名為config-client,那配置文件就應(yīng)該是config-client-**.properties
在程序啟動入口增加獲取config server配置信息的代碼:
@SpringBootApplication
@RestController
public class ConfigClientApplication {
@Value("${test}")
String test;
@RequestMapping(value = "hi")
public String hi(){
return test;
}
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
注意此處的spring.application.name=config-client
,當啟動程序時碍舍,會看到控制臺打印的日志:
2018-04-04 16:21:14.904 INFO 32036 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888
2018-04-04 16:21:15.795 INFO 32036 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=master, version=74cdd22f87198215007e8edf138eb59954b66778, state=null
2018-04-04 16:21:15.796 INFO 32036 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/voyer/config-repo/config-client-dev.yml'}, MapPropertySource {name='https://gitee.com/voyer/config-repo/config-client.yml'}]}
可以看到加載了兩個配置文件https://gitee.com/voyer/config-repo/config-client-dev.yml
柠座、https://gitee.com/voyer/config-repo/config-client.yml
,訪問http://localhost:8889/hi,會看到返回信息:
bbb
唔乒验,此處筆者遠程git倉庫的test值為bbb愚隧,各位讀者可以自己定義。
這就說明锻全,config-client從config-server獲取了test的屬性狂塘,而config-server是從git倉庫讀取的。