学科分类
目录
Spring Cloud

Config Server从远程Git仓库读取配置文件

下面通过一个案例演示Config Server如何从Git仓库读取配置文件,具体步骤如下:

(1)我们在远程Git仓库创建一个名称为hello的公开仓库,然后在公开库中创建一个和config-client-dev.yml相同的文件。

(2)修改Config Server的配置文件application.yml。这里我们配置了服务名、端口号、远程Git仓库的地址、文件夹地址、用户名、密码、分支名等,具体如例1所示。

例1 config-server\src\main\resources\application.yml

 1     server:
 2       port: 8769
 3     spring :
 4       application:
 5          name : config-server
 6       cloud :
 7         config :
 8           server:
 9             git:
 10              uri: https://github.com/itcast132465/hello
 11              serchPaths: cong         #远程仓库的文件夹地址
 12              username: itcast132465     #登录名
 13              password: itcast132465   #密码
 14              label: master               #Git仓库的分支名

(3)重启config-server和config-client,我们可以在控制台看到如下所示的日志。使用浏览器访问http://localhost:8762/foo,效果如图1所示,说明我们已经成功从Git仓库远程读取了配置文件。

Located environment:
name=config-client, profiles=[dev], label=null,
Located property source: CompositePropertySource {name='configService',
propertySources=[MapPropertySource {name='configClient'},
MapPropertySource
{name='https://github.com/itcast132465/hello/cong/config-client
dev.yml'}]}
The following profiles are active: dev

从控制台消息中可以看出,config-client客户端从Git远程仓库https://github.com/itcast132465/hello的cong文件夹下,读取了config-client-dev.yml文件的配置信息。

点击此处
隐藏目录