作者:钟罄石 | 来源:互联网 | 2023-07-23 20:15
RabbitMQ安装教程 RabbitMQ是流行的开源消息队列系统,用erlang语言开发。RabbitMQ是AMQP(高级消息队列协议)的标准实现。
第一步:安装Erlang环境 下载地址:http://www.erlang.org/download otp_win64_19.1.exe 下载exe执行文件直接安装,设置环境变量。
第二步:安装RabbitMQ 下载地址:http://www.rabbitmq.com/ rabbitmq-server-3.6.8.exe 下载exe执行文件直接安装即可。
执行命令:rabbitmq-plugins enable rabbitmq_management 开启网页支持 http://localhost:15672 默认用户:guest/guest 执行命令:rabbitmq-server start 开启服务
第三步:Myeclipse开发RabbitMQ实例—Topic Exchange模式 所需jar包在www.rabbitmq.com上下载的rabbitmq-java-client-bin-3.6.4
RabbitMQ提供了四种Exchange模式:fanout,direct,topic,header 。 本文使用topic作为示例。 所有发送到Topic Exchange的消息被转发到所有关心RouteKey中指定Topic的Queue上,Exchange 将RouteKey 和某Topic 进行模糊匹配。此时队列需要绑定一个Topic。可以使用通配符进行模糊匹配,符号“#”匹配一个或多个词,符号“”匹配不多不少一个词。因此“key.#”能够匹配到“key.1.2”,但是“key. ” 只会匹配到“key.1”。所以,Topic Exchange 使用非常灵活。
生产者:
package com;import com.rabbitmq.client.*;public class Sender {private final static String EXCHANGE_NAME = "test_exchange_topic" ;public static void main (String[] argv) throws Exception{ConnectionFactory factory = new ConnectionFactory();factory.setHost("127.0.0.1" );Connection connection =factory.newConnection();Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic" ); String message = "Hello World!" ; channel.basicPublish(EXCHANGE_NAME, "key.1.2" , null , message.getBytes()); System.out.println(" [x] Sent '" + message + "'" ); channel.close(); connection.close();} }
消费者1:
package com;import com.rabbitmq.client.*;public class RecvTest1 {private final static String QUEUE_NAME = "test_queue_topic_work1" ; private final static String EXCHANGE_NAME = "test_exchange_topic" ; public static void main (String[] argv) throws Exception { System.out.println(" recive 1 start." );ConnectionFactory factory = new ConnectionFactory();factory.setHost("127.0.0.1" );Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false , false , false , null ); channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "key.*" ); channel.basicQos(1 ); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, false , consumer); while (true ) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'" ); Thread.sleep(10 ); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false ); } } }
消费者2:
package comm;import com.rabbitmq.client.*;public class RecvTest2 {private final static String QUEUE_NAME = "test_queue_topic_work2" ; private final static String EXCHANGE_NAME = "test_exchange_topic" ; public static void main (String[] argv) throws Exception { System.out.println(" recive 2 start." );ConnectionFactory factory = new ConnectionFactory();factory.setHost("127.0.0.1" );Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false , false , false , null ); channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "key.#" ); channel.basicQos(1 ); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, false , consumer); while (true ) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'" ); Thread.sleep(10 ); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false ); } } }
注:要引入rabbitmq-client.jar的包 代码结构如下: 先启动生产者进程产生消息,再启动消费者进程,可在网页上看到路由test_exchange_topic,有两个待消费的队列test_queue_topic_work1,test_queue_topic_work2 此时先自动生产者在启动两个消费者:可以看到消费者1接收不到消息,消费者2可以接收到消息。当把生产者的key.1.2改为key.1时可以发现两个消费者都能接收到消息。
至此,RabbitMQ消息队列安装及在Myeclipse下的开发完成,相比UNIX的IPC的单机局限性,RabbitMQ的接口简单及在分布式上应用很大。