RabbitMQ实战:Springboot集成RabbitMQ并验证五种消息模型

这目录

一、添加依赖

javascript 复制代码
        <!--AMQP依赖,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <!-- 引入knife4j-spring-ui包 /doc.html-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

二、配置文件中添加RabbitMQ访问配置

javascript 复制代码
spring.rabbitmq.host=192.168.0.104
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin123

三、消息生产者代码

javascript 复制代码
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "生产者服务")
@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ApiOperation(value = "基本消息模型")
    @PostMapping("/testHello")
    public String testHello() {
        rabbitTemplate.convertAndSend("hello","hello world");
        return "ok";
    }

    @ApiOperation(value = "work消息模型")
    @PostMapping("/testWork")
    public String testWork() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work","hello work!");
        }
        return "ok";
    }

    @ApiOperation(value = "订阅模型-Fanout 广播模式")
    @PostMapping("/testFanout")
    public String testFanout() {
        rabbitTemplate.convertAndSend("logs","","这是日志广播");
        return "ok";
    }

    @ApiOperation(value = "订阅模型-Direct")
    @PostMapping("/testDirect")
    public String testDirect() {
        rabbitTemplate.convertAndSend("directs","error","error 的日志信息");
        return "ok";
    }

    @ApiOperation(value = "订阅模型-Topic")
    @PostMapping("/testTopic")
    public String testTopic() {
        rabbitTemplate.convertAndSend("topics","user.save.findAll","user.save.findAll 的消息");
        return "ok";
    }
}

四、消息消费者代码

javascript 复制代码
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 SpringRabbitListener {

     //基本消息模型
     @RabbitListener(queuesToDeclare = @Queue("hello"))
     public void receive(String message) {
         System.out.println("message = " + message);
     }

     //work消息模型
     @RabbitListener(queuesToDeclare = @Queue("work"))
     public void workReceive1(String message) {
         System.out.println("workReceive1 message = " + message);
     }
     //work消息模型
     @RabbitListener(queuesToDeclare = @Queue("work"))
     public void workReceive2(String message) {
         System.out.println("workReceive2 message = " + message);
     }

    //订阅模型-Fanout 广播模式
    @RabbitListener(bindings = @QueueBinding(value = @Queue, exchange = @Exchange(name="logs",type = "fanout")))
    public void fanoutReceive(String message) {
        System.out.println("fanoutReceive message = " + message);
    }

    //订阅模型-Direct
    @RabbitListener(bindings ={@QueueBinding(value = @Queue(),key={"info","error"},exchange = @Exchange(type = "direct",name="directs"))})
    public void directReceive(String message) {
        System.out.println("directReceive message = " + message);
    }

    //订阅模型-Topic
    @RabbitListener(bindings = {@QueueBinding( value = @Queue,key = {"user.*"},exchange = @Exchange(type = "topic",name = "topics"))})
    public void topicReceive1(String message) {
        System.out.println("topicReceive1 message = " + message);
    }
    //订阅模型-Topic
    @RabbitListener(bindings = {@QueueBinding(value = @Queue,key = {"user.#"},exchange = @Exchange(type = "topic",name = "topics"))})
    public void topicReceive2(String message) {
        System.out.println("topicReceive2 message = " + message);
    }

}

五、验证

浏览器输入:ip:8080/doc.html 对接口逐个进行测试

参考资料

  1. RabbitMQ详解
  2. RabbitMQ五种消息模型
相关推荐
黑胡子大叔的小屋1 小时前
基于springboot的海洋知识服务平台的设计与实现
java·spring boot·毕业设计
计算机毕设孵化场2 小时前
计算机毕设-基于springboot的校园社交平台的设计与实现(附源码+lw+ppt+开题报告)
spring boot·课程设计·计算机毕设论文·计算机毕设ppt·计算机毕业设计选题推荐·计算机选题推荐·校园社交平台
苹果醋32 小时前
Golang的文件加密工具
运维·vue.js·spring boot·nginx·课程设计
小马爱打代码4 小时前
Spring Boot 中 Map 的最佳实践
java·spring boot·spring
全栈开发帅帅5 小时前
基于springboot+vue实现的博物馆游客预约系统 (源码+L文+ppt)4-127
java·spring boot·后端
m0_748255655 小时前
Springboot基于Web的景区疫情预警系统设计与实现5170q(程序+源码+数据库+调试部署+开发环境)
前端·数据库·spring boot
平行线也会相交6 小时前
云图库平台(三)——后端用户模块开发
数据库·spring boot·mysql·云图库平台
lxyzcm7 小时前
深入理解C++23的Deducing this特性(上):基础概念与语法详解
开发语言·c++·spring boot·设计模式·c++23
wy02_7 小时前
Linux下载RabbitMQ,并解决Github拒绝访问443的问题
linux·rabbitmq·github
励碼7 小时前
Spring Security 6.3 权限异常处理实战解析
spring boot