很多时候线上服务的配置需要进行修改,但是我们不想或者没办法重启服务来让配置生效,那么这时候应该怎么办呢?
在Spring Cloud中,spring cloud config所提供的分布式配置中心能完美的解决以上问题,spring cloud config将配置文件存放在配置服务内存中或者托管在远程git仓库中。然后通过一个配置中心来获取配置信息,这样,如果需要维护配置信息,则只需要在本地修改配置文件,然后保存到远程git仓库上即可,十分方便。同时为了保证配置中心的稳定,也可以集群部署。
主要在上一篇的内容上进行扩展.
PS.一下Spring Boot项目都为2.X版本
- 新建配置文件
(1)配置文件的命名规则为
{spring.application.name}-{type}.properties
。
比如我上一篇中的SpringCloudServiceI服务的注册名(spring.application.name)为myServiceI,则其的配置文件名为myServiceI-{type}.properties
。
新建配置文件myServiceI-dev.properties
和myServiceI-prod.properties
myServiceI-dev.properties中内容为name=devConfig
myServiceI-prod.properties中内容为name=prodConfig
然后通过git客户端上传到git仓库,我这里是上传到了码云(github因为某长城的关系访问速度…你懂的)
https://gitee.com/SecondMagic/config
- 配置中心
新建Spring Boot 项目 SpringCloudConfig
(1)pom.xml
添加如下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
(2)application.properties配置
#服务端口
server.port=8091
#服务名称
spring.application.name=myConfigServer
#服务注册中心
eureka.client.service-url.defautZone=http://serviceCenter:8761/eureka/
#服务的git仓库地址
spring