RabbitMQ four ways to enhance the reliability of the data transmission

Foreword

Although there are RabbitMQ queue messages, etc. and some persistent settings, but this one is in fact bare just not able to guarantee the reliability of data, let's ask the question:

(1) RabbitMQ producers do not know if I posted a message has reached the server right thing to do, the situation if it occurred in the middle network anomaly? The message is bound to lose!

(2) RabbitMQ queue if no persistent metadata after heavy RabbitMQ server queue will be lost, the message will naturally be lost!

(3) RabbitMQ if consumers set up automatic confirmation that autoAck is true, then no matter what happened consumers, the message is automatically removed from the queue, in fact, consumers may hang up, the message is bound to lose!

(4) RabbitMQ message in the queue if there is no match, then the message will be lost!

In fact, this article is a combination of the above four aspects to explain the main reference "RabbitMQ combat Guide" (PDF e-book may need to comment or private letter I), from which this article is also shot, another may recognize some of the concepts of RabbitMQ can I refer to the two blog posts understanding RabbitMQ switch model , how RabbitMQ is running?

 


 

 

First, the parameter setting mandotory, AE backup switch

For the first (4) questions in the preface, we can set the parameters mandotory AE backup switch to solve

1, mandotory parameters

  1) When true, the switch could not find a qualifying queue according to their type and routing key and the RabbitMQ will call Basic.Return command returns a message to the producer, the message will not be lost

  2) When is false, the message will be discarded.

  . 3) RabbitMQ added ReturnLisener listener listening through addReturnListener acquisition is not properly routed to the appropriate message queue .

channel.basicPublish(EXCHANGE NAME, "", true, 
MessageProperties.PERSISTENT_TEXT_PLAIN, 
"mandatory test".getBytes()); 
channel.addReturnListener(new ReturnListener(){ 
  public void handleReturn(int replyCode, String replyText, 
                String exchange, String routingKey, 
                AMQP.BasicProperties basicProperties, 
                byte[] body) throws IOException { 
    String message = new String(body); 
    System.out.println("Basic.Return 返回的结果是: " + message);
  }
}); .

 

2, AE backup switch

  Alternate Exchange, referred to as AE, does not set mandatory parameters, the message will be lost, setting mandatory parameters, then you need to add ReturnListner listeners, increasing the complexity of code, if you do not want to add code do not want the message is lost, the use of AE, will not be routing the message stored in the RabbitMQ. When used in conjunction with a mandatory parameter AE, mandatory will fail. Before introducing AE, RabbitMQ also recognized for the message TTL expiration and set an expiration time TTL setting queue

2.1 TTL expiration time set

  Queue may be provided for setting the TTL TTL message, wherein the message is provided for advanced applications often TTL dead letter queue, the queue delay and the like.

  1) Set message TTL

  Set TTL expiration time was general, there are two: First, through the queue properties, all of the messages in the queue to set the same TTL. Two of the message itself is separately provided, each different message TTL. If you use the time together, whichever is smaller TTL, TTL time when more than once set, will become a "dead letter."

  Method 1: For each message set the TTL is achieved by increasing the expiration of attribute parameters, not like Second way as to scan the entire queue and then determine whether or not expired, only if the message is about to be consumed and then determine whether to delete expired, that is, even if the message has expired, but not necessarily immediately be deleted!

AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder(); 
// 持久化消息
builder deliveryMode(2);
// 设置 TTL=60000ms
builder expiration( 60000 ); 
AMQP.BasicProperties properties = builder. build(); 
channel.basicPublish(exchangeName, routingKey, mandatory, properties, "ttlTestMessage".getBytes());

  Second way: set the message queue TTL property by increasing x-message-ttl parameters to achieve, just need to scan the entire head of the queue to delete it immediately, that is, the message once expired will be deleted!

Map<String, Object> argss = new HashMap<String , Object>(); 
argss.put("x-message-ttl", 6000); 
channel.queueDeclare(queueName, durable, exclusive, autoDelete, argss) ;

 

  2) Set the queue TTL

  In the queue by adding parameters x-message-ttl parameters to achieve , set the time before the queue are automatically deleted in the state not in use , note the use of state of the queue, the message is not whether the state of consumption

  Set ttl = 30min how fast the queue, a time to RabbitMQ will ensure that the queue is deleted, but will not guarantee the speed deleted.

Map<String, Object> args = new HashMap<String, Object>{); 
args.put("x-expires", 1800000);
channel.queueDeclare("myqueue", false, false, false, args);

 

 

Use 2.2 AE backup exchanger

  When the switch statement, adding alternate-exchange parameters achieved, or achieved through policies. The former high priority. From the perspective of the code requires the following three steps, specific code as follows:

Map<String, Object> args = new HashMap<String, Object>(); 
args.put("a1ternate-exchange", "myAe"); 
channe1.exchangeDec1are("norma1Exchange", "direct", true, fa1se, args); 
channe1.exchangeDec1are("myAe", "fanout", true, fa1se, nu11) ; 
channe1.queueDec1are( "norma1Queue", true, fa1se, fa1se, nu11); 
channe1.queueB nd("norma1Queue", "norma1Exchange", "norma1Key"); 
channe1.queueDec1are("unroutedQueue", true, fa1se, fa1se, nu11);

  1) direct type declared normalExchange exchanger, of the type fanout myAe backup switch; and a backup switch is normalExchange myAe (recommended fanout backup switch type switches)

  2) Statement normalQueue queue, queue unrouteQueue declaration;

  3) bound by routing keys normalKey normalExchange and normalQueue, NA binding unrouteQueue routing keys and myAe

 

        

  

Guess you like

Origin www.cnblogs.com/jian0110/p/10416116.html