行业知识
如何在Windows中的Rabbitmq如何启动
2026-11-04 17:36:09
来源:互联网转载
1.如何在Windows中的Rabbitmq如何启动
1、进入rabbitmq的sbin目录,输入CMD,然后按下enter(回车键)
2、在命令窗口中输入: rabbitmq-plugins enable rabbitmq_management 启用管理服务。
3、在命令窗口中输入: rabbitmqctl start_app 启动服务。
查看是否启动成功
如果不行的话 就试试 进入rabbitmq的sbin目录,进入CMD 在命令提示符里输入:Rabbitmq-server 启动
出现这样的页面就证明启动成功了!
1、浏览器访问http://localhost:15672/
输入账号:guest(你的账号),密码:guest(你的密码) 点击登录
停止服务的命令: Rabbitmq-service stop
这样就成功了!
2.如何在java中使用Rabbitmq呢?
1.先添加依赖
org.springframework.boot spring-boot-starter-amqp
2.消息生成队列
生成消息队列方法一:
import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import org.junit.Test;import java.io.IOException;import java.util.concurrent.TimeoutException;/** * 消息队列原生的生成方法 */public class PublisherTest { @Test public void testSendMessage() throws IOException, TimeoutException { // 1.建立连接 ConnectionFactory factory = new ConnectionFactory(); // 1.1.设置连接参数,分别是:主机名、端口号、vhost、用户名、密码 factory.setHost("127.0.0.1"); factory.setPort(5672); factory.setVirtualHost("/"); factory.setUsername("guest"); // 你的rabbitMQ的账号 factory.setPassword("guest"); // 你的rabbitMQ的密码 // 1.2.建立连接 Connection connection = factory.newConnection(); // 2.创建通道Channel Channel channel = connection.createChannel(); // 3.创建队列 String queueName = "simple.queue"; channel.queueDeclare(queueName, false, false, false, null); // 4.发送消息 String message = "hello, rabbitmq!"; channel.basicPublish("", queueName, null, message.getBytes()); System.out.println("发送消息成功:【" + message + "】"); // 5.关闭通道和连接 channel.close(); connection.close(); }} 生成消息队列方法二:
@RunWith(SpringRunner.class)@SpringBootTestpublic class publisher{@Autowiredprivate RabbitTemplate rabbitTemplate;@Test// 发送消息队列public void testSendMessage2SimpleQueue(){// 队列名称String queueName = "simple.queue";// 消息String message = "你好啊, rabbitmq!";rabbitTemplate.convertAndSend(queueName,message);} 3.消息消费队列
@Componentpublic class consumer {// 获取那个消息队列 消息队列的名称@RabbitListener(queues = "simple.queue")public void listenSimpleQueueMessage(String message) {//System.out.println("spring 消费者接收到的消息是:"+message);}} 4.yml配置文件
spring: rabbitmq: host: 127.0.0.1 # rabbitMQ的ip地址 port: 5672 # 端口 username: guest # rabbitmq的账号 password: guest # rabbitmq的密码 virtual-host: /
上一篇:前端常用算法(一):防抖+节流
下一篇:怎么开个免费的插件服务器?