1. Home > programming >

Detailed explanation of five rabbitmq modes (including implementation code)

Detailed explanation of one or five modes

1. Simple mode (Queue mode)

When the producer sends a message to the switch, the switch sends the message to the queue according to the message attribute, and the consumer listens The binding queue realizes the message reception and consumption logic writing. In the simple mode, the emphasized one queue is only monitored and consumed by one consumer.

1.1 Structure

Producer: Generate a message and send it to the switch switch: According to Message attributes, send the message to the queue consumer: listen to the queue, and after the message is found, get the message to execute the consumption logic

1.2 application scenarios

The common application scenario is one send, one The structure of the connection
For example:

Single mailing of mobile phone text messages

2. Competition mode (Work mode)

The emphasis is also on the back-end queue Structure bound to consumers

2.1 Structure

Producer: send message to exchange switch: send message to queue consumer according to message attribute: multiple consumers, bind and listen to a queue at the same time, between Formed the effect of scramble for news

2.2 Application scenarios

  1. Grab the red envelope
  2. Resource allocation system

3. Routing mode (Route mode Direct)

Starting from the routing mode, what you care about is how the message arrives in the queue. The type of switch that the routing mode needs to use is the routing switch (direct)

3.1 Structure

  • Production side: send a message, process the message content in the message, and carry a routingkey
  • Exchange: receive the message, calculate the routingkey matching the backend queue according to the routingkey of the message
  • Queue: Storage Switch Messages sent
  • Consumer: Simple mode job competition

3.2 Application scenarios

  • SMS
  • Chat tool
  • Mailbox. .

Mobile phone number/email address can be routing key

4. Publish and subscribe mode (Pulish/Subscribe mode Fanout broadcast)

No A special switch for calculating routing

4.1 Structure

4.2 Application scenarios

  • Message push
  • Advertising

5. Topic mode (Topics mode Tpoic wildcard)

The routing key value is a multi-level path. China. Sichuan. Chengdu. Wuhou District

5.1 Structure

Production side: Carry the routing key and send the message to the switch

Queue: The binding switch is not the same as the route, not a specific one Routing key, but you can use * and # instead of a range
| * | string, which can only represent one level |
| --- | --- |
| # | multiple levels String|

Switch: According to the matching rules, the routing key will be sent to the queue correspondingly

Message routing key:

  • Beijing. Chaoyang District. Jiuxianqiao
  • Beijing.#: matches true
  • Shanghai. Pudong District.*: does not match false
  • Xinjiang.Urumqi.#

5.2 Application scenarios

Multi-level delivery of logistics sorting.

6. Complete structure

Second, code implementation

1. Create a SpringBoot project

1.1 Basic project information

1.2 Dependency Information

1.3 configuration file application.properties

 # application name spring  .application.name=springboot-demo # Actua  tor Web access port management.server.port=8801 management.endpoints.jmx.exposure.include=* management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always # Application service WEB access port  server.port=8801 ######################## RabbitMQ Configuration#################  ###### # RabbitMQ host spring.rabbitmq.host=127.0.0.1 # RabbitMQ virtual host spring.rabbitmq.virtual-host=demo # RabbitMQ service port spring.rabbitmq.port=5672 # RabbitMQ service user name spring.rabbitmq  .username=admin # RabbitMQ service password spring.rabbitmq.password=admin # RabbitMQ service release confirmation attribute configuration## NONE value is to disable the release confirmation mode, which is the default value ## CORRELATED value is to trigger a callback after successfully publishing a message to the exchange  Method## The SIMPLE value has two effects after testing. One effect is the same as the CORRELATED value, which will trigger the callback method. The second is to use the rabbitTemplate to call the waitForConfirms or waitForConfirmsOrDie method after the message is successfully published. Wait for the broker node to return the sending result.  In the next step of the logic, the point to note is that if the waitForConfirmsOrDie method returns false, the channel will be closed, and then the message cannot be sent to the broker; spring.rabbitmq.publisher-confirm-type=simple # RabbitMQ service is turned on and the message is sent to confirm spring.rabbitmq  .publisher-returns=true ######################### simple mode configuration###############  ######## # RabbitMQ service message receiving confirmation mode ## NONE: No confirmation ## AUTO: Automatic  Confirm## MANUAL: Manual confirmation spring.rabbitmq.listener.simple.acknowledge-mode=manual # Specify the minimum number of consumers spring.rabbitmq.listener.simple.cOncurrency=1 # Specify the maximum number of consumers spring.rabbitmq.listener  .simple.max-cOncurrency=1 # Enable support for retry spring.rabbitmq.listener.simple.retry.enabled=true

This article is published by www.internetweblist.com and does not represent the position of www.internetweblist.com/:http://www.internetweblist.com/programming/82412.html

Contact Us

Online consultation:click here to give a message