RabbitMQ practical application skills

1. RabbitMQ practical application skills

1.1 Introduction

As the project reason, to be followed in dealing more and RabbitMQ, so let's take RabbitMQ sort of application under combat skills, try to avoid future mining pit

1.2 Overview

RabbitMQ There are several important concepts: virtual hosts, switches, queues and bindings

  • Web Hosting: Web Hosting holds a set of switches, queues and bindings, we can control permissions from the virtual host-level granularity
  • Switch: Exchange for forwarding the message, it does not store the message, if no queue Queue bind to the Exchange , it drops directly off data sent by the producers.
    There are two important concepts associated switches: routing keys , message forwarding queue to which key decisions based routing
  • Binding: switches and queue is binding, it is many to many relationship , which means that multiple switches can be tied to the same queue, a switch can also be tied more queues

1.3. Switch

There are four types of mode switchDirect, topic, Headers and Fanout

1.3.1. Direct Exchage

Direct mode uses a default switch RabbitMQ, and easiest mode for relatively simple scene

As shown below, using the Direct mode, we need to create different queues, while the default switch through Routing keyto decide which queue is forwarded to the key value of the route, you can see, the route is the key bindings can specify multiple queue of

1.3.2. Topic Exchange

Topic wildcard matching pattern mainly based, also similar to the fuzzy matching, pattern matching, and when this switch routing keys match can forward the message to a specified queue

  • Routing keys as a string of a string, by a period ( .separated), such asa.b.c
  • ( ) *Represents a specified position word ( ) #represent zero or more words, for example a.*.b.#, a and b represent arbitrary fill the intermediate words, the back can be followed by b n words, such asa.x.b.c.d.e

Topic mode and Direct mode difference is that the switch needs its own specified routing key support fuzzy matching, for example:

rabbitTemplate.convertAndSend("topicExchange","a.x.b.d", " hello world!");复制代码

1.3.3. Headers Exchage

Headers are matched according to the rules, but it is not the key according to the route, there is a custom headers matching rule it will match key provided on the properties of the message headers, when a pair of these keys match or all, only messages will be delivered to the queue corresponding to the relatively low efficiency of this mode is generally not recommended

1.3.4. Fanout Exchange

Fanout is the famous broadcast mode, and it does not need to pipe routing keys , will bind its message to all the queue, even if the configuration of the routing keys will be ignored

1.4. Complication

  1. First, we Direct mode, a producer of a consumer case, it corresponds to a sender and a queue A reception, which is no doubt what to send what is received
  2. When the Direct mode, a message producers, consumers open the plurality of the plurality of the same Queue i.e., uniformly shared by a plurality of message at this time the consumer will not be repeated consumption (if normal ack)
  3. When Topic mode, a switch binding two queues, routing keys overlapped relation, the following code, then a routing key topic.messageto send the message queue queueMessage, and queueMessagescan receive the same message, i.e., Topic mode may be implemented similar to a broadcast mode the form , and even more flexible, it can be forwarded to a message is determined by the routing keys.
  4. Compared to Fanout mode, if we want to send a packet queue for the consumer, we need to specify different routing keys ; and you need to specify different modes Fanout switches and queue bindings, the actual use of the actual situation
@Configuration
public class TopicRabbitConfig {

    final static String message = "topic.message";
    final static String messages = "topic.messages";

    @Bean
    public Queue queueMessage() {
        return new Queue(TopicRabbitConfig.message);
    }

    @Bean
    public Queue queueMessages() {
        return new Queue(TopicRabbitConfig.messages);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("exchange");
    }

    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
    }

    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
    }
}复制代码

1.5. Springboot Configuration

Our common configuration is as follows

spring.rabbitmq.addresses=localhost:5672
spring.rabbitmq.username=user
spring.rabbitmq.password=123456
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=1000
##设置监听限制:最大10,默认5
spring.rabbitmq.listener.simple.concurrency=5
spring.rabbitmq.listener.simple.max-concurrency=10
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
spring.rabbitmq.template.mandatory=true
spring.rabbitmq.listener.simple.acknowledge-mode=manual复制代码

The last four configuration requires focus explanation:

  • spring.rabbitmq.publisher-confirmsTrue, indicates that the message is sent producers, the MQ the broker receives a message, send a receipt to confirm the reception, is not provided may lead to message loss
  • spring.rabbitmq.publisher-returnsTrue, indicating when a message can not reach the end of MQ Broker ,, use the listener to unreachable message for further processing, this is generally not a good job routing keys, or it may occur MQ downtime
  • spring.rabbitmq.template.mandatoryWhen the above two is true, this must be equipped with true, otherwise it does not work above two
  • spring.rabbitmq.listener.simple.acknowledge-modeThis is manualfor manual confirmation, the actual production should be set by hand, in order to ensure your business is processed, pay attention to the business of idempotency, repeated calls, manual confirmation code examples below
