消息队列应用场景
消息中间件概述
RabbitMQ 概念
一个客户端只与消息中间件建立一条链接(长连接),一条链接里面有多个channel(虚拟链接,复用一条TCP连接)
虚拟主机是rabbitmq为了隔离不同类型客户端出现的概念。我们在开发过程中可以一个客户端一个虚拟主机。即一个rabbitmq里面可以有多个虚拟主机
虚拟主机:多个交换机构成虚拟主机,主要目的是隔离客户端的消息。比如当前消息队列的生产者有java端和php端,为了隔离开我们可以划分出两个虚拟主机。虚拟主机互相隔离,一台虚拟主机出现问题,不会影响到别的虚拟主机
虚拟主机是按照路径来划分的。
流程:无论是生产者往mq发送消息,还是消费者接受mq的消息,都会建立一条连接,所有的数据都会在连接里面开辟信道来进行收发。收发的消息分成两部分,消息头和消息体。消息头就是消息的属性信息,消息体就是消息的真正内容,消息里面还需要定义发给那个交换机,那个虚拟主机,消息头里面最重要就是route-key。发消息时,消息会来到rabbitmq里面指定的虚拟主机中,然后到达指定的交换机。交换机通过消息里面的route-key和绑定的队列的路由键,决定将消息发送给与交换机绑定的哪些队列中。如果队列放入的消息,监听队列的消费者就会得到队列里面的数据。一个客户端建立一条长连接的好处有,mq能实时的知道那个消费端断线了,从而及时的将队列里面的数据保存起来(停止出队),防止消息丢失。
docker 安装rabbitmq
docker run -d \
-p 5671:5671 \
-p 5672:5672 \
-p 4369:4369 \
-p 25672:25672 \
-p 15671:15671 \
-p 15672:15672 \
--restart=always --name rabbitmq rabbitmq:3-management
默认账户密码是guest
4369, 25672 (Erlang发现&集群端口)
5672, 5671 (AMQP端口)
15672 (web管理后台端口)
61613, 61614 (STOMP协议端口)
1883, 8883 (MQTT协议端口)
https://www.rabbitmq.com/networking.html
RabbitMQ的运行机制
一个交换机可以绑定多个队列,一个队列可以绑定多个交换机。交换机决定要按照什么绑定关系路由给哪个消息队列。
交换机的类型:direct、fanout(扇出)、topic、headers。direct和headers 是点对点的实现、fanout和topic是发布订阅模式的实现。而header性能低下,现在都只用direct、fanout、topic这三种不同类型的交换机。
发消息是将消息发送给虚拟主机里面的交换机,消费消息是监听队列。
direct:直接类型交换机。按照路由键精确匹配到一个队列。
fanout:广播类型交换机。消息会发送给交换机下面绑定的所有队列,不管路由键。
topic:发布订阅模式交换机。可根据路由键进行模糊匹配。#匹配0个或多个单词,* 匹配一个单词。
注意:路由键是有单词组成的,单词之间使用 . 分割,这一点是理解topic模糊匹配的关键。
测试一下三种不同类型的exchange
导出的配置(是json文件,xxx.json 导入即可)
{
"rabbit_version":"3.8.5","rabbitmq_version":"3.8.5","product_name":"RabbitMQ","product_version":"3.8.5","users":[{
"name":"guest","password_hash":"VLq+EgZQzd1z32LOZ83Onxc/MFCJcoMPf4jynGCdC9Aqvc6B","hashing_algorithm":"rabbit_password_hashing_sha256","tags":"administrator"}],"vhosts":[{
"name":"/"}],"permissions":[{
"user":"guest","vhost":"/","configure":".*","write":".*","read":".*"}],"topic_permissions":[],"parameters":[],"global_parameters":[{
"name":"cluster_name","value":"rabbit@7072bb66c978"},{
"name":"internal_cluster_id","value":"rabbitmq-cluster-id-9wl1WHjPlMGESsZsgSTH_g"}],"policies":[],"queues":[{
"name":"atguigu.news","vhost":"/","durable":true,"auto_delete":false,"arguments":{
"x-queue-type":"classic"}},{
"name":"atguigu","vhost":"/","durable":true,"auto_delete":false,"arguments":{
"x-queue-type":"classic"}},{
"name":"gulixueyuan.news","vhost":"/","durable":true,"auto_delete":false,"arguments":{
"x-queue-type":"classic"}},{
"name":"atguigu.emps","vhost":"/","durable":true,"auto_delete":false,"arguments":{
"x-queue-type":"classic"}}],"exchanges":[{
"name":"exchange.fanout","vhost":"/","type":"fanout","durable":true,"auto_delete":false,"internal":false,"arguments":{
}},{
"name":"exchange.topic","vhost":"/","type":"topic","durable":true,"auto_delete":false,"internal":false,"arguments":{
}},{
"name":"my.exchange.direct","vhost":"/","type":"direct","durable":true,"auto_delete":false,"internal":false,"arguments":{
}},{
"name":"exchange.direct","vhost":"/","type":"direct","durable":true,"auto_delete":false,"internal":false,"arguments":{
}}],"bindings":[{
"source":"exchange.direct","vhost":"/","destination":"atguigu","destination_type":"queue","routing_key":"atguigu","arguments":{
}},{
"source":"exchange.direct","vhost":"/","destination":"atguigu.emps","destination_type":"queue","routing_key":"atguigu.emps","arguments":{
}},{
"source":"exchange.direct","vhost":"/","destination":"atguigu.news","destination_type":"queue","routing_key":"atguigu.news","arguments":{
}},{
"source":"exchange.direct","vhost":"/","destination":"gulixueyuan.news","destination_type":"queue","routing_key":"gulixueyuan.news","arguments":{
}},{
"source":"exchange.fanout","vhost":"/","destination":"atguigu","destination_type":"queue","routing_key":"atguigu","arguments":{
}},{
"source":"exchange.fanout","vhost":"/","destination":"atguigu.emps","destination_type":"queue","routing_key":"atguigu.emps","arguments":{
}},{
"source":"exchange.fanout","vhost":"/","destination":"atguigu.news","destination_type":"queue","routing_key":"atguigu.news","arguments":{
}},{
"source":"exchange.fanout","vhost":