ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
![](https://img.kancloud.cn/46/59/46593c5fe0940659b801ba74a574ca02_715x298.png) 从上图中,通过前面章节的讲解,我们已经实现了如下内容 1. 建立Git Repository仓库,我们已经可以向仓库提交配置文件了 2. 搭建了Config Server做统一的配置管理,可以从配置仓库拉取配置文件。 本节就为大家讲解第三步:微服务(config 客户端)从config server获取配置的方法。 > 仍然以aservice-sms和aservice-rbac服务(springboot)使用Spring Cloud Config集中配置管理为需求,为大家讲解。 ## 一、配置工作 在aservice-sms和aservice-rbac服务的pom.xml中引入spring-cloud-starter-config依赖: ~~~ <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> ~~~ src/main/resources/bootstrap.yml配置(以aservice-sms为例),来指定config server,例如: ~~~ spring: application: name: aservice-sms cloud: config: uri: http://localhost:8771 label: master profile: dev ~~~ * spring.cloud.config.profile:对应前配置文件中的{profile}部分 * spring.cloud.config.label:对应前配置文件的git分支 * spring.cloud.config.uri:config server配置中心的地址 > **这里需要格外注意:上面这些属性必须配置在`bootstrap.yml`或properties文件中,而不是application.yml中,config配置内容才能被正确加载。因为`bootstrap.yml`加载优先级高于`application.yml`,保证在应用一起动时就去加载配置,对于Spring 中一些自动装载类来说这很重要。** 最后,我们把application.yml中的配置全部注释掉(如下图)。因为这部分配置,我们已经全都放到git 仓库中进行集中管理,我们的服务通过config server就可以获取到。 ![](https://img.kancloud.cn/0e/cc/0eccc037e4b066b98f105ff0ba094520_1363x736.png) ## 二、结果验证 * 首先启动日志,明确的输出:aservice-sms项目在启动的时候去config server:http://localhost:8771加载配置。加载的是aservice-sms的master分支的dev环境的配置文件,也就是:`https://gitee.com/hanxt/dongbb-cloud/zimug-server-config-repo/aservice-sms-dev.yml`,完全和我们的预期一致(和本文上面内容的配置一致)。 ![](https://img.kancloud.cn/b3/5f/b35f8acfa199b59566560d3a6d4ea4a7_1331x504.png) * 然后我们再看aservice-sms是否向eureka成功的进行了服务注册 ![](https://img.kancloud.cn/a4/2b/a42b5a513ded50b361fb5591b1c8c942_1499x228.png) **eureka相关的配置不是注释掉了么?**是本地应用注释掉了,但是我们已经把它转移到远程配置管理仓库中了(《config-git配置文件仓库》那一节中)。这也再次验证了,aservice-sms正确的应用了远程git仓库和config server进行集中的配置管理。