@Component
public class RabbitReceiver {

    
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue-1", 
            durable="true"),
            exchange = @Exchange(value = "exchange-1", 
            durable="true", 
            type= "topic", 
            ignoreDeclarationExceptions = "true"),
            key = "springboot.*"
            )
    )
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws Exception {
        System.err.println("--------------------------------------");
        System.err.println("消费端Payload: " + message.getPayload());
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        //手工ACK,获取deliveryTag
        channel.basicAck(deliveryTag, false);
    }
}复制代码

1.6. Queue properties

  1. Queue : queue name
  2. Durable : as a true representation of the data queue persisted to disk, you can prevent data loss mq downtime restart
  3. exclusive : exclusivity is true, only allows a current connection to access the queue, currently connected to not allow new connections into the otherwise an error, when disconnected current queue will be destroyed
  4. AutoDelete : true to automatically delete, when there is no connection to the Connection when the queue will be automatically deleted
  5. arguments The : This parameter is used to add some additional parameters, the following picture
    • Such as adding x-message-ttl5000, then a message is not processed more than 5 seconds will timeout expires;
    • x-expiresSet 120 000 indicates that the queue within two minutes of consumption were not being deleted;
    • x-max-length, x-max-length-bytesThe maximum number of bytes transferred and the length of data represented
    • x-dead-letter-exchange, x-dead-letter-routing-keyRepresents a dead letter dead-letter switches and routing, on the need to expire or processing queue attribute failure, the data will be stored and forwarded to the dead letter queue, and switches to create a common queue bindings, the switch to fill in the name x-dead-letter-exchangeof values, fill in the key route to comply with routing keys of the dead letter queue
    • x-max-priorityIndicates priority setting, the range from 0 to 255, only when the message is deposited, it makes sense that the priority , the higher the number the greater the priority
    • x-queue-modeWhen is lazyexpressed inert queue, the message after the concept was only introduced in 3.6.0, the default mode compared to inert queue mode will save producers generated directly to disk, which of course will increase the IO overhead, but is suitable to deal with a large number of situation message packed; because when a large number of messages accumulate, the memory is not enough storage, disk dump sends the message, the process is relatively time-consuming process and can not receive a new message. If you need to convert inert normal queue queue will need to delete the original queue, re-create a queue of inert binding.
    ![](https://user-gold-cdn.xitu.io/2019/10/29/16e15a7f22c92c58?w=1259&h=347&f=jpeg&s=43349)
复制代码

1.7. Switch Properties

  1. exchange: Switch Name
  2. type: switch type
  3. durable: persistence, with queues
  4. autoDelete: whether to automatically delete the same queue
  5. internal: If true, indicates that the exchange can not be used to push message client, only for the exchange between the binding and exchange.
  6. arguments: Additional parameters, only one alternate-exchange, represents when the producer sends a message to the switch, the switch can not route a queue, it will try this parameter specifies routing switch, if the routing keys match, then routed to alternate-exchangea specified queue, forwarding the equivalent, and a parameters just internalfit, if the switch did not want to play the role of routing queues may be set internalto true, the messages are forwarded to the alternate-exchangedesignated switch, the switch to route designated by the queue,
    • FIG follows: exchange0set the alternate-exchangeswitches exchange1, the producer transmits the data to the exchange0routing keys as test1in exchange0the routing less, it is forwarded to the exchange1determined routing compliance, sent to the queuequeue1
    ![](https://user-gold-cdn.xitu.io/2019/10/29/16e15a7f4c0a2b45?w=959&h=500&f=png&s=70920)

复制代码

1.8. Memory and Disk

In RabbitMQ management interface, when we can see the Nodes node cluster deployed in Info field may also be possible to indicate a disk storage or memory storage. In fact, when a cluster deployment, we want to at least one disk storage, which represents the switch, queues, binding, and other user metadata persistence saved to disk, reboot again RabbitMQ can also be restored to its original state, when only when a node must be a disk storage; and memory storage has its advantages, it is more efficient and fasterdiscram

1.9. Cases error

  • As reported the following errors, that you must exist exclusive queue, which is set exclusivequeue properties, due to the same connection different channels created can access the same queue, this time because of this exclusive property will get the resources are locked error, that is, the following mistake.
  • Thus, we can know, if you set the queue became exclusivethe property, then you do not create a new connection to access the same queue
ESOURCE_LOCKED - cannot obtain exclusive access to locked queue xxxxxx复制代码

Old beams tell Java

Welcome to public concern number, learning progress together

Guess you like

Origin juejin.im/post/5db7b850e51d4529e7305db9