Java SpringBoot 整合 RabbitMQ
如果要進行 RabbitMQ 整合的時候一定要注意以下幾個概念:交換空間、虛擬主機、佇列資訊。本次為了方便起見將專案分為 兩個:RabbitMQ-Consumer、RabbitMQ-Producer。
瞭解springcloud架構可以加求求:三五三六二四七二五九
1、 【兩個專案】將 rabbitmq 的依賴支援包複製到專案之中;
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
2、【microboot-rabbitmq-producer、microboot-rabbitmq-consumer】修改 application.yml 配置檔案,追加 rabbitmq 的相關配置項:
server: port: 80 spring: messages: basename: i18n/Messages,i18n/Pages rabbitmq: addresses: rabbitmq-server username: studyjava password: hello virtual-host: /
3、【microboot-rabbitmq-producer】建立一個訊息的傳送介面:
package cn.study.microboot.producer; public interface IMessageProducerService { public void sendMessage(String msg) ; }
4、 【microboot-rabbitmq-producer】為了可以正常使用 RabbitMQ 進行訊息處理,你還需要做一個訊息生產配置類;
package cn.study.microboot.config; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ProducerConfig { public static final String EXCHANGE = "study.microboot.exchange"; // 交換空間名稱 public static final String ROUTINGKEY = "study.microboot.routingkey"; // 設定路由key public static final String QUEUE_NAME = "study.microboot.queue"; // 佇列名稱 @Bean public Binding bindingExchangeQueue(DirectExchange exchange,Queue queue) { return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY) ; } @Bean public DirectExchange getDirectExchange() { // 使用直連的模式 return new DirectExchange(EXCHANGE, true, true); } @Bean public Queue queue() { // 要建立的佇列資訊 return new Queue(QUEUE_NAME); } }
5、 【microboot-rabbitmq-producer】建立訊息服務的實現子類:
package cn.study.microboot.producer.impl; import javax.annotation.Resource; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Service; import cn.study.microboot.config.ProducerConfig; import cn.study.microboot.producer.IMessageProducerService; @Service public class MessageProducerServiceImpl implements IMessageProducerService { @Resource private RabbitTemplate rabbitTemplate; @Override public void sendMessage(String msg) { this.rabbitTemplate.convertAndSend(ProducerConfig.EXCHANGE, ProducerConfig.ROUTINGKEY, msg); } }
6、 【microboot-rabbitmq-consumer】依然需要做一個消費者的配置程式類,而這個程式類裡面主要的目的依然是設定交換空間、 路由 KEY 等資訊。
package cn.study.microboot.config; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ConsumerConfig { public static final String EXCHANGE = "study.microboot.exchange"; // 交換空間名稱 public static final String ROUTINGKEY = "study.microboot.routingkey"; // 設定路由key public static final String QUEUE_NAME = "study.microboot.queue"; // 佇列名稱 @Bean public Queue queue() { // 要建立的佇列資訊 return new Queue(QUEUE_NAME); } @Bean public DirectExchange getDirectExchange() { // 使用直連的模式 return new DirectExchange(EXCHANGE, true, true); } @Bean public Binding bindingExchangeQueue(DirectExchange exchange,Queue queue) { return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY) ; } }
7、 【microboot-rabbitmq-consumer】實現監聽處理類:
package cn.study.microboot.consumer; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Service; @Service public class MessageConsumerService { @RabbitListener(queues="study.microboot.queue") public void receiveMessage(String text) { // 進行訊息接收處理 System.err.println("【*** 接收訊息 ***】" + text); } }
8、 【microboot-rabbitmq-producer】建立一個測試類實現訊息的傳送處理。
package cn.study.microboot.test; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import cn.study.microboot.StartSpringBootMain; import cn.study.microboot.producer.IMessageProducerService; @SpringBootTest(classes = StartSpringBootMain.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class TestActiveMQ { @Resource private IMessageProducerService messageProducer; @Test public void testSend() throws Exception { for (int x = 0; x < 100; x++) { this.messageProducer.sendMessage("study - " + x); } } }
9、 【microboot-rabbitmq-consumer】編寫訊息接收測試類,這裡面不需要編寫程式碼,只需要做一個休眠即可:
package cn.study.microboot; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @SpringBootTest(classes = StartSpringBootMain.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class AppTest { @Test public void testStart() throws Exception { Thread.sleep(Long.MAX_VALUE); } }
整體進行專案開發之中整合的處理步驟還是簡單,但是千萬要注意,由於是第一次整合處理,所以將生產者與消費者的配置 類分開了,實際上這兩個類的作用是完全一樣的。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69952307/viewspace-2677364/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- RabbitMQ -springboot整合rabbitmqMQSpring Boot
- SpringBoot 整合 rabbitmqSpring BootMQ
- Springboot整合RabbitMQSpring BootMQ
- RabbitMQ入門到進階(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)MQSpring Boot
- springboot2.0整合rabbitmqSpring BootMQ
- SpringBoot專案整合RabbitMQSpring BootMQ
- RabbitMq--與SpringBoot整合MQSpring Boot
- Rabbit學習---SpringBoot整合RabbitMQSpring BootMQ
- SpringBoot整合RabbitMQ(一)快速入門Spring BootMQ
- RabbitMQ(二)JavaClient SpringBoot整合 Work queuesMQJavaclientSpring Boot
- 個人學習系列 - SpringBoot整合RabbitMQSpring BootMQ
- RabbitMQ簡介以及與SpringBoot整合示例MQSpring Boot
- SpringBoot整合RabbitMQ實戰附加死信交換機Spring BootMQ
- SpringBoot2.0應用(三):SpringBoot2.0整合RabbitMQSpring BootMQ
- SpringBoot2.0原始碼分析(三):整合RabbitMQ分析Spring Boot原始碼MQ
- SpringBoot 整合 RabbitMQ 實現訊息可靠傳輸Spring BootMQ
- Java SpringBoot 整合 ActiveMQJavaSpring BootMQ
- RabbitMQ-Spring整合RabbitMQMQSpring
- 【RabbitMQ】RabbitMQ與Spring整合MQSpring
- SpringBoot整合rabbitMq實現訊息延時傳送Spring BootMQ
- SpringBoot整合RabbitMQ之典型應用場景實戰二Spring BootMQ
- SpringBoot整合RabbitMQ之典型應用場景實戰一Spring BootMQ
- Java Springboot整合RabbitMQ(六):(RPC)-b2b2c小程式電子商務JavaSpring BootMQRPC
- Java-SpringBoot整合SpringCloudJavaSpring BootGCCloud
- Java Springboot 整合RabbitMQ(四):路由(Routing)-B2B2C小程式電子商務JavaSpring BootMQ路由
- spring boot-整合RabbitMq(RabbitMq基礎)Spring BootMQ
- Spring Boot整合rabbitmqSpring BootMQ
- Spring Boot 整合 rabbitmqSpring BootMQ
- 整合RabbitMQ&SpringMQSpring
- RabbitMQ(三):RabbitMQ與Spring Boot簡單整合MQSpring Boot
- Springboot + rabbitMq佇列Spring BootMQ佇列
- java~springboot~gradle裡的docker整合JavaSpring BootGradleDocker
- Java訊息佇列:RabbitMQ與Kafka的整合與應用Java佇列MQKafka
- RabbitMQ - SpringBoot 案例 - fanout 模式MQSpring Boot模式
- RabbitMQ - SpringBoot 案例 - direct 模式MQSpring Boot模式
- RabbitMQ - SpringBoot 案例 - topic 模式MQSpring Boot模式
- (二)springboot中使用RabbitmqSpring BootMQ
- springboot(八):RabbitMQ詳解Spring BootMQ