其他分享
首页 > 其他分享> > Rabbit学习---SpringBoot整合RabbitMQ

Rabbit学习---SpringBoot整合RabbitMQ

作者:互联网

SpringBoot整合RabbitMQ


我这里只是简单的测试RabbitMQ四种模型,并没有设计太深入的知识点

一. 创建SpringBoot项目导入依赖

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

二. 配置文件application.yml

spring:
  application:
    name: rabbitmq-springboot


  rabbitmq:
    host: 192.168.153.180
    username: /ems
    password: 123
    virtual-host: /ems
    port: 5672

三. 第一种hello world模型使用

RabbitTemplate 用来简化操作 使用时候直接在项目中注入即可使用

1. 生产者

    //注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

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

2. 消费者

package com.xizi.hello;

@Component
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class HelloCustomer {


    @RabbitHandler
    public void receive(String message){
        System.out.println("message======= "+message);
    }
}

3. 测试

在这里插入图片描述

四. 第二种work模型使用

1. 生产者

    //注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //公平消费 work
    @Test
    void testWork() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work","work模型"+i);
        }
    }

2. 消费者

package com.xizi.work;

@Component
public class WorkCustomer {

    //一个消费者
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive1(String message){
        System.out.println("message1========"+message);
    }

    //一个消费者
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive2(String message){
        System.out.println("message2========"+message);
    }

}

3. 测试

在这里插入图片描述

五. Fanout 广播模型

1. 生产者

    //注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //fanout 关播
    @Test
    void testfanout() {
        rabbitTemplate.convertAndSend("logs_fanout","","fanout模型发送的消息");
    }

2. 消费者

package com.xizi.fanout;

@Component
public class Customer {

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "logs_fanout",type = "fanout")
            )
    })
    public void receive1(String message){
        System.out.println("message1======="+message);
    }


    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "logs_fanout",type = "fanout")
            )
    })
    public void receive2(String message){
        System.out.println("message2======="+message);
    }
}


3. 测试

在这里插入图片描述

六. Route 路由模型

1. 生产者

    //注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    //路由模式 route
    @Test
    public void testRoute(){
        rabbitTemplate.convertAndSend("direct","info","发送info的路由信息");
    }

2. 消费者

package com.xizi.route;

@Component
public class RouteCustomer {

    @RabbitListener(bindings = {
          @QueueBinding(
                  value = @Queue,//创建临时队列
                  exchange = @Exchange(value = "direct",type = "direct"),
                  key={"info","error","warn"}
          )
    })
    public  void receive1(String message){
        System.out.println("message1=========="+message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "direct",type = "direct"),
                    key={"error"}
            )
    })
    public  void receive2(String message){
        System.out.println("message2=========="+message);
    }
}

3. 测试

在这里插入图片描述

七. Topic 订阅模型(动态路由模型)

1. 生产者

    //注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    //topic 动态订阅 订阅模式
    @Test
    public void testTopic(){
        rabbitTemplate.convertAndSend("topic","user.save","topic订阅模式");
    }

2. 消费者

package com.xizi.topic;

@Component
public class TopicCustomer {

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



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


3. 测试

在这里插入图片描述

标签:rabbitTemplate,topic,SpringBoot,void,RabbitMQ,---,value,message,public
来源: https://blog.csdn.net/weixin_45480785/article/details/112132485