码迷,mamicode.com
首页 > 其他好文 > 详细

RabbitMQ学习和使用

时间:2016-05-18 19:28:41      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

RabbitMQ学习和使用


RabbitMQ介绍

MQ全称Message Queue 消息队列,RabbitMQ是基于AMQP(高级消息队列协议)实现的。消息队列通常用以应用之间相互通信,解决同步问题。MQ是典型的生产者消费者模型,RabbitMQ最常用的三种模式是点对点模式、发布订阅模式、广播模式。

RabbitMQ is a message-queueing software called a message broker or queue 
manager. Simply said; It is a software where queues can be defined, 
applications may connect to the queue and transfer a message onto it.

A message can include any kind of information. It could, for example, have 
information about a process/task that should start on another application 
(that could be on another server), or it could be just a simple text message. 
The queue-manager software stores the messages until a receiving application 
connects and takes a message off the queue. The receiving application then 
processes the message in an appropriate manner. 

技术分享


RabbitMQ应用场景

1)信息的发送者和接收者如何维持这个连接,如果一方的连接中断,这期间的数据如何方式丢失?(宕机处理)
2)如何降低发送者和接收者的耦合度?(解耦)
3)如何让Priority高的接收者先接到数据?
4)如何做到load balance?有效均衡接收者的负载?(负载均衡)
5)如何有效的将数据发送到相关的接收者?也就是说将接收者subscribe 不同的数据,如何做有效的filter。(路由)
6)如何做到可扩展,甚至将这个通信模块发到cluster上?
7)如何保证接收者接收到了完整,正确的数据?(稳定性)

总结:消息中间件主要的两个功能解耦和异步处理


RabbitMQ安装介绍

1、rabbitMQ是由erlang语言开发的,所以必须先有安装erlang,类似java安装
2、rabbitMQ是C/S模式的,所以安装rabbitMQ服务器,傻瓜安装默认端口5672
3、推荐最好也安装上rabbitMQ 的web管理界面(默认端口15672) 当然使用rabbitctl命令也可以

官方参考链接:http://www.rabbitmq.com/documentation.html


RabbitMQ基本概念和特性

RabbitMQ特性

feature highlights
1、Reliabliity(可靠性):
RabbitMQ offers a variety of features to let you trade off performance with reliability, including persistence, delivery acknowledgements, publisher confirms, and high availability.
2、Flexible Routing(灵活的路由机制):
Messages are routed through exchanges before arriving at queues. RabbitMQ features several built-in exchange types for typical routing logic. For more complex routing you can bind exchanges together or even write your own exchange type as a plugin.
3、Clustering(集群功能):
Several RabbitMQ servers on a local network can be clustered together, forming a single logical broker.
4、Highly Available Queues(高度可用队列):
Queues can be mirrored across several machines in a cluster, ensuring that even in the event of hardware failure your messages are safe.
5、Multi-protocol(多协议支持):
RabbitMQ supports messaging over a variety of messaging protocols.
6、Many Clients(多客户端支持):
There are RabbitMQ clients for almost any language you can think of.
7、Management UI(图形管理界面):
RabbitMQ ships with an easy-to use management UI that allows you to monitor and control every aspect of your message broker.
etc …


专业术语

Producer: Application that sends the messages 消息生产者

Consumer: Application that receives the Messages 消息消费者

Queue: Buffer that store messages 缓存消息的容器

Message: Information that is sent from the producer to a consumer through RabbitMQ.(消费者真正需要的消息数据)

Connection: A connection is a TCP connection between your application and the RabbitMQ broker.(一个tcp连接,相对比较耗资源)

Channel: A channel is a virtual connection inside a connection. When you are publishing or consuming messages or subscribing to a queue is it all done over a channel.(一个管道连接,是tcp连接内的连接,相对比较节约资源)

Exchange: Receives messages from producers and pushes them to queues depending on rules defined by the exchange type. In order to receive messages, a queue needs to be bound to at least one exchange.(消息路由,生产者发送消息并不是直接发送到队列中的而是先到指定方式路由中,然后由路由根据路由key绑定的队列发送到指定队列中。)

Binding: A binding is a link between a queue and an exchange.

Routing key: The routing key is a key that the exchange looks at to decide how to route the message to queues. The routing key is like an address for the message.

AMQP: AMQP (Advanced Message Queuing Protocol) is the protocol used by RabbitMQ for messaging.(高级消息队列协议,RabbitMQ是基于此协议实现的)

virtual host: A Virtual host provide a way to segregate applications using the same RabbitMQ instance. Different users can have different access privileges to different vhost and queues and exchanges can be created so they only exists in one vhost.(主要作用就是用来隔离的)

