Spring Cloud 应用篇 之 Spring Cloud Config(配置中心)一文中介绍了 Spring Cloud Config 的使用,已经实现了配置文件的统一管理(git 仓库),但是,每次修改配置文件后,还需要重新启动应用才能加载到修改后的配置文件,这还没有达到我们的目的,我们最终想要的是,修改完配置文件后,不需要重启我们的应用,就可以重新加载到修改后的配置文件,其实 Spring Cloud 已经为我们提供了这样的支持,那就是 Spring Cloud Bus 组件。
(一)简介
下面是官方文档介绍,我们可以使用消息中间件 RabbitMQ 或者 Kafka,只需要在项目中引用相应的依赖。至于消息中间件的使用,可以查阅相关资料。
Spring Cloud Bus works by adding Spring Boot autconfiguration if it detects itself on the classpath. To enable the bus, add
spring-cloud-starter-bus-amqp or spring-cloud-starter-bus-kafka to your dependency management. Spring Cloud takes care of the rest.
Make sure the broker (RabbitMQ or Kafka) is available and configured. When running on localhost, you need not do anything. If you
run remotely, use Spring Cloud Connectors or Spring Boot conventions to define the broker credentials
下面我们就基于上篇文章的项目进行改造即可。
(二)改造 spring-cloud-config
2.1 修改 spring-cloud-config 的 pom 文件,添加依赖
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starte