2.1、简介
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件Spring Cloud Config ,它支持配置服务放在配置服务的内存中,也支持放在远程Git仓库中。在Spring Cloud Config 组件中,分两个角色,一是config server,二是config client。
2.2、构建Config Server
创建一个叫congfig-server的Spring Boot项目,引入相关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相关信息
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,也可以通过匹配相关格式来获取配置信息,多个匹配格式用逗号分隔,例如:(可以参考官方文档)。
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
返回结果:
{"name":"test","profiles":["dev"],"label":null,"version":"74cdd22f87198215007e8edf138eb59954b66778","state":null,"propertySources":[]}
证明config server可以从远程git仓库获取配置信息。
http请求地址和资源文件映射关系如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
2.3、构建一个config client
创建一个叫congfig-client的Spring Boot项目,引入相关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
#配置文件环境
spring.cloud.config.profile=dev
# 指明配置服务中心的网址。
spring.cloud.config.uri=http://localhost:8888/
server.port=8889
# 总结一下,config-client项目启动报错,获取不到配置文件里的值:
# 1、首先确定项目config-client与config-service的spring-boot,spring-cloud的依赖版本要一致
# 2、配置文件命名,要遵循一定规则,如果项目config-client里命名为config-client,那配置文件就应该是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仓库读取的。
网友评论