SpringBoot之整合RabbitMQ

12 篇文章 0 订阅
7 篇文章 0 订阅

该笔记大部分搬运B站编程不良人的RabbitMQ,顺便把图文合并记录,便于回顾,仅用于学习!
视频地址:https://www.bilibili.com/video/BV1dE411K7MG
作者真的非常好,别白嫖,记得三连 如有侵权,请联系删除!

1.搭建环境

1.1 引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

1.2 编写配置

spring:
  application:
    name: springboot_rabbitmq
  rabbitmq:
    host: localhost
    port: 5672
    username: ems
    password: 123
    virtual-host: /ems

2. 第一种模式(直连)

2.1 开发生产者

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class RabbitmqDemo02ApplicationTests {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Test
    void contextLoads() {
        rabbitTemplate.convertAndSend("hello","hello world");
    }

}

2.2 开发消费者

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

@Component
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class HelloCustomer {
    @RabbitHandler
    public void receive(String message) {
        System.out.println("message=="+message);
    }
}

2.3 测试

在这里插入图片描述

3. 第二种模型(work quene)

3.1 开发生产者

@SpringBootTest
class RabbitmqDemo02ApplicationTests {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Test
    void contextLoads2() {
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend("work",("provider"+i+"hello work"));
        }
    }

}

3.2 开发消费者

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

@Component
public class WorkCustomer {
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive1(String message) {
        System.out.println("work message1==" + message);
    }

    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive2(String message) {
        System.out.println("work message2==" + message);
    }
}

3.3 测试

在这里插入图片描述

4. 第三种模型(fanout)

4.1 开发生产者

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class RabbitmqDemo02ApplicationTests {

    @Autowired
    RabbitTemplate rabbitTemplate;
    //参数1为交换机,参数2为路由key,“”表示为任意路由,参数3为消息内容
    @Test
    void contextLoads3() {
        rabbitTemplate.convertAndSend("logs"," ","fanout provider message");
    }
}

4.2 开发消费者

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

@Component
public class FanoutCustomer {
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(name = "logs",type = "fanout")
    ))
    public void receive1(String message) {
        System.out.println("work message1==" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(name = "logs",type = "fanout")
    ))
    public void receive(String message){
        System.out.println("work message==" + message);
    }
}

4.3 测试

在这里插入图片描述

5. 第四种模式(Routing)

5.1 Routing 之订阅模型-Direct(直连)

5.2 开发生产者

@SpringBootTest
class RabbitmqDemo02ApplicationTests {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Test
    void contextLoads4() {
        rabbitTemplate.convertAndSend("directs","error","direct provider message");
    }
}

5.3 开发消费者

@Component
public class DirectCustomer {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            key = {"info","error"},
            exchange = @Exchange(name = "directs",type = "direct")
    ))
    public void receive1(String message) {
        System.out.println("work message1==" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            key = {"info"},
            exchange = @Exchange(name = "directs",type = "direct")
    ))
    public void receive2(String message) {
        System.out.println("work message2==" + message);
    }
}

5.4 测试

在这里插入图片描述

5.5 Routing 之订阅模型-Topic

5.6 开发生产者

@SpringBootTest
class RabbitmqDemo02ApplicationTests {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Test
    void contextLoads5() {
        rabbitTemplate.convertAndSend("topics","user.save.findAll","user.save.findAll 的消息");
    }
}

5.7 开发消费者

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

@Component
public class topicsCustomer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    key = {"user.*"},
                    exchange = @Exchange(type = "topic",name = "topics")
            )
    })
    public void receive1(String message){
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    key = {"user.#"},
                    exchange = @Exchange(type = "topic",name = "topics")
            )
    })
    public void receive2(String message){
        System.out.println("message2 = " + message);
    }
}

5.8 测试

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
整合 RabbitMQ 可以让你的 Spring Boot 项目具备更好的异步处理和消息传递能力。下面是整合的步骤: 1. 添加 RabbitMQ 依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 配置 RabbitMQ 在 application.properties 文件中添加以下配置: ``` spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ``` 如果你使用的是 RabbitMQ 的默认端口和默认用户,那么可以不用配置端口和用户名密码。 3. 创建消息接收者 ```java @Component public class Receiver { @RabbitListener(queues = "myQueue") public void processMessage(String content) { System.out.println("Received message: " + content); } } ``` 这里使用了 `@RabbitListener` 注解来监听名为 `myQueue` 的队列,并在接收到消息时打印出消息内容。 4. 创建消息发送者 ```java @Component public class Sender { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(String message) { rabbitTemplate.convertAndSend("myQueue", message); } } ``` 这里使用了 `RabbitTemplate` 类来发送消息到名为 `myQueue` 的队列。 5. 测试 ```java @SpringBootTest class RabbitmqApplicationTests { @Autowired private Sender sender; @Test void testSendMessage() { sender.sendMessage("Hello, RabbitMQ!"); } } ``` 在测试方法中调用 `Sender` 的 `sendMessage` 方法发送消息。当消息被发送时,`Receiver` 中的 `processMessage` 方法会被自动调用,从而处理消息。 以上就是整合 RabbitMQ 的基本步骤,你可以根据自己的需求进行配置和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值