Exchanger (Exchange)
The switch is like a router, we first send the message to the exchanger, and then the switch based on the BIND key (binding key) and the producer sends the message when the routing key Routingkey,
Exchange types (fanout,direct,topic) deliver messages to the corresponding queue by Exchange type. (It is important to understand this concept, which is fully reflected in the code that follows).
RABBITMQ basic knowledge to view Message Queuing RABBITMQ basic knowledge
Queuing (queue)
The queue that holds the message.
bind (binding)
How does the exchanger know which queue to deliver this message to? This requires a binding. That is, using a binding key to bind a queue to a switch (Exchange), so the exchanger knows which queue to post the message to based on the routing key. (This is fully reflected in the following code)
Join RabbitMQ maven Dependency
Configuration
Configuration in the Application.yaml file
Rabbitmqconfig.java Configuration
@Configuration
public class Rabbitmqconfig {
Public final static String queue_name = "Spring-boot-queue";
Public final static String exchange_name = "Spring-boot-exchange";
Public final static String Binding_key = "spring.boot.key.#";
Create a queue
@Bean
Public queue Queue () {
return new Queue (queue_name);
}
Create a topic type of exchanger
@Bean
Public Topicexchange Exchange () {
return new Topicexchange (Exchange_name);
}
To bind a queue to a switch (Exchange) using the routing key (Routingkey)
@Bean
Public binding binding (queue queue, Topicexchange Exchange) {
return Bindingbuilder.bind (Queue). to (Exchange). with (Binding_key);
}
}
Note: The above configuration is Topicexchange
In real business, multiple queues and binding can be configured to meet demand.
producers
The Convertandsend method of calling Rabbittemplate directly is possible. As you can see from the code below, instead of sending the message directly to the queue, we send it to the exchanger, which then posts our message to the corresponding queue based on the routing key.
@RestController
public class Producercontroller {
@Autowired
Private Rabbittemplate rabbittemplate;
@GetMapping ("/sendmessage")
Public String SendMessage () {
New Thread ((), {
for (int i = 0; i <; i++) {
String value = new DateTime (). toString ("Yyyy-mm-dd HH:mm:ss");
Console.log ("Send Message {}", value);
Rabbittemplate.convertandsend (Rabbitmqconfig.exchange_name, Rabbitmqconfig.routing_key, value);
}
}). Start ();
return "OK";
}
}
Consumer
The consumer is also very simple, only need the corresponding method to add @RabbitListener annotations, specify the queue name to listen to.
Run the project
Run the project, then open the browser and enter Http://localhost:9999/sendMessage (specific address according to the server). In the console you can see the producers are constantly sending messages, consumers continue to consume messages.
Control Desk
Open the RabbitMQ Web console, and you can see the switches and queues that we have just configured in the code, as well as binding information.
View the details of the exchanger
View queues
This article transferred from: https://www.toutiao.com/i6578064914143773187/
RABBITMQ basic components and Springboot integration RABBITMQ Simple Example