{"error":400,"message":"over quota"} RabbitMQ四种Exchange类型之Fanout (Java)——云诺说
云诺说 - 小程序开发 - 软件定制
当前位置: RabbitMQ > RabbitMQ四种Exchange类型之Fanout (Java)

RabbitMQ四种Exchange类型之Fanout (Java)

2019-02-15 02:47 分类:RabbitMQ 作者:云诺 阅读(1583)

版权声明:本文为博主原创文章,如果转载请给出原文链接:http://doofuu.com/article/4156137.html

RabbitMQ有四种Exchange类型,分别是Direct 、Fanout 、Topic、Headers 

Exchange特点:

  • Fanout 不处理路由键。你只需要简单的将队列绑定到交换机上。一个发送到该类型交换机的消息都会被广播到与该交换机绑定的所有队列上。如下图:


QQ截图20190704195800.png

  • Topic    将路由键和某模式进行匹配。此时队列需要绑定要一个模式上。符号“#”匹配一个或多个词,符号“*”只能匹配一个词。因此“logs.#”能够匹配到“logs.error”、“logs.info.toc”,但是“logs.*” 只能匹配到“logs.error”,不能匹配到“logs.info.toc” 。如下图:

QQ截图20190704195808.png

  • Direct   处理路由键,需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配。这是一个完整的匹配。如果一个队列绑定到该交换机上要求路由键为 “logs”,则只有路由键为“logs”的消息才被转发,不会转发路由键为"logs.error",只会转发路由键为"logs"。 如下图:

QQ截图20190704195815.png

  • Headers  不处理路由键,而是根据发送的消息内容中的headers属性进行匹配。在绑定Queue与Exchange时指定一组键值对;当消息发送到RabbitMQ时会取到该消息的headers与Exchange绑定时指定的键值对进行匹配;如果完全匹配则消息会路由到该队列,否则不会路由到该队列。headers属性是一个键值对,可以是Hashtable,键值对的值可以是任何类型。而fanout,direct,topic 的路由键都需要要字符串形式的。不过headers比较少用到,下面是headers的官方说明文档:

A headers exchange is designed to for routing on multiple attributes that are more easily expressed as message headers than a routing key. Headers exchanges ignore the routing key attribute. Instead, the attributes used for routing are taken from the headers attribute. A message is considered matching if the value of the header equals the value specified upon binding.

It is possible to bind a queue to a headers exchange using more than one header for matching. In this case, the broker needs one more piece of information from the application developer, namely, should it consider messages with any of the headers matching, or all of them? This is what the "x-match" binding argument is for. When the "x-match" argument is set to "any", just one matching header value is sufficient. Alternatively, setting "x-match" to "all" mandates that all the values must match.

Headers exchanges can be looked upon as "direct exchanges on steroids". Because they route based on header values, they can be used as direct exchanges where the routing key does not have to be a string; it could be an integer or a hash (dictionary) for example.

有了上面的介绍,下面就直接上代码吧!!

Consumer:

package fanout;
import java.io.IOException;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;


public class FanoutConsumer {


	private static final String EXCHANGE_NAME 	= "logs";
	
	public static void main(String[] argv) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		//rabbitmq监听IP
		factory.setHost("192.168.249.128");
		//rabbitmq监听默认端口
		factory.setPort(5672);
		//设置访问的用户
		factory.setUsername("test");
		factory.setPassword("test");
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();
		//声明路由名字和类型
		channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
		//获取随机队列名称
		String queueName = channel.queueDeclare().getQueue();
		//创建队列
		channel.queueDeclare(queueName, false, false, true, null);
		//把队列绑定到路由上
		channel.queueBind(queueName, EXCHANGE_NAME, "");


		System.out.println(" Waiting for msg....");


		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("Received msg is '" + message + "'");
			}
		};
		channel.basicConsume(queueName, true, consumer);
	}
}	private static final String EXCHANGE_NAME 	= "logs";
	
	public static void main(String[] argv) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		//rabbitmq监听IP
		factory.setHost("192.168.249.128");
		//rabbitmq监听默认端口
		factory.setPort(5672);
		//设置访问的用户
		factory.setUsername("test");
		factory.setPassword("test");
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();
		//声明路由名字和类型
		channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
		//获取随机队列名称
		String queueName = channel.queueDeclare().getQueue();
		//创建队列
		channel.queueDeclare(queueName, false, false, true, null);
		//把队列绑定到路由上
		channel.queueBind(queueName, EXCHANGE_NAME, "");


		System.out.println(" Waiting for msg....");


		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("Received msg is '" + message + "'");
			}
		};
		channel.basicConsume(queueName, true, consumer);
	}
}

Producer:

package fanout;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;public class FanoutProducer {	private static final String EXCHANGE_NAME = "logs";	public static void main(String[] argv) throws Exception{

		ConnectionFactory factory = new ConnectionFactory();		//rabbitmq监听IP
		factory.setHost("192.168.249.128");		//rabbitmq监听默认端口
		factory.setPort(5672);		//设置访问的用户
		factory.setUsername("test");
		factory.setPassword("test");
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();		//声明路由名字和类型
		channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
		String message = makeMessage(argv);
		
		channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
		System.out.println("Sent msg is '" + message + "'");

		channel.close();
		connection.close();
	}	private static String makeMessage(String[] strings){		if (strings.length < 1){			return "这是默认消息!!";
		}else{
			StringBuffer buffer= new StringBuffer();			for (int i = 0; i < strings.length; i++) {
				buffer.append(strings[i]);
			}			return buffer.toString();
		}
	}

}

运行Consumer:

QQ截图20190704195941.png

运行Producer:

QQ截图20190704195957.png

好了 , 就到这里~~

 

祝生活愉快!

「创作不易,你的支持是本站持续更新最大的动力!」

赞(0) 打赏

谢谢你请我喝奶茶*^_^*

支付宝
微信
1

谢谢你请我喝奶茶*^_^*

支付宝
微信

上一篇:

下一篇:

共有 0 条评论 - RabbitMQ四种Exchange类型之Fanout (Java)

博客简介

云诺说是一个致力于分享互联网编程技术交流、小程序开发、小程序源码分享、软件服务定制和生活记录的技术服务型学习博客网站。

微信 :LGY178888

职业 :小程序开发、软件定制

现居 :广东省-广州市-天河区

友情链接

欢迎与本博客交换友情链接,本博客对交换链接的网站没有要求。如果您是本博客的友情链接网站,在遇到网站运行问题时,可以随时联系,我们将免费提供技术类支持! 申请交换友链

站点统计

  • 文章总数:155 篇
  • 草稿数目:0 篇
  • 分类数目:14 个
  • 独立页面:165 个
  • 评论总数:0 条
  • 访问总量: 582988次
  • 最近更新:2024年04月19日