安装
- 由于rabbitMq需要erlang语言的支持,在安装rabbitMq之前需要安装erlang,执行命令
apt-get install erlang-nox
- 添加公钥
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -
- 更新软件包
apt-get update
- 安装 RabbitMQ
apt-get install rabbitmq-server
- 查看 RabbitMq状态
systemctl status rabbitmq-server #Active: active (running) 说明处于运行状态
# service rabbitmq-server status 用service指令也可以查看,同systemctl指令
- 启动、停止、重启
service rabbitmq-server start # 启动
service rabbitmq-server stop # 停止
service rabbitmq-server restart # 重启
- 启用 web端可视化操作界面,我们还需要配置Management Plugin插件
rabbitmq-plugins enable rabbitmq_management # 启用插件
service rabbitmq-server restart # 重启
- 查看用户
rabbitmqctl list_users
- 添加管理用户
rabbitmqctl add_user admin yourpassword
- 设置账号权限
![image]()
![image]()
入门使用
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
- 配置yml
server:
port: 9527
spring:
application:
name: rabbitmq-demo
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
- 创建生产者
package com.example.rabbitmqdemo.producer;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
- 创建消费者
package com.example.rabbitmqdemo.consumer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
- 创建配置类
package com.example.rabbitmqdemo.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
- 测试
package com.example.rabbitmqdemo.controller;
import com.example.rabbitmqdemo.producer.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
![image]()
![image]()