SpringCloud-config初步实践操作

54 阅读2分钟

提要

本文件夹内含两个示例文件,一个是service端(centralized-configuration-service),一个是client端(spring-cloud-config-demo)

第一步-配置git仓库:

设置一个git仓库,里面配置一个文件用于测试,在仓库的所有配置命名规范需要统一按照官方提供的标准进行命名。本次demo采用/{lable}/{application}-{profile}.yml规范,具体看自己喜好或者公司要求配置。例如,master分支下的开发环境的config文件则是/master/config-dev.yml

第二步-配置service端:

step 1 -> 添加相关的依赖,服务端最重要是添加spring-cloud-config-server依赖

step 2 -> 配置src/main/resources/application.yml,里面已经对各项参数进行注释说明

step 3 -> 配置src/main/java/com/example/centralizedconfigurationservice/CentralizedConfigurationServiceApplication,添加注解@EnableConfigServer使其他服务能够识别其为配置中心服务端

step 4 -> 在网页打开http://localhost:7009/master/config-dev.yml查看远程git仓库的配置信息

第三步-配置client端:

step 1 -> 添加相关的依赖,客户端最重要是添加spring-cloud-starter-config依赖,其次添加自动刷新相关依赖spring-boot-starter-actuator以及2.0版本后的springboot需要添加spring-cloud-starter-bootstrap来读取boostrap文件。

step 2 -> 配置src/main/resources/boostrap.yml,里面已经对各项参数进行注释说明 (boostrap是系统级配置参数文件,会优先读取,一般共用配置参数放在内部。私有的放在application.yml中)

step 3 -> 配置src/main/java/com/example/springcloudconfigdemo/controller/ConfigClientController,添加注解@RefreshScope使其能够在接收到post请求后刷新配置文件

step 4 -> 在网页打开http://localhost:7010/configInfo查看配置中心服务端的配置信息

step 5 -> 当远程git仓库有更新时,需要调用

curl -X POST "http://localhost:7010/actuator/refresh"

来进行刷新;或者使用postman等工具发送post请求到localhost:7010/actuator/refresh也能刷新

相关代码地址:gitee.com/zjackman/Sp…