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 key
to 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 examplea.*.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
- 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
- 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)
- When Topic mode, a switch binding two queues, routing keys overlapped relation, the following code, then a routing key
topic.message
to send the message queuequeueMessage
, andqueueMessages
can 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. - 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-confirms
True, 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 lossspring.rabbitmq.publisher-returns
True, 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 downtimespring.rabbitmq.template.mandatory
When the above two is true, this must be equipped with true, otherwise it does not work above twospring.rabbitmq.listener.simple.acknowledge-mode
This ismanual
for 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
- Queue : queue name
- Durable : as a true representation of the data queue persisted to disk, you can prevent data loss mq downtime restart
- 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
- AutoDelete : true to automatically delete, when there is no connection to the Connection when the queue will be automatically deleted
- arguments The : This parameter is used to add some additional parameters, the following picture
- Such as adding
x-message-ttl
5000, then a message is not processed more than 5 seconds will timeout expires; x-expires
Set 120 000 indicates that the queue within two minutes of consumption were not being deleted;x-max-length
,x-max-length-bytes
The maximum number of bytes transferred and the length of data representedx-dead-letter-exchange
,x-dead-letter-routing-key
Represents 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 namex-dead-letter-exchange
of values, fill in the key route to comply with routing keys of the dead letter queuex-max-priority
Indicates 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 priorityx-queue-mode
When islazy
expressed 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.
- Such as adding
![](https://user-gold-cdn.xitu.io/2019/10/29/16e15a7f22c92c58?w=1259&h=347&f=jpeg&s=43349)
复制代码
1.7. Switch Properties
- exchange: Switch Name
- type: switch type
- durable: persistence, with queues
- autoDelete: whether to automatically delete the same queue
- internal: If true, indicates that the exchange can not be used to push message client, only for the exchange between the binding and exchange.
- 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 toalternate-exchange
a specified queue, forwarding the equivalent, and a parameters justinternal
fit, if the switch did not want to play the role of routing queues may be setinternal
to true, the messages are forwarded to thealternate-exchange
designated switch, the switch to route designated by the queue,- FIG follows:
exchange0
set thealternate-exchange
switchesexchange1
, the producer transmits the data to theexchange0
routing keys astest1
inexchange0
the routing less, it is forwarded to theexchange1
determined routing compliance, sent to the queuequeue1
- FIG follows:
![](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 fasterdisc
ram
1.9. Cases error
- As reported the following errors, that you must exist exclusive queue, which is set
exclusive
queue 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
exclusive
the property, then you do not create a new connection to access the same queue
ESOURCE_LOCKED - cannot obtain exclusive access to locked queue xxxxxx复制代码
Welcome to public concern number, learning progress together