SpringBoot整合RabbitMQ,简单实现消息发送和订阅

avatar 2019年07月05日00:46:43 6 7291 views
博主分享免费Java教学视频,B站账号:Java刘哥
通常为了测试两个服务之间消息生产和消费可以创建两个 SpringBoot 项目,这里为了简单,就用一个项目了,一个在正常的项目中,作为消费者(订阅者),等待接受消息;另一个通过 测试类生产消息,发送消息。 just do it!

一、SpringBoot整合 RabbitMQ

1.首先需要添加依赖 pom.xml
  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-amqp</artifactId>
  4. </dependency>
  2.配置文件 application.yml
  1. spring:
  2.   rabbitmq:
  3.     host: localhost
  4.     port: 5672
  5.     username: guest
  6.     password: guest
  3.服务消费者(消息订阅者)
  1. package com.liuyanzhao.blog.core.message;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.amqp.rabbit.annotation.Exchange;
  4. import org.springframework.amqp.rabbit.annotation.Queue;
  5. import org.springframework.amqp.rabbit.annotation.QueueBinding;
  6. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  7. import org.springframework.stereotype.Component;
  8. /**
  9.  * 服务订阅者
  10.  * @author 言曌
  11.  * @date 2019-07-02 23:58
  12.  */
  13. @Slf4j
  14. @Component
  15. public class MqReceiver {
  16.     //测试1 简单接收(订阅)
  17.     //1. @RabbitListener(queues = "myQueue") //需要手动创建队列
  18.     //2. @RabbitListener(queuesToDeclare = @Queue("myQueue")) //自动创建队列
  19.     //3. 自动创建,Exchange和Queue绑定
  20.     @RabbitListener(bindings = @QueueBinding(
  21.             value = @Queue("myQueue"),
  22.             exchange = @Exchange("myExchange")
  23.     ))
  24.     public void process(String message) {
  25.         log.info("MqReceiver:{}", message);
  26.     }
  27.     //测试2 消息分组
  28.     /**
  29.      * 数码供应商 接受消息
  30.      * 只接受 computer 这个key的消息
  31.      * @param message
  32.      */
  33.     @RabbitListener(bindings = @QueueBinding(
  34.             value = @Queue("myOrder"),
  35.             key = "computer",
  36.             exchange = @Exchange("computerOrder")
  37.     ))
  38.     public void processComputer(String message) {
  39.         log.info("computer MqReceiver:{}", message);
  40.     }
  41.     /**
  42.      * 水果供应商服务 接收消息
  43.      * 只接受 fruit 这个key的消息
  44.      * @param message
  45.      */
  46.     @RabbitListener(bindings = @QueueBinding(
  47.             value = @Queue("myOrder"),
  48.             key = "fruit",
  49.             exchange = @Exchange("fruitOrder")
  50.     ))
  51.     public void processFruit(String message) {
  52.         log.info("fruit MqReceiver:{}", message);
  53.     }
  54. }
  4、服务生产者(消息发送者)
  1. package com.liuyanzhao.blog.core.message;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.amqp.core.AmqpTemplate;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. import java.util.Date;
  9. /**
  10.  *
  11.  * 服务生产者
  12.  * @author 言曌
  13.  * @date 2019-07-03 00:05
  14.  */
  15. @SpringBootTest
  16. @RunWith(SpringRunner.class)
  17. public class MqSenderTest {
  18.     @Autowired
  19.     private AmqpTemplate amqpTemplate;
  20.     /**
  21.      * 生产者将消息直接发送到队列
  22.      * @throws Exception
  23.      */
  24.     @Test
  25.     public void send() throws Exception {
  26.         amqpTemplate.convertAndSend("myQueue""hello saysky, now is " + new Date());
  27.     }
  28.     /**
  29.      * 生产者将消息发送到Exchange
  30.      * @throws Exception
  31.      */
  32.     @Test
  33.     public void sendComputer() throws Exception {
  34.         amqpTemplate.convertAndSend("computerOrder""computer""hello saysky, now is " + new Date());
  35.     }
  36.     /**
  37.      * 生产者将消息发送到Exchange
  38.      * @throws Exception
  39.      */
  40.     @Test
  41.     public void sendFruit() throws Exception {
  42.         amqpTemplate.convertAndSend("fruitOrder""fruit""hello saysky, now is " + new Date());
  43.     }
  44. }
 

二、启动项目

1.启动RabbitMQ RabbitMQ 的安装和启动之前介绍了,点此直达 2.运行项目,目的是启动服务消费者,可以在接收消息里打个断点,方便感知订阅到消息 3.启动测试方法,目的是启动服务生产者,发送消息 经过测试没有问题  
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0