Spring Cloud builds an enterprise-level bus-part 8 message bus

Preface

Let me recap first. In the previous introduction of spring Cloud Config, we still left a suspense: how to achieve real-time update of configuration information. Although we have been able to pass the /refresh interface, if all trigger operations require us to maintain the application location manually, this will become more and more difficult to maintain with the continuous expansion of the system, and the message broker middleware is the solution The most suitable solution to this problem, the message broker middleware can route the message to one or more destinations. Using this function, we can perfectly solve the problem. Let's talk about the specific implementation scheme in Spring Cloud Bus.

Principle analysis

We use the integration of Spring Cloud Bus and Spring Cloud Config, and use Kafka as the message broker to dynamically update the application configuration.


 
The architecture of the entire solution is shown in the figure above, which contains three instances of Svn warehouse, Config Server, and microservice "Service A". All three instances have introduced Spring Cloud Bus, so they are all connected to Kafka messages On the bus.
When we start the system, the three instances of "Service A" will request the Config Server to obtain configuration information. The Config Server obtains the configuration information from the Svn warehouse and returns it according to the rules of the application configuration.
At this point, if we need to modify the attributes of "Service A". First, use the Svn management tool to modify the corresponding attribute value in the warehouse, but this modification will not trigger the attribute update of the "Service A" instance. We send a POST request to instance 3 of "Service A" to access the /bus/refresh interface. At this point, instance 3 of "Service A" will send a refresh request to the message bus, and the message event will be retrieved from the bus by instance 1 and instance 2 of "Service A", and get them from the Config Server again The configuration information to realize the dynamic update of configuration information.

Use case

ready

Let's try the whole configuration process in detail below:
Preparatory work: We are not doing new applications here, but we need to use the several projects about Spring Cloud Config that we have implemented in the previous chapter. If readers don't understand them yet, it is recommended to read Chapter 6 first.
  • cloudConfig: A directory defined in the Svn warehouse, which stores a multi-environment configuration file named didispace, with a from parameter in the configuration file.
  • config-server-eureka: Configure the Svn warehouse and register it to the Eureka server.
  • config-client-eureka: Discover the client of Config Server through Eureka. The application name is didispace, which is used to access the configuration server to obtain configuration information. The application provides a /from interface, which will get the from property in \cloudConfig\config-repo\didispace.properties and return it.

Integrate Spring Cloud Bus

If we want to use Kafka to implement the message bus, we only need to modify the spring-cloud-starter-bus-kafka module in the dependenies node of pom.xml, as follows:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
If we use the default configuration when we start Kafka, then we don't need to do any other configuration to implement Kafka locally. We can try to start up the ZooKeeper and Kafka we just built, and start up the config-server and config-client modified to the spring-cloud-starter-bus-kafka module.
When the config-server starts, we can see the following output in the console:
 
At this point, we can use the kafka-topics --list --zookeeper localhost:2181 command to view the topics in the current Kafka. If the config-server has been successfully started and configured correctly, we can see that there are more in Kafka A topic called springCloudBus.
We then start the config-client configured with the spring-cloud-starter-bus-kafka module, and you can see the following output in the console:
 
You can see that similar content is output when the config-client is started, and they all subscribe to the topic named springCloudBus.
After starting config-server and config-client, in order to observe the effect of message bus refresh configuration more clearly, we can start multiple config-clients with different ports locally. At this point, our config-server and multiple config-clients have been connected to the message bus implemented by Kafka. We can first visit the /from request on each config-client to view the configuration content he obtained. Then, modify the corresponding parameter content in Svn, and then visit the /from request on each config-client, you can see that the configuration content has not changed. Finally, we send a POST request to the config-server: /bus/refresh, at this time we visit the /from request on each config-client, we can get the latest configuration information, and the configuration on each client has been loaded Configure content for the latest Svn
(curl -X POST http://IP:7002/bus/refresh)。
From the config-client console, we can see the following:
 
The RefreshListener listener class records the receipt of a remote refresh request and refreshes the log of the from attribute.

Intelligent Recommendation

Message Bus Bus - Spring Cloud Series (8)

This article is based on spring-boot-starter-parent 2.0.6RELEASE, spring-cloud-dependencies Finchley.SR2. What is Spring Cloud Bus? Spring Cloud Bus is used to provide message bus functionality for mi...

Chapter 8: Spring Cloud Bus Message Bus

In the previous two articles, we talked about the Spring Cloud Config configuration center. When we update the configuration on github, if we want to get the latest configuration, we need to manually ...

SpringCloud (8 message bus (Spring Cloud Bus))

This article mainly comes from the Internet, for reference only Based on my previous blog:     Spring Cloud Bus Spring Cloud Bus connects distributed nodes with a lightweight message broker....

Message bus: Spring Cloud Bus (Chapter 8)

Author: jiangzz Phone: 15652034180 WeChat: jiangzz_wx WeChat public account: jiangzz_wy In a system with a microservice architecture, we usually use a lightweight message broker to build a common mess...

8. Spring Cloud bus message bus

concept When we use the configuration center, when the configuration of the configuration center changes, we will send a post request to the client to let it regain the new configuration. When the cli...

More Recommendation

The simplest Spring Cloud tutorial in history | Part 8: Message Bus (Spring Cloud Bus)

Copyright statement: This article is the original article of the blogger, welcome to reprint, please indicate the author, hyperlink to the original text, and blogger address: http://blog.csdn.net/fore...

Introduction to Spring Cloud | Part Seven: Message Bus (Spring Cloud Bus)

Spring Cloud Bus links the nodes of the distributed system with a lightweight message broker. This can be used to broadcast status changes (such as configuration changes) or other management instructi...

Chapter 8 Spring Cloud Message Bus (Spring Cloud Bus)

Spring Cloud Bus connects distributed nodes with a lightweight message broker. It can be used to broadcast configuration file changes or communication between services, and can also be used for monito...

The simplest SpringCloud tutorial in history | Part 8: The Message Bus (Spring Cloud Bus)

forward from:   This article comes fromFang Zhipeng's blog springCloud Bus connects distributed nodes with lightweight message brokers. This can be used for broadcast profile changes or other adm...

The easiest SpringCloud tutorial in history | Part 8: Message Bus (Spring Cloud Bus)

Please indicate the source for reprinting:   This article is fromFang Zhipeng's blog Spring Cloud Bus connects distributed nodes and lightweight message brokers. This can be used for broadcast co...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top