RabbitMQ官方文档使用指南阅读笔记

来源:互联网 发布:笑傲江湖捏脸数据 编辑:程序博客网 时间:2024/05/04 15:36

 

官方地址 https://www.rabbitmq.com

1"Hello World!"

介绍RabbitMQ的就像一个邮局,的工作流程
介绍了几个名词和及其在以后的图形表示
介绍了一个简单的消息发送和接受的类的过程和及其依赖的Jar
(一个生产者,一个消费者)

Work Queues

Round-robin dispatching

修改一种的Demo,启动多个消费者,查看queue的消息默认的循环分发机制

Message acknowledgment

当一个消费者处理消息时突然挂了,那么这部分已经从队列发出的消息,和没有处理完的消息
就会丢失,这里使用ack来回执缺认后再将消息从队列中删除,来保证消息消费过程的不丢失
这里要关闭自动确认机制.
channel.basicAck(envelope.getDeliveryTag(),false);

Message durability 消息持久化

队列的持久化和消息的持久化
定义队列时候就加入属性
发送消息时设置持久化属性
保证当rabbitMq宕机时消息也不会丢失,重新启动后从磁盘中获取消息给消费者

这里还有一个问题就是当消息从生产者到队列的过程中,可能会存在消息的丢失,
或者,到达队列后仅仅是存在缓存中,机器宕机也会将消息丢失,这时需要进行发送确认
publisher confirms.

Fair dispatch 公平分发


设置让一个队列不是一直工作,而另外一个队列一直清闲.使用分发机制
intprefetchCount=1;channel.basicQos(prefetchCount);
如果所有的消费者都是繁忙的,你需要设置一个监控,发现后处理它



Publish/Subscribe 发布和订阅

写一个日志系统的Demo,能将消息广播模式发送到多个队列,消费者,
一个消费者打印到屏幕,一个消费者写入磁盘

Exchanges 交换机

交换机根据exchange type.决定从生产者接收到的消息应该如何去处理
是发送给制定的queue还是多个queune还是丢弃

exchange types available: direct, topic, headers and fanout. 
定义:
channel.exchangeDeclare("logs","fanout");
channel.basicPublish("logs","",null,message.getBytes());
前面未指定交换机时使用的是默认的direct,默认使用队列的名称作为路由规则

Temporary queues 临时的queues

queue的名称是重要的对于消息的消费而言
但是不需要去并行消费时可以设置一个随机的queue来进行演示
StringqueueName=channel.queueDeclare().getQueue();

Bindings 绑定 

channel.queueBind(queueName,"logs","");
将队列和交换机及路由规则进行绑定,这里是广播模式,使用空字符

Routing

为了完成更加复杂的业务逻辑,我们需要不同的交换机类型,
如,重要的日志信息要记录到磁盘中全部的信息要被打印到控制台

Bindings

在绑定交换器和队列时,我们能使用一个bing key来进行规则设定
使用广播模式,这个key被忽略
channel.queueBind(queueName,EXCHANGE_NAME,"black");

Direct exchange  完全匹配模式交换机


当我们需要很细粒度的消息时,广播模式是不符合的,比如,我们需要将日志划分等级,进行不同的处理,如果使用广播模式,那么所有队列接受到相同的消息.这里我们使用direct来是实现


Multiple bindings  多个绑定

不同队列使用相同的绑定规则绑定到交换机实现广播相同的效果

Emitting logs

As always, we need to create an exchange first:

channel.exchangeDeclare(EXCHANGE_NAME,"direct");

And we're ready to send a message:

channel.basicPublish(EXCHANGE_NAME,severity,null,message.getBytes());

To simplify things we will assume that 'severity' can be one of 'info', 'warning', 'error'.


Subscribing

StringqueueName=channel.queueDeclare().getQueue();for(Stringseverity:argv){channel.queueBind(queueName,EXCHANGE_NAME,severity);}




Topics

Although using the direct exchange improved our system, it still has limitations - it can't do routing based on multiple criteria.

Topic exchange

A few valid routing key examples: "stock.usd.nyse", "nyse.vmw", "quick.orange.rabbit". There can be as many words in the routing key as you like, up to the limit of 255 bytes.

  • * (star) can substitute for exactly one word.
  • # (hash) can substitute for zero or more words.

Topic exchange

Topic exchange is powerful and can behave like other exchanges.

When a queue is bound with "#" (hash) binding key - it will receive all the messages, regardless of the routing key - like in fanout exchange.

When special characters "*" (star) and "#" (hash) aren't used in bindings, the topic exchange will behave just like a direct one.




Remote procedure call (RPC)

But what if we need to run a function on a remote computer and wait for the result? Well, that's a different story. This pattern is commonly known as Remote Procedure Call or RPC.
原理使用回调的queue来实现RPC,通过一个传递一个随意生成的queue给消费者,消费者拿到这个
queue后再将要响应的消息,生产出去即可

Client interface

Callback queue

callbackQueueName = channel.queueDeclare().getQueue();BasicProperties props = new BasicProperties                            .Builder()                            .replyTo(callbackQueueName)                            .build();channel.basicPublish("", "rpc_queue", props, message.getBytes());
关于属性设置

The AMQP protocol predefines a set of 14 properties that go with a message. Most of the properties are rarely used, with the exception of the following:

  • deliveryMode: Marks a message as persistent (with a value of 2) or transient (any other value). You may remember this property from the second tutorial.
  • contentType: Used to describe the mime-type of the encoding. For example for the often used JSON encoding it is a good practice to set this property to: application/json.
  • replyTo: Commonly used to name a callback queue.
  • correlationId: Useful to correlate RPC responses with requests.

Correlation Id

使用UUID来实现,或者别的自己实现的不重复的Key都可以


具体的图和代码官方都有可以去看:
https://www.rabbitmq.com/tutorials/tutorial-six-java.html
再推荐一个专栏,作者写的是汉语版本的官方文档,可以看看
http://blog.csdn.net/column/details/rabbitmq.html




0 0
原创粉丝点击