Spring Cloud Config 是一个**分布式配置管理工具**,用于集中管理微服务架构中所有环境的配置文件。它解决了微服务系统中配置分散、难以管理的问题。 ## 一、建立Gitee仓库 登录[Gitee - 基于 Git 的代码托管和研发协作平台](https://gitee.com/)  通过`代码`去上传文件  上传服务提供者和消费者的配置文件  ## 二、配置文件命名规则 ```java /{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.yml /{label}/{application}-{profile}.properties ``` `application`为应用名称、`profile`指的开发环境(用于区分开发环境(`dev`),测试环境(`test`)、生产环境(`pro`)等) ## 三、创建SCC服务端 创建`config`子模块 #### 1、添加依赖 ```xml org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-netflix-eureka-client ``` #### 2、创建主启动类 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication // 开启Config服务器 @EnableConfigServer public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } } ``` #### 3、创建配置 在资源目录下创建`application.yml` ```yml server: port: 15000 spring: application: name: config cloud: config: server: git: uri: https://gitee.com/v_Xq/scc-test.git #git仓库地址 #eureka配置 eureka: client: service-url: defaultZone: http://eurekaServer13000:13000/eureka,http://eurekaServer13001:13001/eureka instance: prefer-ip-address: true #使用ip地址注册 instance-id: ${spring.cloud.client.ip-address}:${server.port} ``` #### 4、启动测试 访问[localhost:15000/consumer-dev.yml](http://localhost:15000/consumer-dev.yml)获取仓库中的配置  ## 四、创建SCC客户端 修改服务提供者和消费者的配置 #### 1、添加依赖 ```xml org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-bootstrap ``` **注意**:Spring Boot 2.4+ 版本后,默认禁用了 bootstrap.yml,需要添加依赖才可以引用该配置文件 #### 2、删除`application.yml`配置 #### 3、创建`bootstrap.yml`配置 ```yml spring: cloud: config: #应用名称。对应git中文件名称的前半部分 name: provider / provider11001 / consumer #开发环境。对应git中文件名称的后半部分 profile: dev #git分支 label: master #config server的请求地址 #uri: http://localhost:15000 discovery: enabled: true #开启服务发现 service-id: config #config server服务名 #eureka配置 eureka: client: service-url: #将自己注册给Eureka Server集群 defaultZone: http://eurekaServer13000:13000/eureka,http://eurekaServer13001:13001/eureka instance: prefer-ip-address: true #使用ip地址注册 instance-id: ${spring.cloud.client.ip-address}:${server.port} ``` - bootstrap.yml用来在程序引导时执行,应用于更加早期配置信息读取。也就是说:bootstrap.yml配置文件在application.yml配置文件之前被加载。 - bootstrap.yml 用于系统级别的参数配置,这些参数一般是不会变动的。application.yml 用于定义应用级别的参数配置。 - bootstrap.yml的典型应用场景就是:配置信息一般是从 config server中加载。为了取得配置信息,需要一些提早的引导配置。因此,把 config server 信息放在 bootstrap.yml,用来加载在这个时期真正需要的配置信息。 #### 4、重启测试 即使没有本地配置文件也可以运行。 ## 五、Bus动态刷新配置 仅用SpringCloudConfig只能在启动的时候刷新一次配置,若在运行中Git上的配置有更改的话无法及时更改,使用Spring Cloud Bus(消息总线)可以解决这一问题。它用于在集群中传播状态变化,可与SpringCloudConfig联合实现热部署。 #### 1、工作流程 配置变更 → Config Server → Spring Cloud Bus → 消息代理 → 所有微服务实例 ##### 支持的消息代理 - **RabbitMQ**(最常用) - **Kafka** - **Redis**(通过Spring Cloud Stream) #### 2、RabbitMQ部署 ##### 安装Erlang语言环境 官方网站:[Downloads - Erlang/OTP](https://www.erlang.org/downloads) 博客下载链接:[otp_win64_28.2.exe](https://pan.soki.me/s/ONSW) 下载完成后,按照默认配置下一步安装即可。 安装完成后在系统变量中`path`中添加`Erlang`的`bin`目录 在终端输入`erl`,出现下图则安装成功 ##### 安装RabbitMQ 官方网站:[Installing on Windows | RabbitMQ](https://www.rabbitmq.com/docs/install-windows) 博客下载链接:[rabbitmq-server-4.2.1.exe](https://pan.soki.me/s/pACv) 进入安装目录`C:\Program Files\RabbitMQ Server\rabbitmq_server-4.2.1\sbin` 打开终端安装管理插件`.\rabbitmq-plugins enable rabbitmq_management` 在Windows服务中重启服务 打开[RabbitMQ Management](http://127.0.0.1:15672/),账号密码都是`guest`  #### 3、修改Config服务端配置 ##### 添加依赖 ```xml org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.boot spring-boot-starter-amqp org.springframework.boot spring-boot-starter-actuator ``` ##### 修改配置 ```yml server: port: 15000 spring: application: name: config cloud: config: server: git: uri: https://gitee.com/v_Xq/scc-test.git #git仓库地址 bus: enabled: true refresh: enabled: true rabbitmq: host: localhost port: 5672 username: guest password: guest # 启用Bus端点 management: endpoints: web: exposure: include: bus-refresh, health, info endpoint: health: show-details: always bus-refresh: enabled: true #eureka配置 eureka: client: service-url: defaultZone: http://eurekaServer13000:13000/eureka,http://eurekaServer13001:13001/eureka instance: prefer-ip-address: true #使用ip地址注册 instance-id: ${spring.cloud.client.ip-address}:${server.port} ``` #### 4、修改Config客户端配置 ##### 添加依赖 ```xml org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.boot spring-boot-starter-amqp org.springframework.boot spring-boot-starter-actuator ``` ##### 修改配置 每个需要使用SCC的微服务的`boostrap.yml`配置文件都需要添加Bus相关的配置 ```yml spring: cloud: bus: enabled: true refresh: enabled: true rabbitmq: host: localhost port: 5672 username: guest password: guest # 暴露actuator端点,接收Bus刷新消息 management: endpoints: web: exposure: include: bus-refresh, refresh, health, info ``` ##### 修改控制器 在类头上方添加`@RefreshScope`注解,支持动态刷新 ```java import cn.doooog.consumer.feign.TestFeignClient; import cn.doooog.consumer.po.CommonResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/test") @RestController @RefreshScope // 支持动态刷新 public class TestController { @Autowired private TestFeignClient testFeignClient; @RequestMapping("/id/{id}") public CommonResult test(@PathVariable("id") Integer id) { return testFeignClient.getUserById(id); } } ``` #### 5、刷新配置 ##### 查看端点 访问[localhost:15000/actuator](http://localhost:15000/actuator)可以查看到当前开放的端点  ##### 手动刷新 通过`POST`请求端点`http://localhost:15000/actuator/busrefresh`即可手动发起刷新 响应数据为空,状态码为`204`即可正常刷新。 ##### 测试 可以通过在`provider`里添加一个`message`配置项来测试 修改对应的Controller,使用`@Value("${message}")`注解注入配置字段 ```java import cn.doooog.provider.po.CommonResult; import cn.doooog.provider.po.User; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/user") @RestController @RefreshScope public class UserController { @Value("${message}") private String msg; @RequestMapping("/id/{id}") public CommonResult getUserById(@PathVariable("id") Integer id) { //模拟返回业务数据 return CommonResult.success(new User(id, "张三", "123")); } @RequestMapping("/msg") public CommonResult getMsg() { return CommonResult.success(msg); } } ``` 修改消费者的Feign接口 ```java import cn.doooog.consumer.feign.impl.TestFeignClientCallBack; import cn.doooog.consumer.po.CommonResult; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @FeignClient(name = "provider",fallback = TestFeignClientCallBack.class) public interface TestFeignClient { @GetMapping("/user/id/{userId}") CommonResult getUserById(@PathVariable("userId") Integer userId); @GetMapping("/user/msg") CommonResult getMsg(); } ``` 修改消费者Controller ```java import cn.doooog.consumer.feign.TestFeignClient; import cn.doooog.consumer.po.CommonResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/test") @RestController @RefreshScope // 支持动态刷新 public class TestController { @Autowired private TestFeignClient testFeignClient; @RequestMapping("/id/{id}") public CommonResult test(@PathVariable("id") Integer id) { return testFeignClient.getUserById(id); } @RequestMapping("/msg") public CommonResult testMsg() { return testFeignClient.getMsg(); } } ``` 修改网关配置 重启服务后访问`http://localhost:14000/test/msg?token=2`进行测试 修改后刷新配置重新访问即可看到修改内容。 Loading... Spring Cloud Config 是一个**分布式配置管理工具**,用于集中管理微服务架构中所有环境的配置文件。它解决了微服务系统中配置分散、难以管理的问题。 ## 一、建立Gitee仓库 登录[Gitee - 基于 Git 的代码托管和研发协作平台](https://gitee.com/)  通过`代码`去上传文件  上传服务提供者和消费者的配置文件  ## 二、配置文件命名规则 ```java /{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.yml /{label}/{application}-{profile}.properties ``` `application`为应用名称、`profile`指的开发环境(用于区分开发环境(`dev`),测试环境(`test`)、生产环境(`pro`)等) ## 三、创建SCC服务端 创建`config`子模块 #### 1、添加依赖 ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> ``` #### 2、创建主启动类 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication // 开启Config服务器 @EnableConfigServer public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } } ``` #### 3、创建配置 在资源目录下创建`application.yml` ```yml server: port: 15000 spring: application: name: config cloud: config: server: git: uri: https://gitee.com/v_Xq/scc-test.git #git仓库地址 #eureka配置 eureka: client: service-url: defaultZone: http://eurekaServer13000:13000/eureka,http://eurekaServer13001:13001/eureka instance: prefer-ip-address: true #使用ip地址注册 instance-id: ${spring.cloud.client.ip-address}:${server.port} ``` #### 4、启动测试 访问[localhost:15000/consumer-dev.yml](http://localhost:15000/consumer-dev.yml)获取仓库中的配置  ## 四、创建SCC客户端 修改服务提供者和消费者的配置 #### 1、添加依赖 ```xml <!--加入spring-cloud-config的依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- 添加 Bootstrap 支持,使 bootstrap.yml 生效 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> ``` **注意**:Spring Boot 2.4+ 版本后,默认禁用了 bootstrap.yml,需要添加依赖才可以引用该配置文件 #### 2、删除`application.yml`配置 #### 3、创建`bootstrap.yml`配置 ```yml spring: cloud: config: #应用名称。对应git中文件名称的前半部分 name: provider / provider11001 / consumer #开发环境。对应git中文件名称的后半部分 profile: dev #git分支 label: master #config server的请求地址 #uri: http://localhost:15000 discovery: enabled: true #开启服务发现 service-id: config #config server服务名 #eureka配置 eureka: client: service-url: #将自己注册给Eureka Server集群 defaultZone: http://eurekaServer13000:13000/eureka,http://eurekaServer13001:13001/eureka instance: prefer-ip-address: true #使用ip地址注册 instance-id: ${spring.cloud.client.ip-address}:${server.port} ``` - bootstrap.yml用来在程序引导时执行,应用于更加早期配置信息读取。也就是说:bootstrap.yml配置文件在application.yml配置文件之前被加载。 - bootstrap.yml 用于系统级别的参数配置,这些参数一般是不会变动的。application.yml 用于定义应用级别的参数配置。 - bootstrap.yml的典型应用场景就是:配置信息一般是从 config server中加载。为了取得配置信息,需要一些提早的引导配置。因此,把 config server 信息放在 bootstrap.yml,用来加载在这个时期真正需要的配置信息。 #### 4、重启测试 即使没有本地配置文件也可以运行。 ## 五、Bus动态刷新配置 仅用SpringCloudConfig只能在启动的时候刷新一次配置,若在运行中Git上的配置有更改的话无法及时更改,使用Spring Cloud Bus(消息总线)可以解决这一问题。它用于在集群中传播状态变化,可与SpringCloudConfig联合实现热部署。 #### 1、工作流程 配置变更 → Config Server → Spring Cloud Bus → 消息代理 → 所有微服务实例 <img src="https://pic.soki.me/file/piclist/image-20251210084730297-1765327659003.png" alt="image-20251210084730297" style="zoom:50%;" style=""> ##### 支持的消息代理 - **RabbitMQ**(最常用) - **Kafka** - **Redis**(通过Spring Cloud Stream) #### 2、RabbitMQ部署 ##### 安装Erlang语言环境 官方网站:[Downloads - Erlang/OTP](https://www.erlang.org/downloads) 博客下载链接:[otp_win64_28.2.exe](https://pan.soki.me/s/ONSW) 下载完成后,按照默认配置下一步安装即可。 安装完成后在系统变量中`path`中添加`Erlang`的`bin`目录 <img src="https://pic.soki.me/file/piclist/image-20251210085918010-1765328384019.png" alt="image-20251210085918010" style="zoom: 67%;" style=""> 在终端输入`erl`,出现下图则安装成功 <img src="https://pic.soki.me/file/piclist/image-20251210090144654-1765328504947.png" alt="image-20251210090144654" style="zoom: 67%;" style=""> ##### 安装RabbitMQ 官方网站:[Installing on Windows | RabbitMQ](https://www.rabbitmq.com/docs/install-windows) 博客下载链接:[rabbitmq-server-4.2.1.exe](https://pan.soki.me/s/pACv) 进入安装目录`C:\Program Files\RabbitMQ Server\rabbitmq_server-4.2.1\sbin` 打开终端安装管理插件`.\rabbitmq-plugins enable rabbitmq_management` <img src="https://pic.soki.me/file/piclist/image-20251210091227057-1765329147385.png" alt="image-20251210091227057" style="zoom:67%;" style=""> 在Windows服务中重启服务 <img src="https://pic.soki.me/file/piclist/image-20251210091333809-1765329214089.png" alt="image-20251210091333809" style="zoom:67%;" style=""> 打开[RabbitMQ Management](http://127.0.0.1:15672/),账号密码都是`guest`  <img src="https://pic.soki.me/file/piclist/image-20251210091541372-1765329341722.png" alt="image-20251210091541372" style="zoom:67%;" style=""> #### 3、修改Config服务端配置 ##### 添加依赖 ```xml <!-- Bus总线 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> <!-- 或者使用Kafka --> <!-- <artifactId>spring-cloud-starter-bus-kafka</artifactId> --> </dependency> <!-- RabbitMQ 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!-- 监听依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` ##### 修改配置 ```yml server: port: 15000 spring: application: name: config cloud: config: server: git: uri: https://gitee.com/v_Xq/scc-test.git #git仓库地址 bus: enabled: true refresh: enabled: true rabbitmq: host: localhost port: 5672 username: guest password: guest # 启用Bus端点 management: endpoints: web: exposure: include: bus-refresh, health, info endpoint: health: show-details: always bus-refresh: enabled: true #eureka配置 eureka: client: service-url: defaultZone: http://eurekaServer13000:13000/eureka,http://eurekaServer13001:13001/eureka instance: prefer-ip-address: true #使用ip地址注册 instance-id: ${spring.cloud.client.ip-address}:${server.port} ``` #### 4、修改Config客户端配置 ##### 添加依赖 ```xml <!-- Bus总线 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> <!-- 或者使用Kafka --> <!-- <artifactId>spring-cloud-starter-bus-kafka</artifactId> --> </dependency> <!-- RabbitMQ 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!-- 监听依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` ##### 修改配置 每个需要使用SCC的微服务的`boostrap.yml`配置文件都需要添加Bus相关的配置 ```yml spring: cloud: bus: enabled: true refresh: enabled: true rabbitmq: host: localhost port: 5672 username: guest password: guest # 暴露actuator端点,接收Bus刷新消息 management: endpoints: web: exposure: include: bus-refresh, refresh, health, info ``` ##### 修改控制器 在类头上方添加`@RefreshScope`注解,支持动态刷新 ```java import cn.doooog.consumer.feign.TestFeignClient; import cn.doooog.consumer.po.CommonResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/test") @RestController @RefreshScope // 支持动态刷新 public class TestController { @Autowired private TestFeignClient testFeignClient; @RequestMapping("/id/{id}") public CommonResult test(@PathVariable("id") Integer id) { return testFeignClient.getUserById(id); } } ``` #### 5、刷新配置 ##### 查看端点 访问[localhost:15000/actuator](http://localhost:15000/actuator)可以查看到当前开放的端点  ##### 手动刷新 通过`POST`请求端点`http://localhost:15000/actuator/busrefresh`即可手动发起刷新 <img src="https://pic.soki.me/file/piclist/image-20251210105049080-1765335049501.png" alt="image-20251210105049080" style="zoom:80%;" style=""> 响应数据为空,状态码为`204`即可正常刷新。 ##### 测试 可以通过在`provider`里添加一个`message`配置项来测试 <img src="https://pic.soki.me/file/piclist/image-20251210105227072-1765335147369.png" alt="image-20251210105227072" style="zoom:80%;" style=""> 修改对应的Controller,使用`@Value("${message}")`注解注入配置字段 ```java import cn.doooog.provider.po.CommonResult; import cn.doooog.provider.po.User; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/user") @RestController @RefreshScope public class UserController { @Value("${message}") private String msg; @RequestMapping("/id/{id}") public CommonResult<User> getUserById(@PathVariable("id") Integer id) { //模拟返回业务数据 return CommonResult.success(new User(id, "张三", "123")); } @RequestMapping("/msg") public CommonResult<String> getMsg() { return CommonResult.success(msg); } } ``` 修改消费者的Feign接口 ```java import cn.doooog.consumer.feign.impl.TestFeignClientCallBack; import cn.doooog.consumer.po.CommonResult; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @FeignClient(name = "provider",fallback = TestFeignClientCallBack.class) public interface TestFeignClient { @GetMapping("/user/id/{userId}") CommonResult getUserById(@PathVariable("userId") Integer userId); @GetMapping("/user/msg") CommonResult getMsg(); } ``` 修改消费者Controller ```java import cn.doooog.consumer.feign.TestFeignClient; import cn.doooog.consumer.po.CommonResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/test") @RestController @RefreshScope // 支持动态刷新 public class TestController { @Autowired private TestFeignClient testFeignClient; @RequestMapping("/id/{id}") public CommonResult test(@PathVariable("id") Integer id) { return testFeignClient.getUserById(id); } @RequestMapping("/msg") public CommonResult testMsg() { return testFeignClient.getMsg(); } } ``` 修改网关配置 <img src="https://pic.soki.me/file/piclist/image-20251210105515274-1765335315619.png" alt="image-20251210105515274" style="zoom:67%;" style=""> 重启服务后访问`http://localhost:14000/test/msg?token=2`进行测试 修改后刷新配置重新访问即可看到修改内容。 最后修改:2025 年 12 月 10 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 👆请简单粗暴地爱我。