使用SpringCloudConfig消息队列更新项目配置Demo(更新配置需重启项目)

本文介绍了如何使用SpringCloudConfig实现在本地和远程(通过Git)进行项目配置更新,并详细阐述了配置中心和服务端的配置步骤。在当前设置下,更新配置后需重启项目生效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本地(更新需重启):

需要创建两个项目一个配置中心config-server,一个获取配置的服务config-client
CONFIGE-SERVER:
1、创建springBoot项目 加上 config server 依赖
2、启动类上加@EnableConfigServer注解,开启配置中心服务
3、yml配置

server:
  port: 9090
spring:
  cloud:
    config:
      server:
        native:
          search-locations: classpath:shared   #默认查找classpath下shared文件夹中的配置文件,先创建shared文件夹
  profiles:
    active: native

4、shared文件夹下配置文件
 文件名config-local-client-dev.yml:

server:
  port: 9092
myname: zhangsan

此文件中的 myname属性可以用来测试配置是否被配置中心更改。写一个controller使用@value和@RestController,直接访问http:localhost:9092查看属性值
CONFIG-CLIENT:
1、将application.properties改为bootstrap.yml
2、yml配置

spring:
  application:
    name: config-local-client
  cloud:
    config:
      uri: http://localhost:9090
      fail-fast: true
  profiles:
    active: dev

config-server项目启动,再将config-client项目启动,启动时,config-server根据配置文件去 http://localhost:9090查找名字为自身的spring.application.name(config-local-client)
的后缀为dev的配置文件(即为config-local-client-dev.yml),在shared文件夹中找到,加载配置。

远端情况(git,更新需重启):

两个项目,配置中心为config-remote-server,以下简称server服务,获取配置的服务config-remote-client,以下简称client服务。
CONFIGE-REMOTE-SERVER:
与本地情况一样,只是将CONFIG-SERVER中spring.cloud.config配置为在本地查找的改为在git拉取。
1、创建springBoot项目 加上config server 依赖
2、启动类上加@EnableConfigServer注解,开启配置中心服务
3、yml配置

 server:
      port: 9090
    spring:
      application:
        name: config-remote-server
      cloud:
        config:
          server:
            git:
              uri: #git地址
              search-paths: #文件路径

4、git文件

server:
  port: 9092
myname: lisi

CONFIG-REMOTE-CLIENT:
1、创建springBoot项目
2、将application.properties改为bootstrap.yml
这里为区别本地Demo,仅仅将应用名修改,yml内容为:

spring:
  application:
    name: config-remote-client
  cloud:
    config:
      uri: http://localhost:9090
      fail-fast: true
  profiles:
    active: dev

先将server项目启动,再将client项目启动,启动时,config-remote-client根据配置文件去 http://localhost:9090查找名字为自身的spring.application.name(config-remote-client)
的后缀为dev的配置文件(即为config-remoto-client-dev.yml),server服务根据配置git的地址(uri)以及目录( search-paths)查找配置,加载配置。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值