5.Rabbitmq Routing

举报
Nick Qiu 发表于 2021/03/25 23:32:05 2021/03/25
【摘要】 前言 Routing模式通过routingKey,决定发送和接收路径架构如下: image.png 代码 build.gradle plugins { id 'java' } group 'com.nick' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentr...

前言

Routing模式通过routingKey,决定发送和接收路径架构如下:

image.png

代码

  • build.gradle
plugins { id 'java'
}

group 'com.nick'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories { mavenCentral()
}

dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile group: 'com.rabbitmq', name: 'amqp-client', version: '5.2.0' compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25' compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25' compile group: 'org.projectlombok', name: 'lombok', version: '1.18.0'
}
  • ReceiveLogsDirect.java
package com.nick.Routing;

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("192.168.5.136"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, EXCHANGE_NAME, "ERROR"); channel.queueBind(queueName, EXCHANGE_NAME, "INFO"); 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); }
}
  • EmitDebugLogDirect.java
package com.nick.Routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.concurrent.TimeoutException;

public class EmitDebugLogDirect { private static final String EXCHANGE_NAME = "direct_logs"; public static void main(String[] argv) throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.5.136"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); String severity = "DEBUG"; String message = "debug message"; channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println(" [x] Sent '" + severity + "':'" + message + "'"); channel.close(); connection.close(); }
}
  • EmitErrorLogDirect.java
package com.nick.Routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.concurrent.TimeoutException;

public class EmitErrorLogDirect { private static final String EXCHANGE_NAME = "direct_logs"; public static void main(String[] argv) throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.5.136"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); String severity = "ERROR"; String message = "error message"; channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println(" [x] Sent '" + severity + "':'" + message + "'"); channel.close(); connection.close(); }
}

测试

  • 先执行接收;
  • 分别执行发送debug和error;
  • 在接收窗口上可以看到error消息有收到,debug接收不到;

完整代码

https://github.com/qiujiahong/rabbitmq_study

文章来源: www.jianshu.com,作者:Nick_4438,版权归原作者所有,如需转载,请联系作者。

原文链接:www.jianshu.com/p/23c01163d78b

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。