springboot整合RabbitMQ

RabbitMQ 5种消息分发机制

application.yml

spring:
  application:
    name: rabbitmq-ying
  rabbitmq:
    host: localhost
    port: 5672
    username: ying
    password: ying
    virtual-host: /ying

pom.xml

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

1.第一种
生产者生成消息后直接放入通道中,消费者绑定该通道,当有消息进来就进行消费
在这里插入图片描述
创建消费者

package com.ying.rabbitmq.example;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class HelloWorld {
    //对hello通道进行监听,有消息就消费它
    @RabbitListener(queuesToDeclare = @Queue("hello"))
    public void receive1(String message){
        System.out.println("HelloWorld-----"+message);
    }
}

创建生产者

package com.ying.rabbitmq;

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 RabbitmqApplicationTests {
    
    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    @Test
    void helloworld() {
            rabbitTemplate.convertAndSend("hello","hello world");
    }
    

Test执行结果如下,发送消息到hello通道成功被消费
在这里插入图片描述

2.第二种
在这里插入图片描述
生产者生成消息后直接放入通道中,2个或者多个消费者绑定该通道,消息会通过轮询或者形式分发给不同消费者

创建消费者

package com.ying.rabbitmq.example;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class WorkQueue {

    //对work通道进行监听,有消息就消费它
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive1(String message) throws InterruptedException {
        System.out.println("workqueue 1-----"+message);
    }

    //对work通道进行监听,有消息就消费它
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive2(String message){
        System.out.println("workqueue 2-----"+message);
    }
}

创建生产者

    @Test
    void workqueue() {
        for (int i = 1; i <= 10; i++) {
            rabbitTemplate.convertAndSend("work","hello world   "+i);
        }
    }

Test执行结果如下,发送消息到work通道成功被消费
在这里插入图片描述
3.第三种
在这里插入图片描述
生产者将消息发送给指定交换机,由交换机把消息通过临时通道给绑定该交换机的消费者进行消息消费。类似广播和订阅消息。

创建消费者

package com.ying.rabbitmq.example;

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 Subscribe {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,//不指定名字,就是临时通道
            exchange = @Exchange(name = "Subscribe", type = "fanout") //绑定交换机类型,指定广播类型
    ))
    public void receive1(String message) {
        System.out.println("Subscribe1 = " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(name = "Subscribe", type = "fanout")
    ))
    public void receive2(String message) {
        System.out.println("Subscribe2 = " + message);
    }
}

创建生产者

    @Test
    public void Subscribe() {
        rabbitTemplate.convertAndSend("Subscribe","","类似订阅和广播");
    }

Test执行结果如下,发送消息到Subscribe交换机进行广播,
在这里插入图片描述
4.第四种
在这里插入图片描述
生产者将消息发送给指定交换机,并限制要指定发给那个通道,通道在把消息给绑定他的消费者进行消费

创建消费者

package com.ying.rabbitmq.example;

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 Routing {

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(),//临时通道
                    key = {"user", "error"},//绑定要监听的通道
                    exchange = @Exchange(type = "direct", name = "directs") //指定交换机类型 交换机名字
            )})
    public void receive1(String message) {
        System.out.println("Routing1 = " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(),
                    key = {"error"},
                    exchange = @Exchange(type = "direct", name = "directs")
            )})
    public void receive2(String message) {
        System.out.println("Routing2 = " + message);
    }

}

创建生产者

    @Test
    public void Routing() {
        rabbitTemplate.convertAndSend("directs", "error", "error 的信息");
        rabbitTemplate.convertAndSend("directs", "user", "user 的信息");
    }

Test执行结果如下,发送消息到directs交换机进行分发,
在这里插入图片描述
第五种
在这里插入图片描述
生产者将消息发送给指定交换机,进行匹配通配符和占位符来分发消息给不同的通道,通道在把消息给绑定他的消费者进行消费

创建消费者

package com.ying.rabbitmq.example;

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 Topics {

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

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    key = {"order.#"},//#通配符
                    exchange = @Exchange(type = "topic", name = "topics")
            )
    })
    public void receive2(String message) {
        System.out.println("Topics2 = " + message);
    }
}


创建生产者

    @Test
    public void Topics() {
        rabbitTemplate.convertAndSend("topics", "user.update", "user.update 的消息");
        rabbitTemplate.convertAndSend("topics", "user.update.name", "user.update 的消息");
        rabbitTemplate.convertAndSend("topics", "order.test", "order.test 的消息");
        rabbitTemplate.convertAndSend("topics", "order.test.jst", "order.test.jst 的消息");
    }

Test执行结果如下,发送消息到topics交换机进行分发。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值