Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure the center and message bus for java

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to configure the center and message bus in java". In the daily operation, I believe many people have doubts about how to configure the center and message bus in java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to configure the center and message bus in java". Next, please follow the editor to study!

Spring Cloud Bus

Spring cloud bus connects distributed nodes through a lightweight message broker. This can be used for broadcast state changes (such as configuration changes) or other message instructions. One of the core ideas of Spring bus is to extend spring boot applications through distributed initiators, and it can also be used to establish a communication channel between multiple applications. Currently, the only way to do this is to use the AMQP message broker as the channel, and the same feature settings (some depending on the channel settings) are in more channel documentation.

Spring cloud bus is translated as a message bus by many people in China, which is also quite vivid. We can understand it as managing and spreading messages in all distributed projects. In fact, the essence is to use the broadcast mechanism of MQ to spread messages in distributed systems. At present, the commonly used ones are Kafka and RabbitMQ. Many things can be done by using the mechanism of bus, among which the configuration center client refresh is one of the typical application scenarios. We use a diagram to describe the mechanism used by bus in the configuration center.

According to this figure, we can see the steps of using Spring Cloud Bus to make configuration updates:

1. Submitting the code triggers post to send bus/refresh to client A.

2. Client A receives a request to update the configuration from the server and sends it to Spring Cloud Bus

3. Spring Cloud bus receives the message and notifies other clients

4. Other clients receive the notification and request the server to get the latest configuration.

5. All clients get the latest configuration

Project example

Client spring-cloud-config-client transformation

1. Add dependencies

Org.springframework.cloud spring-cloud-starter-bus-amqp

Need to introduce more spring-cloud-starter-bus-amqp packages to increase support for message bus

2. Configuration file

# # when refreshing, disable security verification management.security.enabled=false## and enable message tracking spring.cloud.bus.trace.enabled=true spring.rabbitmq.host=192.168.9.89spring.rabbitmq.port=5672spring.rabbitmq.username=adminspring.rabbitmq.password=123456

The configuration file needs to be added to the relevant configuration of RebbitMq so that the client code transformation is complete.

3. Testing

Start the spring-cloud-eureka, spring-cloud-config-server, and spring-cloud-config-client projects in turn, and when you start the spring-cloud-config-client project, we will find that the startup log will output such a record.

2017-05-26 17 INFO 05 INFO 21924-[main] o.s.b.a.e.mvc.EndpointHandlerMapping: Mapped "{[/ bus/refresh], methods= [POST]}" onto public void org.springframework.cloud.bus.endpoint.RefreshBusEndpoint.refresh (java.lang.String)

It shows that the client already has the ability of message bus notification. in order to better simulate the effect of message bus, we change the port of the client spring-cloud-config-client project to 8003 and 8004 to start in turn, so that the test environment is ready. The backend effect of eureka after startup is as follows:

Let's first test whether the server and client are running correctly. Visit: http://localhost:8001/neo-config/dev and return the message:

{"name": "neo-config", "profiles": ["dev"], "label": null, "version": null, "state": null, "propertySources": [{"name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties"," "source": {"neo.hello": "hello im dev"}]}

It indicates that the configuration information is read normally on the server side.

Visit: http://localhost:8002/hello, http://localhost:8003/hello, http://localhost:8004/hello, and return: hello im dev. It means that the client has read the content on the server side.

Now let's update the value of neo.hello in neo-config-dev.properties to hello im dev update and submit it to the code base. Visit: http://localhost:8002/hello and still return hello im dev. We send a / bus/refresh post request to the client with port 8002. Under win, use the following command to simulate webhook.

Curl-X POST http://localhost:8002/bus/refresh

After the execution is completed, visit http://localhost:8002/hello, http://localhost:8003/hello, http://localhost:8004/hello, and return: hello im dev update. It shows that all three clients have the information about the latest configuration file, so we implement the example in figure 1.

At this point, the study on "how to configure the center and message bus in java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report