目錄
一、安裝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
二、SpringBoot整合RabbitMQ
使用RabbitTemplate發送和接收消息;
1、創建SpringBoot項目的時候, 添加RabbitMQ的依賴
2、RabbitMQ的測試
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<String, Object> 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
的消息隊列中了
交換機和消息隊列綁定在一起
從消息隊列中取出消息, 取出後, 在該消息隊列中就沒有這條消息了!
3、監聽消息隊列中的內容
跳轉到目錄
使用@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("創建完成!");
}