Rabbit學習---SpringBoot整合RabbitMQ

戲子zzz發表於2021-01-03


我這裡只是簡單的測試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. 測試

在這裡插入圖片描述

相關文章