SpringBoot之整合RabbitMQ

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

1.搭建环境

1.1 引入依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-amqpartifactId>
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 测试

SpringBoot之整合RabbitMQ_第1张图片

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

SpringBoot之整合RabbitMQ_第2张图片

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

SpringBoot之整合RabbitMQ_第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 测试

SpringBoot之整合RabbitMQ_第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 测试

SpringBoot之整合RabbitMQ_第5张图片

你可能感兴趣的:(SpringBoot,中间件,#,RabbitMQ,SpringBoot,整合RabbitMQ)