目录
- 一、安装RabbitMQ
- 二、SpringBoot整合RabbitMQ
- 1、创建SpringBoot项目的时候, 添加RabbitMQ的依赖
- 2、RabbitMQ的测试
- 3、监听消息队列中的内容
- 4、AmqpAdmin 管理RabbitMQ功能的组件
这里介绍的是Mac电脑的安装方法:
- 通过
brew
来安装, 如果你还没有安装过brew,那么请使用一下指令安装下这个mac平台里十分好用的包管理器
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- 下载安装RabbitMQ
// 更新brew资源
brew update
// 执行安装
brew install rabbitmq
- 显示下图表示安装成功
-
MQ的安装目录在
/usr/local/Cellar/rabbitmq
, 进入sbin目录, 使用命令./rabbitmq-server
来开启服务, 使用./rabbitmq stop
来关闭服务! -
http://localhost:15672
访问RabbitMQ的管理页面, 默认账号密码都是:guest
跳转到目录
使用RabbitTemplate发送和接收消息;
跳转到目录
RabbitAutoConfiguration
自动配置类- 在自动配置类中自动配置了连接工厂
ConnectionFactory
RabbitProperties
封装了RabbitMQ的配置spring: rabbitmq: host: 127.0.0.1 username: guest password: guest #port: 5672 #virtual-host: / # 默认就是 /, 可以不写
RabbitTemplate
给RabbitMQ发送和接收消息的AmqpAdmin
是RabbitMQ系统管理功能组件
public class Book {
private String name;
private String author;
public Book() {
}
public Book(String name, String author) {
this.name = name;
this.author = author;
}
// 省略getter/setter方法
}
@SpringBootTest
class Springboot06RabbitmqApplicationTests {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 测试:
* 1、单播(点对点)
*/
@Test
void contextLoads() {
//******发送消息******
// 这种方式,message消息需要我们自己构造一个, 定义消息体内容和消息头
// rabbitTemplate.send(exchage, routeKey, message);
// object默认当成消息体, 只需要传入要发送的对象,就会序列化发送给rabbitmq
// rabbitTemplate.convertAndSend(exchage, routeKey, object);
Map map = new HashMap();
map.put("msg", "这是第一个消息");
map.put("data", Arrays.asList("HelloWorld", 1998, true));
// 对象被默认序列化以后发送出去
rabbitTemplate.convertAndSend("exchange.direct", "zy.news", map);
}
/**
*
*/
@Test
void receive() {
//******接收消息******
// 从"zy.news"这个消息队列中接收消息
Object o = rabbitTemplate.receiveAndConvert("zy.news");
System.out.println(o.getClass());
System.out.println(o);
}
/*
广播:给所有的消息队列都发送消息
*/
@Test
void sendAllQueue() {
rabbitTemplate.convertAndSend("exchange-fanout", "", new Book("三国演义", "罗贯中"));
}
}
/**
* Description: 自定义序列化规则
*
* @author zygui
* @date 2020/4/25 19:13
*/
@Configuration
public class MyAMQPConfig {
@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
}
手动添加的三个交换机
成功将消息发送到zy.news
的消息队列中了
交换机和消息队列绑定在一起
从消息队列中取出消息, 取出后, 在该消息队列中就没有这条消息了!
跳转到目录 使用@RabbitListener
和@EnableRabbit
来监听
@EnableRabbit //开启RabbitMQ注解功能
@SpringBootApplication
public class Springboot06RabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot06RabbitmqApplication.class, args);
}
}
@Service
public class BookService {
// 用来监听`zy.news`消息队列发布的消息
@RabbitListener(queues = "zy.news")
//@RabbitListener(queues = {"zy.news", "zy", "zy.emps", "gzy.news"})
public void receive(Book book) {
System.out.println("收到消息:" + book);
}
}
4、AmqpAdmin 管理RabbitMQ功能的组件
跳转到目录
- 上面的测试,都是 存在交换机(Exchange), 消息队列(Queue)、绑定规则(Binding), 最开始在
RabbitMQ的管理网页中就已经手动配置好了
, 如何通过代码来创建上面的组件呢? 此时就用到了AmqpAdmin
;
@Autowired
private AmqpAdmin amqpAdmin;
@Test
void createExchange() {
// 创建路由器
amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
// 创建消息队列
amqpAdmin.declareQueue(new Queue("amqpadmin.queue", true));
// 创建绑定规则
amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE,
"amqpadmin.exchange", "zy.hhhh",null));
System.out.println("创建完成!");
}