Problems encountered by spring boot integrating rabbitmq

1. Repeat the error connection reset

According to the meaning of connection reset, the server closes the connection for a certain reason, and the client is still sending. Combined with Baidu's judgment that the configured account does not have queue access rights, log in to the manager to modify the corresponding rights.

2. rabbitTemplate is injected as null

Using the instance of new sender directly in the controller for testing, it has always been found that rabbitTemplate has not been automatically injected. After comparison, the problem was found. Such a problem occurred during the integration of netty before, and more attention should be paid in the future.


3. Basic concepts of rabbitmq

Rabbitmq consists of two parts: queue and exchange, in which queue must be bound to exchange to be meaningful and routed. Exchange has three working modes or matching modes 

3.1 Directly match the corresponding convertAndSend("queuename", context); there is no exchange parameter in this mode, and the corresponding queue will be found from the default exchange of Default exchange binding (all queues are bound to this exchange by default)

3.2fanout multicast mode BindingBuilder.bind(AMessage).to(fanoutExchange); When the queue is directly bound to the exchange without parameters, all messages sent to the exchange will be routed to the queue. This mode is called fanout, corresponding to send convertAndSend("fanoutExchange","", context); The second parameter is meaningless in this case.

3.3 If the binding method of the model in 3.2 above is added with the with parameter, such as BindingBuilder.bind(queueMessage).to(exchange).with("topic.fliter") In this mode, the exchange will no longer route all messages to The corresponding queue is sent after matching by routekey.

From the perspective of springboot, in fact, there is no such concept. The above combination is realized through different combinations of send and bind.


4. The concept of virtual host

It is mainly used for multiple applications to directly manage permissions and name isolation.

5. Delay queue

In reality, there are always many needs, how long to do after processing an event, such as automatic confirmation of receipt 3 days after the order is signed, etc. rabbitmq solves this problem by using the deadLetter queue. The basic idea is to set a queue as the deadletter queue of an exchange. , then the message sent to the queue will not be processed, and finally the timeout route corresponding to the deadletter will be set out over time, and the timeout route can be processed according to the message.

Key code:

arguments.put("x-dead-letter-exchange", "dlexchange"); 设置为exchange的dead-letter.

arguments.put("x-dead-letter-routing-key", "ontimeout"); Set trigger timeout forwarding queue



send partial code

MessagePostProcessor processor = new MessagePostProcessor(){
@Override
public Message postProcessMessage(Message message) throws AmqpException {
message.getMessageProperties().setExpiration(times + "");
return message;
}
};

rabbitTemplate.convertAndSend("dlexchange","dlqueue", "testmessage", processor);

The message is received by the ontimeout queue after the test times elapses.

Reference https://blog.csdn.net/i_vic/article/details/72742277





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325954791&siteId=291194637
Recommended