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 测试

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值