springcloudconfig本地配置中心_SpringCloud极简入门|配置中心入门spring cloud config 第7讲...

f7c54039794d31721222467326d0a8c2.png

一、 Spring Cloud Config简介

微服务要实现集中管理微服务配置、不同环境不同配置、运行期间也可动态调整、配置修改后可以自动更新的需求,Spring Cloud Config同时满足了以上要求。Spring Cloud Config 分为Config Server和Config Client两部分,是一个可以横向扩展,集中式的配置服务器, 默认使用Git存储配置内容。

Spring Cloud Config 原理图如图所示:

-

1d5df0f602083388d175f021ff37ce2f.png

二、创建Server端

代码以springcloud-03-config为例:https://github.com/shinians/springcloud-demos

9495b86cfe8ea8504e968c7c496cbf3e.png

​1.先在github上新建一个仓库,添加配置文件。

8e6a84f48e78c51f1c17b78e29f9873d.png

limp-config-dev.yml 文件内容如下

limp: hello: hello springcloud dev config

2.pom引入

org.springframework.bootspring-boot-starter-weborg.springframework.cloudspring-cloud-starter-config-server

3.在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能,代码如下:

86f84ce55a3c26e06f55dba8ed0c8ac8.png

3.需要在程序的配置文件application.yml文件配置以下

spring: cloud: config: server: git: uri: https://github.com/shinians/spring-cloud-demo-config #username: #password: #search-paths: test-CONFIG application: name: spring-cloud-config-server-007server: port: 7002

spring.cloud.config.server.git.uri:配置git仓库地址

spring.cloud.config.server.git.searchPaths:配置仓库路径

spring.cloud.config.label:配置仓库的分支

spring.cloud.config.server.git.username:访问git仓库的用户名

spring.cloud.config.server.git.password:访问git仓库的用户密码

如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写。

启动后我们可以访问下

b3e942085d8070cb43b6f7357fbf8a39.png

服务端启动成功

http请求地址和资源文件映射如下:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

三:构建客端

pom引入

org.springframework.bootspring-boot-starter-weborg.springframework.cloudspring-cloud-starter-config

配置文件bootstrap.yml

1e26fcd36c8b7755c2319aa0eca9e8d3.png

读取配置文件种的信息

e2eda108ea6ac742ba17521c1d24e1e2.png

Controller

dd714af3a0a043033518381d0884efa9.png

​测试

33d80f6ee100d87e8758298b1e186ab0.png

客户端成功获取配置信息(完成)

End:

例子很简单,像自动刷新、结合bus、配置数据库和公用配置会在之后的章节介绍。

更多信息可以关注今日头条@架构师速成记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值