【RabbitMQ自学笔记三】Spring Boot整合RabbitMQ

本文介绍如何使用SpringBoot整合RabbitMQ的五种通信模型,包括HelloWorld、Work工作模型、发布/订阅模型、路由(直连)模型及主题(通配符)模式。通过具体代码示例,详细解释了每种模型的配置与实现。
摘要由CSDN通过智能技术生成


本文将使用Spring Boot完成上一期RabbitMQ5种模型的整合。

配置RabbitMQ

使用Idea快速构建一个Spring Boot项目,选择Web和Spring for RabbitMQ依赖:

在这里插入图片描述

接下来配置application.properties,当然你也可以选择application.yml,配置基本的RabbitMQ信息:

spring.application.name=rabbit-springboot
server.port=8080
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtual-host=/demo

整合5种模型

Spring Boot为我们使用RabbitMQ封装好了RabbitTemplate对象,我们只需要借助这个对象,搭配注解,就可以实现各种操作。

Hello World模型

Publisher

Spring Boot整合RabbitMQ非常简单,只需要调用自动注入rabbitTemplate的convertAndSend方法即可。

我们来看一下convertAndSend(转换并发送)的几种重载:

  • convertAndSend(String routingKey, Object object) 提供路由键和消息体,相当于basicPublish("",String routingKey, byte[] body)
  • convertAndSend(String exchange, String routingKey, Object object) 提供交换机、路由键和消息体,相当于basicPublish(String exchange, String routingKey, byte[] body)

这两种重载已经足够我们完成5种模型。

hello world模型,不提供交换机名字,即使用默认交换机,在使用默认交换机时,路由键即队列名。

package org.koorye.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.koorye.RabbitSpringbootApplication;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = RabbitSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbit {
  @Autowired
  private RabbitTemplate rabbitTemplate;

  @Test
  public void sendMsg() {
    rabbitTemplate.convertAndSend("hello world", "Hello world!");
  }
}

Consumer

使用@Component注解标注组件。

使用@RabbitListener注解标注接收者,里面的queuesToDeclare声明队列。

使用@Queue声明一个队列。

@Queue包含之前声明队列的5个属性:

  • value 队列名
  • durable 是否持久化,默认true
  • exclusive 是否独占,默认false
  • autoDelete 是否自动删除,默认false
  • arguments 额外参数,默认空

此时我们只声明队列名,其他默认。

除此之外,还有一种注解方法,可以把@RabbitListener注解到类上,接收消息的方法体则注解@RabbitHandler,可以达到一样的效果。

package org.koorye.component;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class TestRabbit {
  @RabbitListener(queuesToDeclare = @Queue(value = "hello world"))
  public void getMsg(String msg) {
    System.out.println("[ INFO ] Get message: " + msg);
  }
}

运行:

...
2020-07-19 15:55:38.729  INFO 13484 --- [           main] org.koorye.test.TestRabbit               : Started TestRabbit in 6.182 seconds (JVM running for 7.474)
[ INFO ] Get message: Hello world!
2020-07-19 15:55:39.166  INFO 13484 --- [extShutdownHook] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
...

Process finished with exit code 0

成功收到消息。

Work工作模型

基于hello world模型,实现work模型非常简单,只需要再编写一个Listener方法体即可:

@Component
public class TestRabbit {
  @RabbitListener(queuesToDeclare = @Queue(value = "hello world"))
  public void consumer1(String msg) {
    System.out.println("[ INFO ] Consumer1 get message: " + msg);
  }

  @RabbitListener(queuesToDeclare = @Queue(value = "hello world"))
  public void consumer2(String msg) {
    System.out.println("[ INFO ] Consumer2 get message: " + msg);
  }
}

这次我们循环发送10条消息:

  @Test
  public void sendMsg() {
    for (int i = 0; i < 10; ++i)
      rabbitTemplate.convertAndSend("hello world", "Hello world!");
  }

运行:

