实现Spring Cloud Bus详细步骤

在本节中, 当我们在Git存储库中进行更改时, 我们必须命中limits-service的多个实例以刷新配置。
我们将调用一个URL, 它将反映微服务的所有一百个实例。在这里, 我们将使用Spring Cloud Bus。 Spring Cloud Bus中有许多可用选项:Apache Kafka, RabbitMQ等。在本节中, 我们将使用RabbitMQ。
注意:在进行下一步之前, 请确保RabbitMQ服务器在后台运行。
让我们在limits-service和spring-cloud-config-server中实现Spring Cloud Bus。
步骤1:打开limits-service和spring-cloud-config-server项目的pom.xml文件。添加amqp依赖项并保存文件以反映更改。

< dependency> < groupId> org.springframework.cloud< /groupId> < artifactId> spring-cloud-starter-bus-amqp< /artifactId> < /dependency>

步骤2:打开bootstrap.properties文件, 并通过添加以下语句来禁用管理安全性:
依赖性并保存文件以反映更改。
management.security.enabled=false

步骤3:首先, 运行SpringCloudConfigServerApplication.java文件, 然后运行limits-service的两个实例:LimitsServiceApplication和LimitsServiceApplication8081。
第5步:调用URL http:// localhost:8080 / limits。它返回以下响应:
依赖性并保存文件以反映更改。
{"maximum":222, "minimum":22}

现在, 调用URL http:// localhost:8081 / limits。它还返回相同的响应。
步骤6:打开limits-service-qa.properties文件, 并将最小值从22更改为29。
现在我们必须提交更改。
第7步:打开邮递员并发送URL为http:// localhost:8080 / bus / refresh的POST请求。
注意:如果你使用的是Spring Boot 2.0.0或更高版本, 请使用以下URL:http:// localhost:8080 / actuatror / bus-refresh步骤8:打开浏览器, 并调用Limits-service的两个实例(http:// localhost:8080 / limits和http:// localhost:8081 / limits)。这两个URL均返回我们在limits-service-qa.properties文件中配置的更新值。
{"maximum":222, "minimum":29}

我们已经看到最小值更改为29。在这里, 你可以注意到我们没有在Git存储库中手动提交更改, 但是更改反映在Limits-service的两个实例中。
我们没有提交Git存储库, 而是调用了URL http:// localhost:8080 / bus / refresh。所有这些都是由于Spring Cloud Bus而发生的。
在特定实例中, 我们已经在RabbitMQ上运行了Spring Cloud Bus。当我们启动应用程序时, 它也从此开始。启动Spring Cloud Bus之后, 所有微服务都向该总线注册。
当我们更改配置并在任何实例上调用更改时, 微服务都会通过Spring Cloud Bus发送事件。 Spring Cloud Bus将该事件传播到已向其注册的所有微服务实例。
在本节中, 我们解决了调用微服务的多个实例的问题。
点击这里下载限制服务
【实现Spring Cloud Bus详细步骤】点击这里下载spring-cloud-config-server

    推荐阅读