先声明本人遇到问题的解决方案:同时开放虚拟机的15672端口和5672端口
生产者代码如图所示
package Simple;
import Utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
//生产者
public class Productor {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws Exception {
//创建一个连接工厂
ConnectionFactory factory = new ConnectionFactory();
//此处xxx.xxx.xxx.xxx为你的虚拟机的ip地址
factory.setHost("xxx.xxx.xxx.xxx");
factory.setUsername("root");
factory.setPassword("root");
factory.setPort(5672);
//channel 实现了自动 close 接口 自动关闭 不需要显示关闭
try(Connection connection = factory.newConnection();Channel channel =
connection.createChannel()) {
/**
* 生成一个队列
* 1.队列名称
* 2.队列里面的消息是否持久化 默认消息存储在内存中
* 3.该队列是否只供一个消费者进行消费 是否进行共享 true 可以多个消费者消费
* 4.是否自动删除 最后一个消费者端开连接以后 该队列是否自动删除 true 自动删除
* 5.其他参数
*/
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
String message="hello world";
/**
* 发送一个消息
* 1.发送到那个交换机
* 2.路由的 key 是哪个
* 3.其他的参数信息
* 4.发送消息的消息体
*/
channel.basicPublish("",QUEUE_NAME,null,message.getBytes());
System.out.println("消息发送完毕");
}
}
}
消费者代码如图所示:
package Simple;
import Utils.RabbitMQUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
//消费者
public class Consumer {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
//此处xxx.xxx.xxx.xxx为你的虚拟机的ip地址
factory.setHost("xxx.xxx.xxx.xxx");
factory.setUsername("root");
factory.setPassword("root");
factory.setPort(5672);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
System.out.println("等待接收消息....");
//推送的消息如何进行消费的接口回调
DeliverCallback deliverCallback=(consumerTag,delivery)->{
String message= new String(delivery.getBody());
System.out.println(message);
};
//取消消费的一个回调接口 如在消费的时候队列被删除掉了
CancelCallback cancelCallback=(consumerTag)->{
System.out.println("消息消费被中断");
};
/**
* 消费者消费消息
* 1.消费哪个队列
* 2.消费成功之后是否要自动应答 true 代表自动应答 false 手动应答
* 3.消费者未成功消费的回调
*/
channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);
}
}
在虚拟机上开启RabbitMQ服务,别并且打开web后台管理
进入到rabbitmq-server的sbin目录下(我的安装在/opt/RabbitMQ/下)
cd /opt/RabbitMQ/rabbitmq/sbin/
启动服务
./rabbitmq-server start
启动成功如图所示:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RcSHmzB4-1649950931241)(C:\Users\ming’s\AppData\Roaming\Typora\typora-user-images\image-20220414232726945.png)]
运行生产者代码却发生错误;
"C:\Program Files\Java\jdk1.8.0_161\bin\java.exe" "-javaagent:D:\studyTool\IDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=53384:D:\studyTool\IDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar;D:\studyHomework\RabbitMQ\target\classes;D:\workspace\myProfiles\com\rabbitmq\amqp-client\5.4.1\amqp-client-5.4.1.jar;D:\workspace\myProfiles\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\workspace\myProfiles\commons-lang\commons-lang\2.6\commons-lang-2.6.jar" Simple.Productor
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:156)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1095)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1054)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1012)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1170)
at Simple.Productor.main(Productor.java:53)
Process finished with exit code 1
经过查阅网上的资料可能出现的原因:
1.ip地址错误
2.用户名错误
3.密码错误
(以上错误请自行排查)
4.端口错误(重点在此)
4.1使用rabbitmq连接后台管理的插件时虚拟机开放的是15672端口
4.2而在java远程连接的时候用到的是5672端口
所以在虚拟机上你需要同时开放15672端口和5672端口
(如果你直接关闭虚拟机防火墙也可)
下面看成功的实例:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MWIpTb9R-1649950931247)(D:\studyHomework\RabbitMQ\image-20220414233603994.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dGKPxmAr-1649950931248)(D:\studyHomework\RabbitMQ\image-20220414233615722.png)]