一、準備工作
安裝RabbitMQ,安裝步驟參考:https://blog.csdn.net/pkxwyf/article/details/105155865
二、消息生產者
1、創建生產者SpringBoot工程,導入相關依賴座標
<!-- 引入父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<!-- 引入SpringBoot整合RabbitMQ依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- 引入測試依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
2、編寫配置文件:application.yml
# 配置RabbitMQ基本信息
spring:
rabbitmq:
host: 192.168.33.100
username: guest
password: guest
virtual-host: /
port: 5672
3、編寫啓動類:ProducerApplication
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
4、編寫配置類:RabbitMQConfig
在配置類定義交互機,隊列以及綁定交互機和隊列的關係
@Configuration
public class RabbitMQConfig {
// 交換機名稱
public static final String EXCHANGE_NAME = "xkp_topic_exchange";
// 隊列名稱
public static final String QUEUE_NAME = "xkp_queue";
// 1. 定義交換機
@Bean("xkpExchange")
public Exchange createExchange(){
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
// 2. 定義隊列
@Bean("xkpQueue")
public Queue createQueue(){
return QueueBuilder.durable(QUEUE_NAME).build();
}
// 3. 隊列與交換機綁定關係
@Bean
public Binding bindExchangeAndQueue(@Qualifier("xkpQueue") Queue queue,
@Qualifier("xkpExchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("xkp.#").noargs();
}
}
5、編寫測試類
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRabbitMQ {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 發送消息到MQ:發送字符串消息
*/
@Test
public void testSendStr(){
// 參數1:交換機名字
// 參數2:路由鍵
// 參數3:消息字符串
rabbitTemplate.convertAndSend(
RabbitMQConfig.EXCHANGE_NAME,
"xkp.news",
"hello rabbitmq......");
}
}
6、查看管理控制檯
三、消息消費者
1、創建消費者SpringBoot工程,導入相關依賴座標
<!-- 引入父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<!-- 引入SpringBoot整合RabbitMQ依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- 引入測試依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
2、編寫配置文件:application.yml
# 配置RabbitMQ基本信息
spring:
rabbitmq:
host: 192.168.33.100
username: guest
password: guest
virtual-host: /
port: 5672
3、編寫啓動類:ConsumerApplication
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
4、編寫監聽類監聽隊列的消息
方式1:使用@RabbitListener和@RabbitHandler實現消息監聽
@Component
// @RabbitListener:監聽指定隊列的消息(xkp_queue 是隊列名字)
@RabbitListener(queues = "xkp_queue")
public class RabbitMQConfig {
/**
@RabbitListener 和 @RabbitHandler 搭配使用
* @RabbitListener 可以標註在類上面,需配合 @RabbitHandler 註解一起使用
* @RabbitListener 標註在類上面表示當收到消息的時候,就交給 @RabbitHandler 的方法處理
* 具體使用哪個方法處理,根據 MessageConverter 轉換後的參數類型決定。
* 注意事項:方法參數類型和消息生成者發送消息類型要一致
*/
@RabbitHandler
public void handlerMessage(String message){
System.out.println("handlerMessage 接收到mq的消息:" + message);
}
}
方式2:使用@RabbitListener修飾方式方法實現消息監聽
@Component
public class RabbitMQConfig {
// bindings: 綁定交互機和隊列並指定路由鍵,如果交互機和隊列不存在則會自動創建
@RabbitListener(bindings = @QueueBinding(
exchange = @Exchange(value = "xkp_topic_exchange",type = "topic"),
value = @Queue(value = "xkp_queue",durable = "true"),
key = "xkp.#"
))
public void handlerMessage(String message) {
System.out.println("handlerMessage " + message);
}
}
以上兩種方式選擇其一即可,推薦方式2。
5、運行啓動類可以看到控制檯輸出