[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
...

Process finished with exit code 0

发布 / 订阅模型

Publisher

使用convertAndSend的第二种重载,不指定路由键:

  @Test
  public void sendMsg() {
    for (int i = 0; i < 10; ++i)
      rabbitTemplate.convertAndSend("logs","", "Hello world!");
  }

Consumer

使用@QueueBingding绑定队列和交换机。

使用@Queue(后不跟内容)来声明一个临时队列,测试完成即删除

使用@Exchange声明一个交换机,value指定名字,type指定类型为fanout

  @RabbitListener(bindings = {
      @QueueBinding(
          value = @Queue,
          exchange = @Exchange(value = "logs", type = "fanout")
      )})
  public void consumer1(String msg) {
    System.out.println("[ INFO ] Consumer1 get message: " + msg);
  }

  @RabbitListener(bindings = {
      @QueueBinding(
          value = @Queue,
          exchange = @Exchange(value = "logs", type = "fanout")
      )})
  public void consumer2(String msg) {
    System.out.println("[ INFO ] Consumer2 get message: " + msg);
  }

运行:

[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer1 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!
[ INFO ] Consumer2 get message: Hello world!

Process finished with exit code 0

测试成功。

路由(直连)模型

Publisher

在发布订阅模型的基础上,发送时指定路由键即可。

  @Test
  public void sendMsg() {
    rabbitTemplate.convertAndSend("logs", "info", "This is an info.");
    rabbitTemplate.convertAndSend("logs", "err", "This is an err.");
  }

Consumer

@QueueBinding的key中指定路由键,以花括号包围字符串。

  @RabbitListener(bindings = {
      @QueueBinding(
          value = @Queue,
          exchange = @Exchange(value = "logs", type = "direct"),
          key = {"info"}
      )})
  public void consumer1(String msg) {
    System.out.println("[ INFO ] Consumer1 get message: " + msg);
  }

  @RabbitListener(bindings = {
      @QueueBinding(
          value = @Queue,
          exchange = @Exchange(value = "logs", type = "direct"),
          key = {"info", "err"}
      )})
  public void consumer2(String msg) {
    System.out.println("[ INFO ] Consumer2 get message: " + msg);
  }

运行,记得先删除之前的交换机

[ INFO ] Consumer2 get message: This is an info.
[ INFO ] Consumer1 get message: This is an info.
[ INFO ] Consumer2 get message: This is an err.
...

Process finished with exit code 0

测试成功。

主题(通配符)模式

Publisher

  @Test
  public void sendMsg() {
    rabbitTemplate.convertAndSend("logs", "info", "This is an info.");
    rabbitTemplate.convertAndSend("logs", "err", "This is an err.");
    rabbitTemplate.convertAndSend("logs", "info.err", "This is an info and err.");
  }

Consumer

在直连模式的基础上,修改type和key即可:

  @RabbitListener(bindings = {
      @QueueBinding(
          value = @Queue,
          exchange = @Exchange(value = "logs", type = "topic"),
          key = {"#.info.#"}
      )})
  public void consumer1(String msg) {
    System.out.println("[ INFO ] Consumer1 get message: " + msg);
  }

  @RabbitListener(bindings = {
      @QueueBinding(
          value = @Queue,
          exchange = @Exchange(value = "logs", type = "topic"),
          key = {"#.err.#"}
      )})
  public void consumer2(String msg) {
    System.out.println("[ INFO ] Consumer2 get message: " + msg);
  }

  @RabbitListener(bindings = {
      @QueueBinding(
          value = @Queue,
          exchange = @Exchange(value = "logs", type = "topic"),
          key = {"info.#.err.#"}
      )})
  public void consumer3(String msg) {
    System.out.println("[ INFO ] Consumer3 get message: " + msg);
  }

运行:

[ INFO ] Consumer3 get message: This is an info and err.
[ INFO ] Consumer1 get message: This is an info.
[ INFO ] Consumer2 get message: This is an err.
[ INFO ] Consumer1 get message: This is an info and err.
[ INFO ] Consumer2 get message: This is an info and err.
...

Process finished with exit code 0

测试成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值