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

What are the five development schemes of RabbitMq?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail what are the five development plans for RabbitMq. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. Installation

Windows install rabbitmq

When mac installs rabbitmq, it should be noted that when mac installs rabbitmq, you need to add sudo before starting the command, otherwise an error will be reported.

2. Rabbitmq development concept words

2.1 Producer (producer)

2.2 Consumer (Consumer)

2.3 Exchange (switch)

2.4 Queue (queue)

2.5 rountingKey (relationship between switch and queue)

3. RabbitMQ development plan

Of the 6 modes on the official website, you can click on this URL to show the 6 modes, and the 6th mode RPC remote call we do not need to use this mode, so we just need to pay attention to the first five.

Next, we will simply teach the simple use of rabbitmq.

Code description / * declare queue, five parameter lists. If you directly use the default channel.queueDeclare ("queue"), then other parameters will automatically default to the property, so generally we almost default to it * String queue queue name * boolean durable queue needs to be persisted, which means that when rabbitmq is restarted, if it is not persisted, then the queue will disappear. Default true * boolean exclusive if you want to create a queue that is only visible to you, do not allow other users to access RabbitMQ allows you to declare an Queue as exclusive, only visible to the connection (Connection) that first declared it, and will be automatically deleted when its connection is disconnected. Therefore, we generally do not need this operation in our development. The default false * boolean autoDelete message needs to be persisted, which means that when rabbitmq is restarted, if it is true, then the messages accepted during the shutdown will automatically disappear. The default parameter false * Map arguments is only used when using delay queues. That is, the related configuration of delay queue and other properties * method channel.queueDelete ("queue") is equivalent to channel.queueDeclare ("queue", true,false,false,null) * / channel.queueDeclare (QUEUE_NAME, false, null); / * * bind queue to switch * String queue queue name * String exchange switch name * String routingKey binding relationship such as big head son, small head father, their rountingKey is father and son * / channel.queueBind (QUEUE_NAME, EXCHANGE_NAME, "*. *"); 3.1 pom.xml

The version is optional, or any higher version of this blog is fine.

Com.rabbitmq amqp-client 3.4.1 org.slf4j slf4j-log4j12 1.7.7 org.apache.commons commons-lang3 3.3.2 org.springframework.amqp spring-rabbit 1.4.0.RELEASE3.2 tool class preparation import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.client.Connection;public class ConnectionUtil {public static Connection getConnection () throws Exception {/ / define connection factory ConnectionFactory factory = new ConnectionFactory () / / set service address factory.setHost ("localhost"); / / Port factory.setPort (5672); / / set account information, username, password, vhost factory.setVirtualHost ("/"); factory.setUsername ("guest"); factory.setPassword ("guest"); / / obtain connection Connection connection = factory.newConnection () through project Return connection;}} 3.3 simple Mode (simple)

Understanding: the rabbit server has only a single queue

