理解 Spring Cloud Bus 的需求

2025 年 1 月 8 日 | 3 分钟阅读

步骤 1: 选择项目 spring-cloud-config-server 并运行 SpringCloudConfigServerApplication.java 文件。

步骤 2: 选择项目 limits-service 并运行 LimitsServiceApplication.java 文件。

步骤 3: 打开浏览器并输入 https:///8080/limits。它将返回以下响应

我们从 limits-service-qa.properties 文件中获取这些值,因为我们已将此文件配置到 limits-servicebootstrap.properties 文件中。

在下一步中,我们将创建另一个 LimitsServiceApplication 实例。

步骤 4: 创建 LimitsServiceApplication 的一个实例。

右键单击 limits-service 项目 -> Run As -> Run Configurations… -> 右键单击 LimitsServiceApplication -> Duplicate -> 将应用程序名称重命名为 LimitsServiceApplication8081 -> 单击 Arguments 选项卡 -> 提供 VM 参数: -Dserver.port=8081 -> Run。

Understanding the need for Spring Cloud Bus

LimitsServiceApplication 的一个实例将在端口 8081 上运行。

步骤 5: 打开浏览器并调用 URL http://locahost:8081/limits。它返回与原始 limits-service 相同的响应。

两个 limits-service 实例正在运行。

步骤 6: 更改 limits-service-qa.properties, 我们将最小值从 2 更改为 22

limits-service-qa.properties

我们需要提交更改。

步骤 7: 打开 Git Bash 并运行以下命令

Understanding the need for Spring Cloud Bus

再次,调用 URL https://:8080/limits 和 https://:8081/limits。这两个 URL 都返回旧值,而我们已在 Git 存储库中提交了值。它不会反映在 limits-service 中。  为了在 limits-service 中进行更改,我们将使用 Postman。

注意:在进行下一步之前,从 limits-service 中删除安全设置。

步骤 8: 打开 limits-service.properties 文件,并使用以下语句禁用 security

再次,在 limits-service-qa.properties 文件中设置旧值并提交更改。

 

步骤 9: 打开 Postman 并使用 URL https://:8080/application/refresh 发送 POST 请求。

注意:如果调用 URL https://:8080/limits 并且它没有返回新值。因此,要通过调用 URL https://:8081/limits 获取新值,您必须执行以下操作
打开 Postman 并使用 URL https://:8081/application/refresh 发送 POST 请求。

步骤 10:  调用 URL https://:8080/limits 和 https://:8081/limits。现在这两个 URL 都返回更改后的值。

我们创建了 limits-service 的两个实例。假设有数百个并行运行的 limits-services 实例。我们需要调用数百个 URL 来从 Git 存储库刷新配置。

调用数百个 URL 并不像 limits-service 数量增加那么容易。 此外,服务的维护也增加了麻烦。

每当我们更改配置时,它必须反映在微服务中。 在这里,Spring Cloud Bus 为此提供了解决方案,因此我们不需要调用数百个 URL。

Spring Cloud Bus 为所有数百个实例提供了一个 URL。 当我们调用该 URL 时,所有微服务的实例都将使用 Git 配置中的最新值进行更新。

在下一步中,我们将实现 Spring Cloud Bus。