windows下安装rabbitMQ

官网地址:https://www.rabbitmq.com/download.html


1.docker安装方式,这种最简单,但需要一个docker环境 。

windows下安装docker,先在https://github.com/docker/toolbox/releases下载dockertoolbox。它会附带一个VirtualBox,而有的电脑(实体机)BIOS,没有开虚拟模式的支持。需要在Adwance->CPU->Virturl之类的菜单开启。

安装好后,用以下命令就可以了。

docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
2.实体机安装。

a.在https://www.rabbitmq.com/which-erlang.html找到erl的版本(根据想安装的rabbitmq版本),如下:

进入https://www.erlang.org/downloads 下载相应的erlang包,安装好。

再回到https://www.rabbitmq.com/install-windows-manual.html 下载rabbitmq,安装好。


b.进入rabbitmq安装目录sbin文件夹,执行:

rabbitmq-plugins enable rabbitmq_management

用rabbitmqctl.bat status查看rabbitmq状态

c.启动rabbitmq

rabbitmq-server.bat

d.停止rabbitmq

rabbitmqctl.bat stop


启动好后是否可以用java远程访问了?还差点,在安装机浏览器上进入url:   
127.0.0.1:15672
用户名密码是guest/guest

进入菜单:Admin->Add a user

输入用户名密码添加一个用户。

再进入菜单:Admin->Virtual Host ->点name:"/" -> Permissions 选择刚建的用户名.如下图:

这样就加好了一个用户,并授权了。

下面开始开发Java客户端,发送端与接收端。

1.maven的环境准备:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>net.highersoft</groupId>
	<artifactId>rabbitmq</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>com.rabbitmq</groupId>
			<artifactId>amqp-client</artifactId>
			<version>5.9.0</version>
		</dependency>
	</dependencies>
</project>
2.发送端:

package net.highersoft.rabbitmq;

import java.nio.charset.StandardCharsets;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Send {


    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.126.217");
        factory.setUsername("cz");
        factory.setPassword("**");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Hello World!";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + message + "'");
        }
    }
}
接收端:

package net.highersoft.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

public class Recv {


    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.126.217");
        factory.setUsername("cz");
        factory.setPassword("**");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        };
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
    }
}


好了,可以测试了。

文/程忠 浏览次数:0次   2020-09-25 19:27:55

相关阅读


评论: