Rabbit學習---SpringBoot整合RabbitMQ
SpringBoot整合RabbitMQ
我這裡只是簡單的測試RabbitMQ四種模型,並沒有設計太深入的知識點
一. 建立SpringBoot專案匯入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
二. 配置檔案application.yml
spring:
application:
name: rabbitmq-springboot
rabbitmq:
host: 192.168.153.180
username: /ems
password: 123
virtual-host: /ems
port: 5672
三. 第一種hello world模型使用
RabbitTemplate 用來簡化操作 使用時候直接在專案中注入即可使用
1. 生產者
//注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
void testhello() {
rabbitTemplate.convertAndSend("hello","hello world");
}
2. 消費者
package com.xizi.hello;
@Component
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class HelloCustomer {
@RabbitHandler
public void receive(String message){
System.out.println("message======= "+message);
}
}
3. 測試
四. 第二種work模型使用
1. 生產者
//注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
//公平消費 work
@Test
void testWork() {
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("work","work模型"+i);
}
}
2. 消費者
package com.xizi.work;
@Component
public class WorkCustomer {
//一個消費者
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive1(String message){
System.out.println("message1========"+message);
}
//一個消費者
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive2(String message){
System.out.println("message2========"+message);
}
}
3. 測試
五. Fanout 廣播模型
1. 生產者
//注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
//fanout 關播
@Test
void testfanout() {
rabbitTemplate.convertAndSend("logs_fanout","","fanout模型傳送的訊息");
}
2. 消費者
package com.xizi.fanout;
@Component
public class Customer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,//建立臨時佇列
exchange = @Exchange(value = "logs_fanout",type = "fanout")
)
})
public void receive1(String message){
System.out.println("message1======="+message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,//建立臨時佇列
exchange = @Exchange(value = "logs_fanout",type = "fanout")
)
})
public void receive2(String message){
System.out.println("message2======="+message);
}
}
3. 測試
六. Route 路由模型
1. 生產者
//注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
//路由模式 route
@Test
public void testRoute(){
rabbitTemplate.convertAndSend("direct","info","傳送info的路由資訊");
}
2. 消費者
package com.xizi.route;
@Component
public class RouteCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,//建立臨時佇列
exchange = @Exchange(value = "direct",type = "direct"),
key={"info","error","warn"}
)
})
public void receive1(String message){
System.out.println("message1=========="+message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,//建立臨時佇列
exchange = @Exchange(value = "direct",type = "direct"),
key={"error"}
)
})
public void receive2(String message){
System.out.println("message2=========="+message);
}
}
3. 測試
七. Topic 訂閱模型(動態路由模型)
1. 生產者
//注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
//topic 動態訂閱 訂閱模式
@Test
public void testTopic(){
rabbitTemplate.convertAndSend("topic","user.save","topic訂閱模式");
}
2. 消費者
package com.xizi.topic;
@Component
public class TopicCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(type ="topic",value ="topic",name ="topic" ),
key = {"user.save","user.*"}
)
})
public void receive1(String message ){
System.out.println("message1========"+message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(type ="topic",value ="topic",name ="topic" ),
key = {"order.#","produce.#","user.*"}
)
})
public void receive2(String message ){
System.out.println("message2========"+message);
}
}
3. 測試
相關文章
- 個人學習系列 - SpringBoot整合RabbitMQSpring BootMQ
- RabbitMQ -springboot整合rabbitmqMQSpring Boot
- SpringBoot 整合 rabbitmqSpring BootMQ
- Springboot整合RabbitMQSpring BootMQ
- Java SpringBoot 整合 RabbitMQJavaSpring BootMQ
- RabbitMQ入門到進階(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)MQSpring Boot
- springboot2.0整合rabbitmqSpring BootMQ
- SpringBoot專案整合RabbitMQSpring BootMQ
- RabbitMq--與SpringBoot整合MQSpring Boot
- SpringBoot學習之整合MybatisSpring BootMyBatis
- SpringBoot整合Activiti學習(一)Spring Boot
- SpringBoot學習之整合AOPSpring Boot
- SpringBoot整合RabbitMQ(一)快速入門Spring BootMQ
- RabbitMQ(二)JavaClient SpringBoot整合 Work queuesMQJavaclientSpring Boot
- Elasticsearch學習<四>SpringBoot整合esElasticsearchSpring Boot
- Elasticsearch學習(三)springboot整合ElasticSearchElasticsearchSpring Boot
- RabbitMQ簡介以及與SpringBoot整合示例MQSpring Boot
- 【springboot】學習4:整合JDBC、整合druid、整合mybatis、整合 SpringSecuritySpring BootJDBCUIMyBatisGse
- rabbitMq學習MQ
- SpringBoot學習之整合SwaggerSpring BootSwagger
- SpringBoot整合RabbitMQ實戰附加死信交換機Spring BootMQ
- SpringBoot2.0應用(三):SpringBoot2.0整合RabbitMQSpring BootMQ
- SpringBoot2.0原始碼分析(三):整合RabbitMQ分析Spring Boot原始碼MQ
- SpringBoot 整合 RabbitMQ 實現訊息可靠傳輸Spring BootMQ
- rabbitMq的status報錯Error: unable to perform an operation on node ‘rabbit……MQErrorORM
- CentOS7環境下RabbitMQ管理外掛rabbitmq_management啟動報錯{:query, :rabbit@rabbit1, {:badrpc, :timeout}}CentOSMQRPC
- RabbitMQ-Spring整合RabbitMQMQSpring
- 【RabbitMQ】RabbitMQ與Spring整合MQSpring
- SpringBoot整合ElasticSearch 入門demo學習筆記Spring BootElasticsearch筆記
- RabbitMQ學習筆記MQ筆記
- SpringBoot整合rabbitMq實現訊息延時傳送Spring BootMQ
- 整合學習(一):簡述整合學習
- SpringBoot整合RabbitMQ之典型應用場景實戰二Spring BootMQ
- SpringBoot整合RabbitMQ之典型應用場景實戰一Spring BootMQ
- RabbitMQ鞏固學習一MQ
- rabbitmq學習與分享(4)MQ
- RabbitMQ學習筆記-HelloWorldMQ筆記
- rabbitMq學習筆記(未完)MQ筆記