SpringBoot整合五种RabbitMQ模式

1 搭建初始环境

1.1 引入依赖

<dependency>
  <groupId>org.springframework.amqp</groupId>
  <artifactId>spring-rabbit-test</artifactId>
  <scope>test</scope>
</dependency>

1.2 配置配置文件

spring:
  application:
    name: springboot-rabbitmq
  rabbitmq:
    host: localhost
    port: 5672
    username: xin
    password: xin
    virtual-host: xin

2 第一种hello world 模型的使用

2.1 开发生产者

@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
    //注入RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public void sendMessage(){
        rabbitTemplate.convertAndSend("hello","hello Han Han Xin");
    }
}

2.2 开发消费者

@Component
//可以在@Queue中设置消息队列的一些属性,比如是否可以持久化,是否自动删除
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class Consumer1 {
    @RabbitHandler
    public void receive1(String message){
        System.out.println("消费者获取的消息:" + message);
    }
}

3 第二种 (Work Queue)工作队列

3.1 开发生产者

@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
    //注入RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public void sendMessageForWork(){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work","发送work模型的消息" + i);
        }

    }
}

3.2 开发消费者

@Component
public class WorkMessage {
    @RabbitListener(queuesToDeclare = @Queue(value = "work"))
    public void consumer1(String message){
        System.out.println("消费者 1:" + message);
    }
    @RabbitListener(queuesToDeclare = @Queue(value = "work"))
    public void consumer2(String message){
        System.out.println("消费者 2:" + message);
    }
}

3.3 说明

默认在spring AMQP实现中work这个方式是公平调度,如果需要实现能者多劳需要额外配置。

4 第四种 广播模型

4.1 开发生产者

@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
    //注入RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
  //fanout 广播
    @Test
    public void  sendMessageForFanout(){
        rabbitTemplate.convertAndSend("logs","","Send Message For Fanout");
    }
}

4.2 开发消费者

@Component
public class FanoutConsumer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "logs", type = "fanout")//绑定交换机
            )
    })
    public void consumer1(String message){
        System.out.println("consumer1:" + message);
    }
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "logs", type = "fanout")//绑定交换机
            )
    })
    public void consumer2(String message){
        System.out.println("consumer1:" + message);
    }
}

5 第五种 Routing 路由模式

5.1 开发生产者

@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
    // 注入RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
   // 路由模式 routing
    @Test
    public void SendMessageForRouting(){
        rabbitTemplate.convertAndSend("directs","error","Send Message For Direct");
    }
}

5.2 开发消费者

@Component
public class Consumer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "directs", type = "direct"),
                    key = {"info", "error", "warn"}
            )
            }
    )
    public void consumer1(String message){
        System.out.println("consumer1:" + message);
    }

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

6 第六种 动态路由Topic

6.1 开发生产者

@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
    // 注入RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
   //动态路由 Topic
    @Test
    public void SendMessageForTopic(){
        //参数二 设置不同的值,可以使不同的消费者接收到发送的消息
        rabbitTemplate.convertAndSend("RTopics","order","Send Message For Topics");
    }
}

6.2 开发消费者

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

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(name = "RTopics", type = "topic"),
                    key = {"order.#", "product.#","user.*"}
            )
    })
    public void consumer2(String message){
        System.out.println("consumer2:" + message);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码不能跑我能跑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值