Import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;public class Send {private final static String QUEUE_NAME = "test_queue"; public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); / / create channel Channel channel = connection.createChannel () from the connection / / declare (create) queue channel.queueDeclare (QUEUE_NAME, false, null); / / message content String message = "Hello World!"; channel.basicPublish ("", QUEUE_NAME, null, message.getBytes ()); System.out.println ("[x] Sent'" + message + ") / / close the channel and connection channel.close (); connection.close ();}} public class Recv {private final static String QUEUE_NAME = "mujiutian_queue"; public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); / / create channel Channel channel = connection.createChannel () / / create channel.queueDeclare (QUEUE_NAME, false, null); / / define the consumer QueueingConsumer consumer of the queue = new QueueingConsumer (channel); / / listen to the queue and send messages channel.basicConsume (QUEUE_NAME, true, consumer) / / get the message. The thread has been doing while (true) {QueueingConsumer.Delivery delivery = consumer.nextDelivery (); String message = new String (delivery.getBody ()); System.out.println ("get message" + message);}

First open the main function of rece so that you can execute send to send messages.

3.4Operation mode (work)

Understanding: rabbit server has an exchange (switch), there are two queues under the switch, a total of 100 messages were sent, A queue efficiency can grab 80 messages to the consumer, B queue can only grab 20 messages to the sender, their total is 100, for the working mode.

Execute first, execute the consumer main function first, and look at the result diagram:

Import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;public class Send {private final static String QUEUE_NAME = "test_queue_work"; public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); Channel channel = connection.createChannel (); / / declare queue channel.queueDeclare (QUEUE_NAME, false, null) For (int I = 0; I

< 100; i++) { // 消息内容 String message = "" + i; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); } channel.close(); connection.close(); }}import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.QueueingConsumer;public class Recv { private final static String QUEUE_NAME = "test_queue_work"; public static void main(String[] argv) throws Exception { // 获取到连接以及mq通道 Connection connection = ConnectionUtil.getConnection(); Channel channel = connection.createChannel(); // 声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 同一时刻服务器只会发一条消息给消费者 channel.basicQos(10); // 定义队列的消费者 QueueingConsumer consumer = new QueueingConsumer(channel); // 监听队列,手动返回完成 channel.basicConsume(QUEUE_NAME, false, consumer); // 获取消息 while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println("收到消息'" + message + "'"); // 返回确认状态 channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } }}import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.QueueingConsumer;public class Recv2 { private final static String QUEUE_NAME = "test_queue_work"; public static void main(String[] argv) throws Exception { // 获取到连接以及mq通道 Connection connection = ConnectionUtil.getConnection(); Channel channel = connection.createChannel(); // 声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 同一时刻服务器只会发一条消息给消费者 channel.basicQos(1); // 定义队列的消费者 QueueingConsumer consumer = new QueueingConsumer(channel); // 监听队列,手动返回完成状态 channel.basicConsume(QUEUE_NAME, false, consumer); // 获取消息 while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } }} 如图:

/ / the server will only send one message to the consumer channel.basicQos (1) at the same time

It has something to do with this, which is what we call the mode of work.

3.5 subscription Model (Publish/Subribe)

Understanding: all queues under the switch will receive messages from the same switch, similar to the qq group.

Import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;public class Send {private final static String EXCHANGE_NAME = "test_exchange_fanout"; public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); Channel channel = connection.createChannel (); / / declare exchange channel.exchangeDeclare (EXCHANGE_NAME, "fanout") / / message content String message = "Hello World!"; channel.basicPublish (EXCHANGE_NAME, "", null, message.getBytes ()); System.out.println ("[x] Sent'" + message + ""); channel.close (); connection.close ();} import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.QueueingConsumer Public class Recv {private final static String QUEUE_NAME = "test_queue_work"; private final static String EXCHANGE_NAME = "test_exchange_fanout"; public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); Channel channel = connection.createChannel (); / / declaration queue channel.queueDeclare (QUEUE_NAME, false, null) / bind the queue to the switch channel.queueBind (QUEUE_NAME, EXCHANGE_NAME, ""); / / the server will only send one message to the consumer channel.basicQos (1) at the same time; / / define the consumer QueueingConsumer consumer of the queue = new QueueingConsumer (channel) / / listen to the queue and manually return the completed channel.basicConsume (QUEUE_NAME, false, consumer); / / get the message while (true) {QueueingConsumer.Delivery delivery = consumer.nextDelivery (); String message = new String (delivery.getBody ()); System.out.println ("[x] Received'" + message + ") Thread.sleep (10); channel.basicAck (delivery.getEnvelope (). GetDeliveryTag (), false);}} import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.QueueingConsumer;public class Recv2 {private final static String QUEUE_NAME = "test_queue_work2"; private final static String EXCHANGE_NAME = "test_exchange_fanout" Public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); Channel channel = connection.createChannel (); / / declare queue channel.queueDeclare (QUEUE_NAME, false, null); / / bind queue to switch channel.queueBind (QUEUE_NAME, EXCHANGE_NAME, "") / / the server will only send one message to the consumer channel.basicQos (1) at the same time; / / the consumer who defines the queue QueueingConsumer consumer = new QueueingConsumer (channel); / / listens to the queue and manually returns the completion channel.basicConsume (QUEUE_NAME, false, consumer). / / get message while (true) {QueueingConsumer.Delivery delivery = consumer.nextDelivery (); String message = new String (delivery.getBody ()); System.out.println ("[x] Received'" + message + ""); Thread.sleep (10); channel.basicAck (delivery.getEnvelope () .getDeliveryTag (), false) 3.6Route pattern (Routing)

Understanding is: you are a person, if the gender is female, please enter the women's room, is male, please enter the men's room, like, a switch under multiple queues, according to the rountingKey to judge the message to be accepted

Import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;public class Send {private final static String EXCHANGE_NAME = "test_exchange_direct"; public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); Channel channel = connection.createChannel (); / / declare exchange channel.exchangeDeclare (EXCHANGE_NAME, "direct") / / message content String message = "Hello World!"; channel.basicPublish (EXCHANGE_NAME, "key2", null, message.getBytes ()); System.out.println ("[x] Sent'" + message + ""); channel.close (); connection.close ();} import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.QueueingConsumer Public class Recv {private final static String QUEUE_NAME = "test_queue_work"; private final static String EXCHANGE_NAME = "test_exchange_direct"; public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); Channel channel = connection.createChannel (); / / declaration queue channel.queueDeclare (QUEUE_NAME, false, null) / bind the queue to the switch channel.queueBind (QUEUE_NAME, EXCHANGE_NAME, "key"); / / the server will only send one message to the consumer channel.basicQos (1) at the same time; / / the consumer who defines the queue QueueingConsumer consumer = new QueueingConsumer (channel) / / listen to the queue and manually return the completed channel.basicConsume (QUEUE_NAME, false, consumer); / / get the message while (true) {QueueingConsumer.Delivery delivery = consumer.nextDelivery (); String message = new String (delivery.getBody ()); System.out.println ("[x] Received'" + message + ") Thread.sleep (10); channel.basicAck (delivery.getEnvelope (). GetDeliveryTag (), false);}} import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.QueueingConsumer;public class Recv2 {private final static String QUEUE_NAME = "test_queue_work2"; private final static String EXCHANGE_NAME = "test_exchange_direct" Public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); Channel channel = connection.createChannel (); / / declare queue channel.queueDeclare (QUEUE_NAME, false, null); / / bind queue to switch channel.queueBind (QUEUE_NAME, EXCHANGE_NAME, "key2") / / the server will only send one message to the consumer channel.basicQos (1) at the same time; / / the consumer who defines the queue QueueingConsumer consumer = new QueueingConsumer (channel); / / listens to the queue and manually returns the completion channel.basicConsume (QUEUE_NAME, false, consumer). / / get message while (true) {QueueingConsumer.Delivery delivery = consumer.nextDelivery (); String message = new String (delivery.getBody ()); System.out.println ("[x] Received'" + message + ""); Thread.sleep (10); channel.basicAck (delivery.getEnvelope () .getDeliveryTag (), false) } 3.7 wildcard mode (Topics)

The understanding is: path aas.# A queue accepts all paths, B queue accepts all queues aab.# under the path

Import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;public class Send {private final static String EXCHANGE_NAME = "test_exchange_topic"; public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); Channel channel = connection.createChannel (); / / declare exchange channel.exchangeDeclare (EXCHANGE_NAME, "topic") / / message content String message = "Hello World!"; channel.basicPublish (EXCHANGE_NAME, "key.1", null, message.getBytes ()); System.out.println ("[x] Sent'" + message + ""); channel.close (); connection.close ();} import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.QueueingConsumer Public class Recv {private final static String QUEUE_NAME = "test_queue_topic_work"; private final static String EXCHANGE_NAME = "test_exchange_topic"; public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); Channel channel = connection.createChannel (); / / declaration queue channel.queueDeclare (QUEUE_NAME, false, null) / bind the queue to the switch channel.queueBind (QUEUE_NAME, EXCHANGE_NAME, "key.*"); / / the server will only send one message to the consumer channel.basicQos (1) at the same time; / / the consumer who defines the queue QueueingConsumer consumer = new QueueingConsumer (channel) / / listen to the queue and manually return the completed channel.basicConsume (QUEUE_NAME, false, consumer); / / get the message while (true) {QueueingConsumer.Delivery delivery = consumer.nextDelivery (); String message = new String (delivery.getBody ()); System.out.println ("[x] Received'" + message + ") Thread.sleep (10); channel.basicAck (delivery.getEnvelope (). GetDeliveryTag (), false);}} import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.QueueingConsumer;public class Recv2 {private final static String QUEUE_NAME = "test_queue_topic_work2"; private final static String EXCHANGE_NAME = "test_exchange_topic" Public static void main (String [] argv) throws Exception {/ / get the connection and mq channel Connection connection = ConnectionUtil.getConnection (); Channel channel = connection.createChannel (); / / declare queue channel.queueDeclare (QUEUE_NAME, false, null); / / bind queue to switch channel.queueBind (QUEUE_NAME, EXCHANGE_NAME, "*. *") / / the server will only send one message to the consumer channel.basicQos (1) at the same time; / / the consumer who defines the queue QueueingConsumer consumer = new QueueingConsumer (channel); / / listens to the queue and manually returns the completion channel.basicConsume (QUEUE_NAME, false, consumer). / / get message while (true) {QueueingConsumer.Delivery delivery = consumer.nextDelivery (); String message = new String (delivery.getBody ()); System.out.println ("[x] Received'" + message + ""); Thread.sleep (10); channel.basicAck (delivery.getEnvelope () .getDeliveryTag (), false) } on the five RabbitMq development solutions are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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