RabbitMQ 基础教程 Routing(消息路由)

xiaoxiao2021-02-28  160

上篇文章中,我们构建了一个简单的日志系统。接下来,我们将丰富它:能够使用不同的severity来监听不同等级的log。比如我们希望只有error的log才保存到磁盘上。

一、Bindings(绑定)

上篇文章中我们是这么做的绑定:

channel.queueBind(queueName, EXCHANGE_NAME, "");

绑定其实就是关联了exchange和queue。或者这么说:queue对exchagne的内容感兴趣,exchange要把它的Message deliver到queue中。

实际上,绑定可以带routing_key 这个参数。其实这个参数的名称和basic_publish 的参数名是相同了。为了避免混淆,我们把它成为binding key。

使用一个key来创建binding :

channel.queueBind(queueName, EXCHANGE_NAME, "black");

对于fanout的exchange来说,这个参数是被忽略的。

二、Direct exchange

Direct exchange的路由算法非常简单:通过binding key的完全匹配,可以通过下图来说明。 exchange X和两个queue绑定在一起。Q1的binding key是orange。Q2的binding key是black和green。

当P publish key是orange时,exchange会把它放到Q1。如果是black或者green那么就会到Q2。其余的Message都会被丢弃。

三、 Multiple bindings

多个queue绑定同一个key是可以的。对于下图的例子,Q1和Q2都绑定了black。也就是说,对于routing key是black的Message,会被deliver到Q1和Q2。其余的Message都会被丢弃。

四、Emitting logs

首先是我们要创建一个direct的exchange:

channel.exchangeDeclare(EXCHANGE_NAME, "direct");

我们将使用log的severity作为routing key,这样Consumer可以针对不同severity的log进行不同的处理。

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

其中severity取值范围为:’info’, ‘warning’, ‘error’.

五、Subscribing

对于queue,我们需要绑定severity:

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

六、最终版本

EmitLogDirect.java

import com.rabbitmq.client.*; import java.io.IOException; public class EmitLogDirect { private static final String EXCHANGE_NAME = "direct_logs"; public static void main(String[] argv) throws java.io.IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); String severity = getSeverity(argv); String message = getMessage(argv); channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println(" [x] Sent '" + severity + "':'" + message + "'"); channel.close(); connection.close(); } //.. }

ReceiveLogsDirect.java

import com.rabbitmq.client.*; import java.io.IOException; public class ReceiveLogsDirect { private static final String EXCHANGE_NAME = "direct_logs"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); String queueName = channel.queueDeclare().getQueue(); if (argv.length < 1){ System.err.println("Usage: ReceiveLogsDirect [info] [warning] [error]"); System.exit(1); } for(String severity : argv){ channel.queueBind(queueName, EXCHANGE_NAME, severity); } System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'"); } }; channel.basicConsume(queueName, true, consumer); } }
转载请注明原文地址: https://www.6miu.com/read-18209.html

最新回复(0)