Server 的配置
1. 使用占位符灵活控制查询目录
先来看配置好的 config-server 和 config-client 的配置文件
config-sever 的 application.properties 文件
spring.application.name=config-server
server.port=8081
# 配置文件仓库地址
spring.cloud.config.server.git.search-paths=client1
spring.cloud.config.server.git.uri=https://github.com/xuanjinnan/configRepository.git
# 仓库中配置文件的目录
# 仓库的用户名密码
spring.cloud.config.server.git.username=xuanjinnan@163.com
spring.cloud.config.server.git.password=
config-client 的 bootstrap.properties 文件
# 下面三行配置分别对应 config-server 中的{application}、{profile}、{label}占位符
spring.application.name=client1
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8081
server.port=8082
原来是spring.cloud.config.server.git.search-paths=client1
,写死的,可以通过 {application}
占位符,表示连接上来的 client1 的 spring.application.name 属性的值,它是动态的。
spring.application.name=config-server
server.port=8081
# 配置文件仓库地址
spring.cloud.config.server.git.search-paths={application}
spring.cloud.config.server.git.uri=https://github.com/xuanjinnan/configRepository.git
# 仓库中配置文件的目录
# 仓库的用户名密码
spring.cloud.config.server.git.username=xuanjinnan@163.com
spring.cloud.config.server.git.password=
r然后重启 config-server 和 config-client 服务,访问http://localhost:8082/hello
依然正常
同样,可以用 {profile} 表示 client1 的 spring.cloud.config.profile 的属性,用 {label} 表示spring.cloud.config.label 的属性:
spring.cloud.config.profile={profile}
spring.cloud.config.label={label}
spring.cloud.config.server.git.search-paths={application}
再次重启 config-client 和 config-server ,访问http://localhost:8082/hello
接口也是正常的:
2.Config-server 配置文件本地获取
①classpath
通常情况下配置文件是从 Git 仓库获取的,但是也可以直接 classpath 获取
- 首先复制配置文件:
- 直接拷贝到 config-server 的 resources 目录下
- 然后配置 config-server 的 application.properties 文件,增加
spring.profiles.active=native
spring.application.name=config-server
server.port=8081
# 配置文件仓库地址
spring.cloud.config.profile={profile}
spring.cloud.config.label={label}
spring.cloud.config.server.git.search-paths={application}
spring.cloud.config.server.git.uri=https://github.com/xuanjinnan/configRepository.git
# 表示让 config-sever 从 classpath 下查找配置,而不是 Git 仓库中
spring.profiles.active=native
# 仓库中配置文件的目录
# 仓库的用户名密码
spring.cloud.config.server.git.username=xuanjinnan@163.com
spring.cloud.config.server.git.password=
- 简单修改下 client1 中 接口访问的配置文件,来做验证,值修改为 dev123456
- 重启 config-server 和 config-client 两个服务,访问
http://localhost:8082/hello
接口
接口的返回值就是刚才修改的内容,说明 config-server 从 classpath 获取配置文件成功。
②本地获取
也可以通过配置本地路径来获取配置文件,比如我电脑的F:\springcloudStudy\configRepository\client1
- 在刚刚的
spring.profiles.active=native
基础上,再添加spring.cloud.config.server.native.search-locations=file:/F:/springcloudStudy/configRepository/client1
的路径
spring.application.name=config-server
server.port=8081
# 配置文件仓库地址
spring.cloud.config.profile={profile}
spring.cloud.config.label={label}
spring.cloud.config.server.git.search-paths={application}
spring.cloud.config.server.git.uri=https://github.com/xuanjinnan/configRepository.git
# 表示让 config-sever 从 classpath 下查找配置,而不是 Git 仓库中
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=file:/F:/springcloudStudy/configRepository/client1
# 仓库中配置文件的目录
# 仓库的用户名密码
spring.cloud.config.server.git.username=xuanjinnan@163.com
spring.cloud.config.server.git.password=
-
简单修改下 client1 中 接口访问的配置文件,来做验证,值修改为 dev123456789
-
重启 config-server 和 config-client 两个服务,访问
http://localhost:8082/hello
接口
接口的返回值就是刚才修改的内容,说明 config-server 从本地 F:\springcloudStudy\configRepository\client1 获取配置文件成功。
总结
Spring Cloud Config 的配置文件可以从 Git 或者 classpath 或 本地获取,一般都是用 Git 配置,但是作为技术方案其他两种方法也可以