在微服务 SpringCloud 之 Spring Cloud Config 配置中心 SVN 博客中每个 client 刷新配置信息时需要 post 请求 / actuator/refresh, 但客户端越来越多时,, 需要每个客户端都执行一遍, 这种方案就不太适合了. 使用 Spring Cloud Bus 可以完美解决这一问题.
一, Spring Cloud Bus
Spring cloud bus 通过轻量消息代理连接各个分布的节点. 这会用在广播状态的变化 (例如配置变化) 或者其他的消息指令. Spring bus 的一个核心思想是通过分布式的启动器对 spring boot 应用进行扩展, 也可以用来建立一个多个应用之间的通信频道. 目前唯一实现的方式是用 AMQP 消息代理作为通道, 同样特性的设置 (有些取决于通道的设置) 在更多通道的文档中.
Spring cloud bus 被国内很多都翻译为消息总线, 也挺形象的. 大家可以将它理解为管理和传播所有分布式项目中的消息既可, 其实本质是利用了 MQ 的广播机制在分布式的系统中传播消息, 目前常用的有 Kafka 和 RabbitMQ. 利用 bus 的机制可以做很多的事情, 其中配置中心客户端刷新就是典型的应用场景之一, 我们用一张图来描述 bus 在配置中心使用的机制.
二, 启动 RabbitMq
启动 RabbitMq 的具体方法之前博客已经写了, 略过.
三, SpringCloud Config Server 端
1. 引入依赖
Demo 是在上篇博客的基础上进行修改, 主要增加引入 spring-cloud-starter-bus-amqp.
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.6.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example</groupId>
- <artifactId>SpringCloudConfigServer</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>SpringCloudConfigServer</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</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>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-bus-amqp</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
- View Code
2. 新增 RabbitMq 配置信息
这里主要增加开启消息跟踪, 配置 rabbitmq 相关信息.
- spring.cloud.bus.trace.enabled=true
- spring.rabbitmq.host=127.0.0.1
- spring.rabbitmq.port=5672
- spring.rabbitmq.username=guest
- spring.rabbitmq.password=guest
- management.endpoints.Web.exposure.include=*
四, SpringCloud Config Client 端
1. 引入依赖
Client 端和服务端有类似的配置, 也是引入 spring-cloud-starter-bus-amqp, 增加 rabbitmq 相关配置.
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.6.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example</groupId>
- <artifactId>SpringCloudConfigClient</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>SpringCloudConfigClient</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-Web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-config</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</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-starter-bus-amqp</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
- View Code
2. 新增 RabbitMq 配置信息
- management.endpoints.Web.exposure.include=*
- ## 开启消息跟踪
- spring.cloud.bus.trace.enabled=true
- spring.rabbitmq.host=127.0.0.1
- spring.rabbitmq.port=5672
- spring.rabbitmq.username=guest
- spring.rabbitmq.password=guest
五, 测试
1. 依次启动 EurekaServer,SpringCloudConfigServer,SpringCloudConfigClient, 输入 http://localhost:8001/neo-config/dev , http://localhost:8003/hello .
2. 修改配置信息
修改 GitHub 上的配置信息, 再次输入上面的 url, 在 8003 端口的是没有更新的, 此时需要 post 请求 http://localhost:8001/actuator/bus-refresh 来刷新 SpringCloudConfigServer. 这里在 post 请求时可能会出现 405,404 的错误, 需要设置 management.endpoints.Web.exposure.include=*, 并且由于 springcloud 的版本不同, post 请求的 url 也不同.
post 请求之后返回 204, 然后刷新即可看到最新的配置信息.
参考: http://www.ityouknow.com/springcloud/2017/05/26/springcloud-config-eureka-bus.html
来源: https://www.cnblogs.com/5ishare/p/11490849.html