Chapter 8 playing with RabbitMQ routing, theme mode, actual combat and summary
Episode 1 play with the routing mode and application scenario of RabbitMQ
Introduction: RabbitMQ routing mode and application scenario
-
What is the routing mode of rabbitmq
-
file: https://www.rabbitmq.com/tutorials/tutorial-four-java.html
-
The switch type is Direct
-
When the queue is bound to the switch, you need to specify a routing key (also known as the binding key)
-
The message producer needs to specify routingKey when sending messages to the switch
-
The switch forwards the message to the corresponding queue according to the routing key of the message
-
-
Example: log collection system ELK
-
A queue collects error information - "alarm"
-
One queue collects all information - daily use
-
Episode 2 actual combat of RabbitMQ routing mode code
Introduction: RabbitMQ routing mode code practice
-
Message producer
public class Send { private final static String EXCHANGE_NAME = "exchange_direct"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("10.211.55.13"); factory.setUsername("admin"); factory.setPassword("password"); factory.setVirtualHost("/xdclass1"); factory.setPort(5672); /** * The message producer does not need too much operation, but only needs to bind with the switch */ try (//Create connection connection = factory newConnection(); / / create channel = connection Createchannel()) {/ / bind the switch and connect it directly to channel. Exchangedeclare (exchange_name, builtineexchangetype. Direct); String error = "I'm the error log"; String info = "I'm the info log"; String debug = "I'm the debug log" ; channel.basicPublish(EXCHANGE_NAME, "errorRoutingKey", null, error.getBytes(StandardCharsets.UTF_8)); channel.basicPublish(EXCHANGE_NAME, "infoRoutingKey", null, info.getBytes(StandardCharsets.UTF_8)); channel.basicPublish(EXCHANGE_NAME, "debugRoutingKey", null, debug.getBytes(StandardCharsets.UTF_8)); System.out.println("message sent successfully"); } }} Message consumer (two nodes) public class Recv1 { private final static String EXCHANGE_NAME = "exchange_direct"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("10.211.55.13"); factory.setUsername("admin"); factory.setPassword("password"); factory.setVirtualHost("/xdclass1"); factory.setPort(5672); //Consumers generally don't add auto close connection connection = factory newConnection(); Channel channel = connection.createChannel(); / / bind the switch, fanout, that is, broadcast type: fanout channel exchangeDeclare(EXCHANGE_NAME,BuiltinExchangeType.DIRECT); / / get queue name string queuename = channel queueDeclare(). getQueue(); / / bind the queue and switch. Only one errorrutingkey channel is bound to another node queueBind(queueName,EXCHANGE_NAME,"errorRoutingKey"); channel.queueBind(queueName,EXCHANGE_NAME,"infoRoutingKey"); channel.queueBind(queueName,EXCHANGE_NAME,"debugRoutingKey"); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println(" [x] Received '" + message + "'"); }; / / automatic confirmation message: channel basicConsume(queueName, true, deliverCallback, consumerTag -> { }); }}
Episode 3 play with the topic theme wildcard mode and application scenario of RabbitMQ
Introduction: theme mode and application scenario of RabbitMQ
-
Background:
-
How to maintain routing key s if there are many services??
-
topic switch, which supports wildcard matching mode, is more powerful
-
This topic mode is basically used for work
-
-
What is the theme mode of rabbitmq
-
binding used when binding switches and queues Lu Youjian using wildcards
-
Producers need to use specific luyoujian when sending messages
-
Documentation https://www.rabbitmq.com/tutorials/tutorial-five-java.html
-
The switch is topic, which can realize the functions of publish and subscribe mode, fanout and routing mode Direct. It is more flexible and supports pattern matching, wildcards and so on
-
The switch forwards the same wildcard to the corresponding queue, * represents a word, # represents one or more words, and # is generally used as a wildcard, such as # Order will match info order ,sys.error.order, and * Order will only match info Order is used between Point to segment multiple words; If so, Then info order,error. Orders will match
-
be careful
-
-
Test, what are the following matching rules
quick.orange.rabbit will only match * orange.* And *. * rabbit, go to Q1 and q2lazy orange. Elephant will only match * orange.* And lazy. #, Go to Q1 and q2quick orange. Fox will only match * orange.*, Enter q1lazy brown. Fox will only match azy. #, Enter q2lazy pink. rabbit will only match lazy# And *. * rabbit, the same queue enters Q2 (the message will be sent only once) quick brown. Fox does not match. It will be discarded by default. You can listen for the secondary processing of lazy through callback orange. male. rabbit, it will only match lazy. #, Enter Q2
-
Example: log collection system
-
A queue collects all log information of the order system, order log.#
-
A queue collects all log information of all systems, # log
-
Episode 4 play with the topic wildcard mode code of RabbitMQ
Introduction: topic theme mode code of RabbitMQ
-
Example: log collection system
-
A queue collects the error log information of the order system, order log. error
-
One queue collects all levels of log information of all systems, * log. *
-
-
producer
public class Send { private final static String EXCHANGE_NAME = "exchange_topic"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("10.211.55.13"); factory.setUsername("admin"); factory.setPassword("password"); factory.setVirtualHost("/dev"); factory.setPort(5672); /** * The message producer does not need too much operation, but only needs to bind with the switch */ try (//Create connection connection = factory newConnection(); / / create channel = connection Createchannel()) {/ / bind the switch and connect it directly to channel.exchangeDeclare(EXCHANGE_NAME,BuiltinExchangeType.TOPIC); String error = "I'm the order error log"; String info = "I'm the order info log"; String debug = "I'm the product debug log" ; channel.basicPublish(EXCHANGE_NAME, "order.log.error", null, error.getBytes(StandardCharsets.UTF_8)); channel.basicPublish(EXCHANGE_NAME, "order.log.info", null, info.getBytes(StandardCharsets.UTF_8)); channel.basicPublish(EXCHANGE_NAME, "product.log.debug", null, debug.getBytes(StandardCharsets.UTF_8)); System.out.println("message sent successfully"); } }} Consumer (two) public class Recv1 { private final static String EXCHANGE_NAME = "exchange_topic"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("10.211.55.13"); factory.setUsername("admin"); factory.setPassword("password"); factory.setVirtualHost("/dev"); factory.setPort(5672); //Consumers generally don't add auto close connection connection = factory newConnection(); Channel channel = connection.createChannel(); / / bind switch channel exchangeDeclare(EXCHANGE_NAME,BuiltinExchangeType.TOPIC); / / get queue name string queuename = channel queueDeclare(). getQueue(); / / bind queue and switch. The first node / / channel queueBind(queueName,EXCHANGE_NAME,"order.log.error"); / / bind queue and switch, the second node / / channel queueBind(queueName,EXCHANGE_NAME,"*.log.*"); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println(" [x] Received '" + message + "'"); }; / / automatic confirmation message: channel basicConsume(queueName, true, deliverCallback, consumerTag -> { }); }}
Episode 5 Summary of various working modes of RabbitMQ
Introduction: summary of multiple working modes of RabbitMQ
-
Review and summary against the official website
-
https://www.rabbitmq.com/getstarted.html
-
-
Simple mode
-
For one production and one consumption, the default switch is used without specifying the switch
-
-
Work queue mode
-
One production and multiple consumption can have rotation training and fair strategy. The default switch is used instead of specifying the switch
-
-
Publish subscribe mode
-
The fanout type switch is bound to the queue through the switch without specifying the bound routing key. The producer sends messages to the switch, and the fanout switch forwards them directly without specifying the routingkey routing key
-
-
Routing mode
-
direct type switch, which is bound through the switch and queue, specifies the bound routing key. The producer sends the message to the switch. The switch forwards the message to the corresponding queue according to the routing key of the message. The message should specify the routingkey routing key
-
-
wildcard pattern
-
topic switch: bind the switch to the queue and specify the bound [wildcard routing key]. The producer sends the message to the switch. The switch forwards the message to the corresponding queue according to the routing key of the message. The message should specify the routingkey routing key
-