Users: It is possible to connect to RabbitMQ with a given username and password. Every user can be assigned permissions such as rights to read, write and configure privileges within the instance. Users can also be assigned permissions to specific virtual hosts.(RabbitMQ服务是基于C/S模式,通常连接都需要认证)


RabbitMQ开发使用

开发步骤

1、获得与RabbitMQ服务器的连接

 ConnectionFactory factory = new ConnectionFactory();//创建连接工厂对象
 factory.setHost(hostName);//指定主机名
 factory.setPort(portNumber);//指定端口号
 factory.setVirtualHost(virtualHost);//指定RabbitMQ服务器的虚拟主机
 factory.setUsername(username);//指定连接用户名
 factory.setPassword(password);//指定连接用户密码
 Connection conn = factory.newConnection();//创建连接

备注:由于这个Conneciton连接是一个TCP连接相对比较耗资源,通常我们都会在其内部创建管道连接来操作RabbitMQ.

//open a channel
 Channel channel = conn.createChannel();

2、声明路由和使用的路由类型以及声明队列名称

3、发送消息Publish或接受消息Consume


RabbitMQ Consumer 获取消息的两种方式(poll、subscribe)
1、Subscribe订阅方式
代码如下:

Consumer consumer = new DefaultConsumer(channel) {
        public void handleDelivery(String consumerTag, com.rabbitmq.client.Envelope envelope,
                com.rabbitmq.client.AMQP.BasicProperties properties, byte[] body) throws java.io.IOException {
            //使用以上的几个入参来处理信息
            // doSomething ...
        };
    };

2、poll API方式(轮询方式)

channel.basicGet(String queue, boolean autoAck);

备注:相比而言使用轮询方式效率更低效推荐使用订阅方式

RabbitMQ Start:http://www.rabbitmq.com/getstarted.html

备注实际开发中通常会结合spring来使用RabbitMQ使开发更简便。


RabbitMQ三种路由方式

一、Direct Exchange(直接路由)

任何发送到Direct Exchange的消息都会被转发到RouteKey中指定的Queue。

1.一般情况可以使用rabbitMQ自带的Exchange:"(该Exchange的名字为空字符串,下文称其为default Exchange)。
2.这种模式下不需要将Exchange进行任何绑定(binding)操作
3.消息传递时需要一个“RouteKey”,可以简单的理解为要发送到的队列名字。
4.如果vhost中不存在RouteKey中指定的队列名,则该消息会被抛弃。

技术分享



二、Fanout Exchange(广播路由)

任何发送到Fanout Exchange的消息都会被转发到与该Exchange绑定(Binding)的所有Queue上。

1.可以理解为路由表的模式
2.这种模式不需要RouteKey
3.这种模式需要提前将Exchange与Queue进行绑定,一个Exchange可以绑定多个Queue,一个Queue可以同多个Exchange进行绑定。
4.如果接受到消息的Exchange没有与任何Queue绑定,则消息会被抛弃

技术分享



三、Topic Exchange(主题订阅模式路由)

任何发送到Topic Exchange的消息都会被转发到所有关心RouteKey中指定话题的Queue上

1.这种模式较为复杂,简单来说,就是每个队列都有其关心的主题,所有的消息都带有一个“标题”(RouteKey),Exchange会将消息转发到所有关注主题能与RouteKey模糊匹配的队列。
2.这种模式需要RouteKey,也许要提前绑定Exchange与Queue。
3.在进行绑定时,要提供一个该队列关心的主题,如“#.log.#”表示该队列关心所有涉及log的消息(一个RouteKey为”MQ.log.error”的消息会被转发到该队列)。
4.“#”表示0个或若干个关键字,“*”表示一个关键字。如“log.*”能与“log.warn”匹配,无法与“log.warn.timeout”匹配;但是“log.#”能与上述两者匹配。
5.同样,如果Exchange没有发现能够与RouteKey匹配的Queue,则会抛弃此消息。

技术分享


参考

1、https://en.wikipedia.org/wiki/RabbitMQ
2、http://www.rabbitmq.com/
3、https://www.cloudamqp.com/blog/2015-05-18-part1-rabbitmq-for-beginners-what-is-rabbitmq.html
4、http://www.gaort.com/index.php/archives/366
5、http://blog.csdn.net/yangbutao/article/details/10395599
6、https://www.ibm.com/developerworks/cn/opensource/os-cn-rabbit-mq/
7、http://www.ibm.com/developerworks/cn/websphere/library/techarticles/0805_wenhongt/

RabbitMQ学习和使用

标签:

原文地址:http://blog.csdn.net/nicewuranran/article/details/51405116

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!