SpringCloud(九)微服务的配置中心-Spring Cloud Config

文章内索引
[显示]

配置中心(Spring Cloud Config)

spring cloud config ,支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

 

服务端(Config Server)

Simple

http://start.spring.io/中,输入config server

添加@EnableConfigServer,配置文件指定git地址与路径即可。

在git上上传配置文件,其中有配置,ps.github访问太慢,之后换为gitlib访问。

init.failed.exit=false

访问

http://localhost:8888/var-dev.properties

返回

SpringCloud(九)微服务的配置中心-Spring Cloud Config|spring cloud-翰林小院 第1张

 

证明已经读取到远端git上的配置。

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

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

客户端(Config Client)

Simple

http://start.spring.io/中,输入config client,web

 

@Value(“${init.failed.exit}”) // 读取gitlab配置文件中的属性,如果我们读取到了值,说明客户端是OK的

@RefreshScope //注解@RefreshScope指示Config客户端在服务器配置改变时,也刷新注入的属性值

spring.application.name需要与git上的{application}一致,如果spring.application.name和git上的属性文件名对应不上,需要配置spring.cloud.config.name: 文件名不包括profile

 

访问localhost:8080返回

SpringCloud(九)微服务的配置中心-Spring Cloud Config|spring cloud-翰林小院 第2张

此配置为git上读取到的配置,下一章会讲到配置中心的高可用以及配置推送。


©版权声明:本文为【翰林小院】(huhanlin.com)原创文章,转载时请注明出处!

发表评论

电子邮件地址不会被公开。