Spring Cloud(二) :分布式配置中心,动态修改和应用配置

很多时候线上服务的配置需要进行修改,但是我们不想或者没办法重启服务来让配置生效,那么这时候应该怎么办呢?
在Spring Cloud中,spring cloud config所提供的分布式配置中心能完美的解决以上问题,spring cloud config将配置文件存放在配置服务内存中或者托管在远程git仓库中。然后通过一个配置中心来获取配置信息,这样,如果需要维护配置信息,则只需要在本地修改配置文件,然后保存到远程git仓库上即可,十分方便。同时为了保证配置中心的稳定,也可以集群部署。
主要在上一篇的内容上进行扩展.
PS.一下Spring Boot项目都为2.X版本

  1. 新建配置文件
    (1)配置文件的命名规则为
    {spring.application.name}-{type}.properties
    比如我上一篇中的SpringCloudServiceI服务的注册名(spring.application.name)为myServiceI,则其的配置文件名为myServiceI-{type}.properties
    新建配置文件myServiceI-dev.propertiesmyServiceI-prod.properties
    myServiceI-dev.properties中内容为name=devConfig
    myServiceI-prod.properties中内容为name=prodConfig
    然后通过git客户端上传到git仓库,我这里是上传到了码云(github因为某长城的关系访问速度…你懂的)
    https://gitee.com/SecondMagic/config

  1. 配置中心
    新建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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

团子ing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值