美文网首页微服务架构与应用我爱编程java面试
Spring Boot微服务架构 - 分布式配置中心(Spr

Spring Boot微服务架构 - 分布式配置中心(Spr

作者: AKyS佐毅 | 来源:发表于2018-02-12 13:45 被阅读69次

1、简介

  • 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。
  • Spring Cloud中,有分布式配置中心组件spring cloud config,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client

2 、构建工程

  • pom.xml文件设置文件
  • application.yml文件设置
  • 启动类中设置操作如下
  • 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仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,放心使用。

远程仓库https://github.com/forezp/SpringcloudConfig/ 中有个文件config-client-dev.properties文件中有一个属性:

foo = foo version 3

启动程序:访问http://localhost:8888/foo/dev

{"name":"foo","profiles":["dev"],"label":"master",
"version":"792ffc77c03f4b138d28e89b576900ac5e01a44b","state":null,"propertySources":[]}
  • 证明配置服务中心可以从远程程序获取配置信息。

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

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

  • /{application}-{profile}.yml

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

  • /{application}-{profile}.properties

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

    3、构建一个config client

    • 重新创建一个springboot项目,取名为config-client,其pom文件:
    • 启动类设置
    • 其配置文件bootstrap.properties:

foo version 3

注意事项: 本文转载自地址 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)


微信扫码关注java架构,获取Java面试题和架构师相关题目和视频。

相关文章

网友评论

    本文标题:Spring Boot微服务架构 - 分布式配置中心(Spr

    本文链接:https://www.haomeiwen.com/subject/ocvrtftx.html