在完成了上述验证之后,确定配置服务中心已经正常运作,下面我们尝试如何在微服务应用中获取上述的配置信息。
- 创建一个Spring Boot应用,命名为
config-client
,并在pom.xml
中引入下述依赖:
|
|
- 创建Spring Boot的应用主类,具体如下:
|
|
- 创建
bootstrap.yml
配置,来指定获取配置文件的config-server-git
位置,例如:
|
|
上述配置参数与Git中存储的配置文件中各个部分的对应关系如下:
- spring.application.name:对应配置文件规则中的
{application}
部分 - spring.cloud.config.profile:对应配置文件规则中的
{profile}
部分 - spring.cloud.config.label:对应配置文件规则中的
{label}
部分 - spring.cloud.config.uri:配置中心
config-server
的地址
这里需要格外注意:上面这些属性必须配置在bootstrap.properties中,这样config-server中的配置信息才能被正确加载。
在完成了上面你的代码编写之后,读者可以将config-server-git、config-client都启动起来 我们可以看到该端点将会返回从git仓库中获取的配置信息:
|
|
另外,我们也可以修改config-client的profile为dev来观察加载配置的变化。
从现在开始,我这边会将近期研发的springcloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,希望可以帮助更多的好学者。大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目。源码来源