#1. 获取镜像
docker pull rabbitmq:management
#2. 运行镜像
a.默认guest 用户,密码也是 guest
docker run -d --hostname my-rabbit --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq:management
b.设置用户名和密码
docker run -d --hostname my-rabbit --name rabbit -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=password -p 15672:15672 -p 5672:5672 rabbitmq:management
#3. 访问操作界面
http://localhost:15672/
Spring boot 集成 rabbitMQ
#配置rabbitMq 服务器
rabbitmq:
host: locahost
port:5672
username: guest
password: guest
#虚拟host 可以不设置,使用server默认host
#virtual-host: JCcccHost
#发送消息接口
package com.ruoyi.project.api.rabbitMQ;
import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;
import java.util.HashMap;import java.util.Map;
import java.util.UUID;
@Slf4j@RestController
@RequestMapping("/api/rabbit")
@Api(tags = "API: 消息队列测试")
public class SendMessageController {
@Autowired RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法 @ApiOperation("发送消息")
@GetMapping("/sendDirectMessage")
public String sendDirectMessage() {
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "test message, hello!";
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String,Object> map=new HashMap<>();
map.put("messageId",messageId);
map.put("messageData",messageData);
map.put("createTime",createTime); //将消息携带绑定键值:TestDirectRouting 发送到交换机
TestDirectExchange rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map); return "ok";
}
}
# 创建一个rabbit 配置文件
package com.ruoyi.framework.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configurationpublic class DirectRabbitConfig {
//队列 起名:TestDirectQueue
@Bean public Queue TestDirectQueue() {
return new Queue("TestDirectQueue",true); //true 是否持久
}
//Direct交换机 起名:TestDirectExchange
@Bean DirectExchange TestDirectExchange() {
return new DirectExchange("TestDirectExchange");
}
//绑定 将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
@Bean Binding bindingDirect() {
return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
}
}
#监听消息队列服务
package com.ruoyi.framework.config;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueuepublic class DirectReceiver {
@RabbitHandler public void process(Map testMessage) {
System.out.println("DirectReceiver消费者收到消息 : " + testMessage.toString());
}
}
#启动服务后,请求发送消息的接口,我这里使用的是项目中的swagger,也可以用postman等
#发送请求后swagger截图
#发送请求后控制台截图
#发送请求后rabbitMQ web页面截图