官方下载地址:http://www.erlang.org/downloads
注意:
在搭建RabbitMQ环境过程中,由于版本问题导致环境一直搭建不起来,以下是RabbitMQ与Erlang的版本对应关系
| RabbitMQ版本 | Erlang最低要求 | Erlang最高要求 |
|---|---|---|
| 3.7.7 - 3.7.12 | 20.3.x | 21.x |
| 3.7.0 - 3.7.6 | 19.3 | 20.3.x |
2 安装RabbitMQ可视化并启动官方下载地址:http://www.rabbitmq.com/download.html
1、使用命令:rabbitmq-plugins.bat enable rabbitmq_management
2、安装完成后关闭cmd窗口,重新打开,加载可视化插件
3、访问地址:http://localhost:15672/
4、默认用户名密码均为:guest
4 编写java代码 4.1 引入依赖1、访问:http://localhost:15672/
2、用户名密码均为guest
2、点击Queues
3、输入队列名点击添加
4、也可以自行添加用户,但是添加的用户许授权,否则无法登录
4.2 编写消息生产者com.rabbitmq amqp-client 5.9.0
消息队列名称需要自行替换为上步骤添加的队列名
public void producer() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setVirtualHost("/");
factory.setHost("127.0.0.1");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
String message = "这是发送的消息:" + new Date();
// HelloQueues为消息队列名称,需要在消息队列中存在
channel.basicPublish("", "HelloQueues", null, message.getBytes(StandardCharsets.UTF_8));
channel.close();
connection.close();
}
4.3 编写消息消费者
消息队列名称需要自行替换为上步骤添加的队列名
public void consumer() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setVirtualHost("/");
factory.setHost("127.0.0.1");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// HelloQueues为消息队列名称,需要在消息队列中存在
channel.basicConsume("HelloQueues", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("收到消息:" + new String(body, "utf-8"));
}
});
System.in.read();
channel.close();
connection.close();
}
4.4 测试



