前言
Rabbitmq簡介這里就不多說了,安裝可以見我另外一篇帖子使用Docker安裝Rabbitmq,Springboot對Mq協議封裝的比較好了,因此其實整合程序很簡單,大家按步驟進行操作就可以完成快速整合,
文章末尾處提供了源代碼地址,可以下載完整代碼,
maven依賴
我這里放工程的完整依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
組態檔
這里在Spring中配置Mq的地址及密碼,這里沒有設定vhost,是使用默認的,如果你們創建了則可以通過配置指定對應的vhost,
spring:
#rabbitmq配置
rabbitmq:
username: admin
password: 12345678Aa
addresses: 127.0.0.1:5672
工程目錄結構
目錄結構僅供參考,這里做個簡單說明:
- base包中定義了交換機名稱、佇列名稱、路由鍵名稱,
- config包中是用來系結交換機和佇列以及路由鍵,
- mq包中是生產者和消費者,
- vo包中放著mq的傳輸物件,

功能類詳解
QueueConfig中使用@Bean注解將對應的queue、exchange、binding等交給容器統一管理,
@Configuration
public class QueueConfig {
@Bean
public DirectExchange defaultExchange() {
return new DirectExchange(ExchangeName.DEFAULT.name());
}
@Bean
public DirectExchange notifyMsgDirectExchange() {
return new DirectExchange(ExchangeName.NOTIFY_MSG.name());
}
@Bean
public Queue notifyMsgQueue() {
return new Queue(QueueNames.NOTIFY_MSG_QUEUE, true);
}
@Bean
public Binding notifyMsgQueueBinding() {
return BindingBuilder
.bind(notifyMsgQueue())
.to(notifyMsgDirectExchange())
.with(RoutingKeyName.NOTIFY_MSG);
}
}
Mq傳輸物件需要實作Serializable,
@Data
@ToString
public class NotifyMsgSendVO implements Serializable {
private static final long serialVersionUID = 5905249092659173678L;
private String priKey;
private String businessType;
private String phoneNum;
private String msg;
}
Mq生產者,直接注入RabbitTemplate,呼叫方法傳入交換機名稱及路由鍵,它會根據之前設定的系結關系將訊息路由到對應的佇列中由佇列另一端的消費者消費,
@Slf4j
@Component
public class NotifyMsgProducer {
@Resource
private RabbitTemplate rabbitTemplate;
public void send(NotifyMsgSendVO notifyMsgSendVO) {
log.debug("生產訊息【{}】",notifyMsgSendVO);
this.rabbitTemplate.convertAndSend(ExchangeName.NOTIFY_MSG.name(),
RoutingKeyName.NOTIFY_MSG.name(), notifyMsgSendVO);
}
}
Mq消費者,使用@RabbitListener注解指定對應的佇列即可消費訊息,這里值得一說的是,在消費者中需要處理一下例外,如果不處理會導致消費者無法繼續消費,
@Component
@Slf4j
public class NotifyMsgConsumer {
@RabbitListener(queues = QueueNames.NOTIFY_MSG_QUEUE)
public void msgSend(NotifyMsgSendVO vo) {
System.out.println("消費者收到訊息:"+vo);
}
}
測驗
這里提供了一個http介面,呼叫介面可以生成一個測驗訊息,
@RestController
public class TestMqController {
@Resource
private NotifyMsgProducer notifyMsgProducer;
@GetMapping("/produce")
public String produce() {
NotifyMsgSendVO vo = new NotifyMsgSendVO();
vo.setPriKey(UUID.randomUUID().toString());
vo.setPhoneNum("191xxxxxxxx");
vo.setBusinessType("msg_send");
notifyMsgProducer.send(vo);
return "success";
}
}
呼叫介面:http://localhost:9091/produce
查看控制臺日志:
可以看到消費者已經收到了訊息,
看完記得點贊哦!
下面是源代碼地址:https://gitee.com/ErGouGeSiBaKe/springboot-rabbitmq
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/292903.html
標籤:其他
