在git上创建仓库,把配置文件上传到里面
创建配置中心服务
在服务里创建配置文件application.properties
server.port=12000
spring.application.name=cloud-config
spring.cloud.config.server.git.uri=写上git地址
spring.rabbitmq.host=localhost
#暴露触发消息总线的地址
management.endpoints.web.exposure.include=bus-refresh
导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
入口类加上注解
@EnableConfigServer
这样服务端就完成了
接着客户端
创建文件
bootstrap.properties
#对应git里的master分支的eureka-dev.properties文件
spring.cloud.config.name=eureka
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://127.0.0.1:12000
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<!-- 依赖于消息队列,由于使用的是rabbitmq,导入rabbitmq相关依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
还要确保git中的配置文件里加上了引入了mq
spring.rabbitmq.host=localhost
这样改完配置文件后,用postman调用一下如下地址,记得要是post请求
http://localhost:12000/actuator/bus-refresh
这样修改springboot相关的如mysql等配置就生效了
如果是自定义配置,要在调用的controller加上@RefreshScope注解