rabbitmq
发布日期:2021-06-30 20:10:46 浏览次数:2 分类:技术文章

本文共 3762 字,大约阅读时间需要 12 分钟。

 

机制和ActiveMQ差不多,都是 起一个MQ服务,service发送消息,client 注册接收消息。

 

文档地址:http://www.rabbitmq.com/install-rpm.html

 

安装  erlang
 
yum install erlang

 

 

 

 

 

安装 rabbitmq-server
 
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc

下载 安装 rabbitmq-server-3.6.10-1.noarch.rpm

地址:https://github.com/rabbitmq/rabbitmq-server/releases/download/rabbitmq_v3_6_10/rabbitmq-server-3.6.10-1.el7.noarch.rpm
yum install rabbitmq-server-3.6.10-1.noarch.rpm
 
启动
 
service rabbitmq-server start

 

 

 

如果报错:

 

Redirecting to /bin/systemctl restart  rabbitmq-server.serviceJob for rabbitmq-server.service failed because the control process exited with error code. See "systemctl status rabbitmq-server.service" and "journalctl -xe" for details.

 

 

执行 journalctl -xe 查看详细信息,查看日志,看端口是否冲突;

 

他的端口有:

 

  • 4369: , a peer discovery service used by RabbitMQ nodes and CLI tools
  • 5672, 5671: used by AMQP 0-9-1 and 1.0 clients without and with TLS
  • 25672: used by Erlang distribution for inter-node and CLI tools communication and is allocated from a dynamic range (limited to a single port by default, computed as AMQP port + 20000). See  for details.
  • 15672:  clients and  (only if the  is enabled)
  • 61613, 61614:  without and with TLS (only if the  is enabled)
  • 1883, 8883: ( without and with TLS, if the  is enabled
  • 15674: STOMP-over-WebSockets clients (only if the  is enabled)
  • 15675: MQTT-over-WebSockets clients (only if the  is enabled)

 

 

 

启动成功后编写代码:

maven:

 

com.rabbitmq
amqp-client
4.1.1

发送端:Send.java 连接到RabbitMQ(此时服务需要启动),发送一条数据,然后退出。

 

 

 

[java]   
 
  1. package com.zhy.rabbit._01;  
  2.   
  3. import com.rabbitmq.client.Channel;  
  4. import com.rabbitmq.client.Connection;  
  5. import com.rabbitmq.client.ConnectionFactory;  
  6.   
  7. public class Send  
  8. {  
  9.     //队列名称  
  10.     private final static String QUEUE_NAME = "hello";  
  11.   
  12.     public static void main(String[] argv) throws java.io.IOException  
  13.     {  
  14.         /** 
  15.          * 创建连接连接到MabbitMQ 
  16.          */  
  17.         ConnectionFactory factory = new ConnectionFactory();  
  18.         //设置MabbitMQ所在主机ip或者主机名  
  19.         factory.setHost("localhost");  
  20.         //创建一个连接  
  21.         Connection connection = factory.newConnection();  
  22.         //创建一个频道  
  23.         Channel channel = connection.createChannel();  
  24.         //指定一个队列  
  25.         channel.queueDeclare(QUEUE_NAME, falsefalsefalsenull);  
  26.         //发送的消息  
  27.         String message = "hello world!";  
  28.         //往队列中发出一条消息  
  29.         channel.basicPublish("", QUEUE_NAME, null, message.getBytes());  
  30.         System.out.println(" [x] Sent '" + message + "'");  
  31.         //关闭频道和连接  
  32.         channel.close();  
  33.         connection.close();  
  34.      }  
  35. }  

值得注意的是队列只会在它不存在的时候创建,多次声明并不会重复创建。信息的内容是字节数组,也就意味着你可以传递任何数据。

 

 

 

 

接收端:Recv.java 不断等待服务器推送消息,然后在控制台输出。

 

[java]   
 
  1. package com.zhy.rabbit._01;  
  2.   
  3. import com.rabbitmq.client.Channel;  
  4. import com.rabbitmq.client.Connection;  
  5. import com.rabbitmq.client.ConnectionFactory;  
  6. import com.rabbitmq.client.QueueingConsumer;  
  7.   
  8. public class Recv  
  9. {  
  10.     //队列名称  
  11.     private final static String QUEUE_NAME = "hello";  
  12.   
  13.     public static void main(String[] argv) throws java.io.IOException,  
  14.             java.lang.InterruptedException  
  15.     {  
  16.         //打开连接和创建频道,与发送端一样  
  17.         ConnectionFactory factory = new ConnectionFactory();  
  18.         factory.setHost("localhost");  
  19.         Connection connection = factory.newConnection();  
  20.         Channel channel = connection.createChannel();  
  21.         //声明队列,主要为了防止消息接收者先运行此程序,队列还不存在时创建队列。  
  22.         channel.queueDeclare(QUEUE_NAME, falsefalsefalsenull);  
  23.         System.out.println(" [*] Waiting for messages. To exit press CTRL+C");  
  24.           
  25.         //创建队列消费者  
  26.         QueueingConsumer consumer = new QueueingConsumer(channel);  
  27.         //指定消费队列  
  28.         channel.basicConsume(QUEUE_NAME, true, consumer);  
  29.         while (true)  
  30.         {  
  31.             //nextDelivery是一个阻塞方法(内部实现其实是阻塞队列的take方法)  
  32.             QueueingConsumer.Delivery delivery = consumer.nextDelivery();  
  33.             String message = new String(delivery.getBody());  
  34.             System.out.println(" [x] Received '" + message + "'");  
  35.         }  
  36.   
  37.     }  
  38. }  

 

分别运行Send.java和Recv.java 顺序无所谓。前提RabbitMQ服务开启。

运行结果:

[x]Sent 'hello world!'

----------------------------------------

[*] Waiting for messages. To exitpress CTRL+C

[x] Received 'hello world!'

 

 

 

 

 

 

 

转载地址:https://liuhuiyao.blog.csdn.net/article/details/74132922 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:robots.txt SEO 与搜索引擎
下一篇:RabbitMq、ActiveMq、ZeroMq、kafka之间的比较,资料汇总

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年05月03日 11时16分37秒