[RocketMQ] Principle analysis: Producer message sending mode, distribution rules

 

1. Message sending mode

1.1 Synchronous transmission

The synchronous message sending mode means that after the message is sent, the producer will wait until the broker responds before continuing to send the next message

Insert picture description here

DefaultMQProducer producer = new DefaultMQProducer("my_producer_group");
producer.send(message); // 同步发送

The returned SendResult has four states: SEND OK (success), FLUSH_DISK_TIMEOUT (flash timeout), FLUSH_SLAVE_TIMEOUT (synchronization timeout), SLAVE_NOT_AVAILABLE,

1.2 Asynchronous sending

Asynchronous sending refers to the communication method in which the sender sends the data without waiting for the receiver to send back a response, and then sends the next data packet.

Insert picture description here

The asynchronous sending of MQ requires users to implement the asynchronous sending callback interface (SendCallback). After the message sender sends a message, it does not need to wait for a response from the server to return and send the second message. The sender receives the server response through the callback interface and processes the response result

DefaultMQProducer producer = new DefaultMQProducer("my_producer_group");
producer.send(msg, new SendCallback() {    
    @Override // 成功回调
    public void onSuccess(SendResult sendResult) {    
        System.out.printf("%s%n",sendResult);  
    }    
    @Override // 失败回调
    public void onException(Throwable throwable) {     
        throwable.printStackTrace();  
    } 
});

1.3 One-way transmission

One-way (Oneway) sending is characterized by the fact that the sender is only responsible for sending messages, not waiting for server response and no callback function is triggered, that is, only sending requests without waiting for responses. The most efficient

Insert picture description here

DefaultMQProducer producer = new DefaultMQProducer("my_producer_group");
producer.sendOneway(msg);

1.4 Sequential transmission

In Kafka, messages can be sent sequentially through a custom partition strategy. The realization principle is to send the same type of messages to the same partition.

In RocketMQ, a partition effect similar to kafka is achieved based on multiple Message Queues . If the amount of data to be sent and received by a topic is very large, a machine that can support parallel processing is needed to increase the processing speed. At this time, a topic can set up one or more Message Queues according to the needs. After Topic has multiple Message Queues, messages can be sent to each Message Queue in parallel, and consumers can also read and consume messages from multiple Message Queues in parallel.

2. Distribution rules

2.1 Default rules

By customizing the sending strategy to realize that messages are only sent to the same queue, because a topic will have multiple Message Queues, if the default configuration of the Producer is used, the Producer will send messages to each Message Queue in turn. When Consumer consumes messages, it will consume the assigned Message Queue according to the load balancing strategy.

2.2 Custom

Without specific settings, it is unknown to which Message Queue a certain message is sent and which Consumer consumes. If the business requires us to send messages to the specified Message Queue, for example, send the same type of messages to the same Message Queue. Is it possible to achieve the function of sequential messages?

Like kafka, rocketMQ also provides the function of message routing. We can customize the message distribution strategy and implement our own message distribution strategy by implementing MessageQueueSelector.

DefaultMQProducer producer = new DefaultMQProducer("my_producer_group");
SendResult sendResult = producer.send(msg, new MessageQueueSelector() {    
    @Override   
    public MessageQueue select(List<MessageQueue> list, Message message, Object o) {   
        int key=o.hashCode();     
        int size = list.size(); // 当前topic的消息队列数
        int index = key%size;   // 目标队列索引   
        return list.get(index); // list.get(0);   
    }
},"key_"+i);
  •  

Guess you like

Origin blog.csdn.net/qq_33762302/article/details/114859111