Rabbit MQ实战【direct交换器】

偏执的太偏执、 2023-07-03 06:15 129阅读 0赞

spring boot版本:2.1.10.RELEASE

本文涉及两个项目 rabbitmq-direct-consumerrabbitmq-direct-provider,所需maven依赖和配置相同。

在这里插入图片描述

相关依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-amqp</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-test</artifactId>
  12. <scope>test</scope>
  13. </dependency>

相关配置

  1. #rabbitmq配置
  2. spring.rabbitmq.host=192.168.xxx.xxx
  3. spring.rabbitmq.port=5672
  4. spring.rabbitmq.username=admin
  5. spring.rabbitmq.password=admin
  6. spring.rabbitmq.virtual-host=/
  7. #设置交换器(以下5个变量均为自定义变量)
  8. mq.config.exchange=log.direct
  9. mq.config.queue.info=log.info
  10. mq.config.queue.info.routing.key=log.info.routing.key

rabbitmq-direct-consumer项目

创建消息接收类

  1. package com.ebook.rabbitmq;
  2. import org.springframework.amqp.core.ExchangeTypes;
  3. import org.springframework.amqp.rabbit.annotation.*;
  4. import org.springframework.stereotype.Component;
  5. /** * @author:JZ * @date:2020/2/2 */
  6. @Component
  7. @RabbitListener(bindings = @QueueBinding(
  8. value = @Queue(value = "${mq.config.queue.info}", autoDelete = "true"),//autoDelete表示队列为临时队列
  9. exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.DIRECT),
  10. key = "${mq.config.queue.info.routing.key}"
  11. ))
  12. public class InfoReciver {
  13. @RabbitHandler
  14. public void process(String msg) {
  15. System.out.println("接收到INFO日志:" + msg);
  16. }
  17. }

rabbitmq-direct-provider项目

(1)创建消息发送类

  1. package com.ebook.rabbitmq;
  2. import org.springframework.amqp.core.AmqpTemplate;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Component;
  6. import java.util.Date;
  7. /** * @author:JZ * @date:2020/2/2 */
  8. @Component
  9. public class Sender {
  10. @Value("${mq.config.exchange}")
  11. private String exchange;
  12. @Value("${mq.config.queue.info.routing.key}")
  13. private String infoRoutingKey;
  14. @Autowired
  15. private AmqpTemplate rabbitTemplate;
  16. public void send() {
  17. String msg = "hello:" + new Date();
  18. this.rabbitTemplate.convertAndSend(this.exchange, this.infoRoutingKey, msg);
  19. }
  20. }

(2)创建测试类

  1. import com.ebook.rabbitmq.RabbitmqDirectProvider;
  2. import com.ebook.rabbitmq.Sender;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  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. @RunWith(SpringRunner.class)
  9. @SpringBootTest(classes = RabbitmqDirectProvider.class)
  10. public class RabbitMqHelloApplicationTests {
  11. @Autowired
  12. private Sender sender;
  13. @Test
  14. public void send() throws InterruptedException {
  15. this.sender.send();
  16. }
  17. }

运行

启动rabbitmq-direct-consumer项目,然后运行测试类,在consumer的控制台查看收到的消息。

发表评论

表情:
评论列表 (有 0 条评论,129人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Rabbit MQ篇:Rabbit MQ 介绍

    `RabbitMQ`作为目前应用相当广泛的消息中间件,在企业级应用、微服务应用中充当着重要的角色。特别是在一些典型的应用场景以及业务模块中具有重要的作用,比如业务服务模块解耦、

    相关 Rabbit MQ机制

    你的应用程序和Rabbit Server之间会创建一个TCP连接,一旦TCP打开,并通过了认证,认证就是你试图连接Rabbit之前发送的Rabbit服务器连接信息和用户名和密码