In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to realize Spring Cloud Bus message bus based on kafka". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to realize Spring Cloud Bus message bus based on kafka" can help you solve the problem.
What is a message bus
I believe that most readers have used a variety of message queues before, such as RabbitMQ, kafka, etc., message bus is similar to his concept. In the architecture of micro-service systems, we usually use lightweight message agents to build a common message topic to connect all the micro-services in the system, because the messages generated in this topic will be monitored and consumed by all instances. So we call them message buses. Each instance on the bus can easily broadcast messages that need to be known to other instances connected to the topic, such as configuration changes or other management operations.
Second, integrate message bus to realize automatic refresh of configuration.
In the last blog, spring cloud config, we implemented a distributed configuration center in the micro-service architecture, but there is a problem, that is, when we modify the configuration on git, we need to manually notify each service instance. This operation will kill people in projects with more instances. The sping cloud family will certainly consider and give a solution to this problem.
2.1 client-oriented basic architecture
When our system starts up according to the above figure, the three instances of serviceA in the figure will request Config Server to get the configuration. Config Server will obtain the configuration information from the Git repository and return it according to the rules of the application configuration.
At this point, if we want to modify the configuration of serviceA. First, go to the git server to modify the corresponding parameter values, but this will not trigger the property update of the serviceA instance. At this point, we send a post request to instance 3. At this time, instance 3 will send the refresh request to the message bus, and the message event will be obtained from the bus by instance 1 and instance 2 of serviceA, and their configuration information will be re-obtained from config server, thus realizing the dynamic update of configuration information.
2.2 Server-oriented architecture
In the previous architecture, the configuration update of the service needed to send a request through an instance of the specific service, and then trigger the configuration update to the entire service cluster. Although it can be sad, the result is that the application instance we specify will be different from other application instances in the cluster, which will increase the complexity of the cluster content and is not conducive to the future operation and maintenance work.
Third, using kafka to realize message bus 3.1 Spring Boot integrating kafka
You can refer to this article
If the spring boot version is 2.2.5, the kafka version uses 2.4.0.RELEASE.
3.2 achieve dynamic refresh
We use two projects of config in the last blog post to transform it.
3.2.1 server transformation
Increase dependence:
Org.springframework.cloud spring-cloud-starter-bus-kafka org.springframework.cloud spring-cloud-starter-stream-kafka org.springframework.cloud spring-cloud-bus org.springframework.boot spring-boot-starter-actuator
Add configuration:
Spring.kafka.bootstrap-servers=211.159.167.180:9092spring.kafka.consumer.group-id=test-consumer-groupspring.cloud.bus.enabled=truemanagement.endpoints.web.exposure.include= *
Attention should be paid to the configuration of management.endpoints.web.exposure.include= *
Note:
_ _ if it is yum,''need to add' 'single quotation marks *
Include:'* 'http://localhost:8769/actuator/bus-refresh refreshes all microservices
Include: 'refresh' http://localhost:8769/actuator/bus-refresh cannot access
3.2.2 client transformation
Increase dependence:
Org.springframework.cloud spring-cloud-starter-bus-kafka org.springframework.cloud spring-cloud-bus
Add configuration:
Management.endpoints.web.exposure.include= * spring.kafka.bootstrap-servers=211.159.167.180:9092spring.cloud.bus.enabled=true
In this way, we ok. After starting the project, when the configuration is modified, we send a POST request to the server: http://localhost:7071/actuator/bus-refresh
Dynamic refresh can be achieved.
3.3 specify refresh range
In the above example, we trigger the refresh of all service instances on the bus by requesting the / actuator/bus-refresh interface from the server, but in some special scenarios, we want to be able to refresh the configuration of a specific instance in the service. Spring Cloud Bus also supports this scenario. / actuator/bus-refreshdestination=customers:9000 provides a destination parameter to locate the specific application to be refreshed. When we call the interface with the destination parameter, the application instance on the bus will determine whether it is its own instance name based on the value of the destination attribute. If it matches, the configuration will be refreshed. If it does not match, the message will be ignored.
This is the end of the introduction of "how to implement Spring Cloud Bus message bus based on kafka". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.