这是消息模式的最后一种,通配符模式。总算是都写完了,真不容易。在通配符模式下可以按照 *
或者 #
进行匹配
*
只能替代一个词,例如:*.error.log
或者*.log
#
可以替代0个或更多的词,例如:#.log
通配符模式和路由模式代码很像,只需要修改发送方式就可。
使用 Java 来实现发送者和接收者
-
发送者
private static final String EXCHANGE_NAME = "logs_topic"; public static void main(String[] args) { try { ConnectionFactory factory = new ConnectionFactory(); // 设置参数 factory.setHost("192.168.3.64"); factory.setPort(5672); factory.setVirtualHost("/default_virtual_host"); factory.setUsername("admin"); factory.setPassword("admin"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // 创建交换机 此处不能用 FANOUT 作为交换机的类型了 ,需要换成 TOPIC channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, true, false, false, null); // 声明两个队列 channel.queueDeclare("log_topic_queue1", false, false, false, null); channel.queueDeclare("log_topic_queue2", false, false, false, null); // 绑定两个队列 (此处必须要要指定 routingkey) // 队列1只接收 *.info channel.queueBind("log_topic_queue1", EXCHANGE_NAME, "*.info"); // 队列2接收所有 channel.queueBind("log_topic_queue2", EXCHANGE_NAME, "#"); // String message = "[日志][支付信息][订单号:" + new Date().getTime() + "][支付ID:10]"; // 由于要使用 routing 模式发送所以就需要执行 routingkey channel.basicPublish(EXCHANGE_NAME, "member.comment.info", null, "用户评论了订单:10026".getBytes(StandardCharsets.UTF_8)); channel.basicPublish(EXCHANGE_NAME, "operate.info", null, "用户操作了一个按钮".getBytes(StandardCharsets.UTF_8)); // 释放资源 channel.close(); connection.close(); } catch (IOException | TimeoutException e) { e.printStackTrace(); } }
-
接收者1
private static final String EXCHANGE_NAME = "logs_topic"; public static void main(String[] args) { try { ConnectionFactory factory = new ConnectionFactory(); // 设置参数 factory.setHost("192.168.3.64"); factory.setPort(5672); factory.setVirtualHost("/default_virtual_host"); factory.setUsername("admin"); factory.setPassword("admin"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, true, false, false, null); String queue1Name = "log_topic_queue1"; DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), StandardCharsets.UTF_8); System.out.println("Received '" + message + "'"); }; channel.basicConsume(queue1Name, true, deliverCallback, consumerTag -> { }); } catch (IOException | TimeoutException e) { e.printStackTrace(); } }
-
接收者2
private static final String EXCHANGE_NAME = "logs_topic"; public static void main(String[] args) { try { ConnectionFactory factory = new ConnectionFactory(); // 设置参数 factory.setHost("192.168.3.64"); factory.setPort(5672); factory.setVirtualHost("/default_virtual_host"); factory.setUsername("admin"); factory.setPassword("admin"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, true, false, false, null); String queue1Name = "log_topic_queue2"; DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), StandardCharsets.UTF_8); System.out.println("Received '" + message + "'"); }; channel.basicConsume(queue1Name, true, deliverCallback, consumerTag -> { }); } catch (IOException | TimeoutException e) { e.printStackTrace(); } }