RabbitMQ 快速入门

目录

目录

1、基本收发

2、Work Queues

3、Fanout(广播)交换机

4、Direct(定向)交换机

5、Topic(话题)交换机

6、声明队列和交换机——方法一

7、声明队列和交换机——方法二

8、消息转换器


1、基本收发

引入spring-boot-starter-amqp依赖

ymal文件写rabbitmq服务器信息

使用RabbitTemplate工具类

消息发送:

@Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    void testSendMessage2Queue(){
        String queueName = "simple.queue";
        String msg = "hello";
        rabbitTemplate.convertAndSend(queueName,msg);
    }

消息接收:使用注解@RabbitListener

@Slf4j
@Component
public class MqListen {
    
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg){
        System.out.println("消费者收到消息:"+msg);
    }
}

2、Work Queues

增加prefetch参数

修改后可以让处理快的消费者处理更多信息

 @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1收到消息:"+msg);
        Thread.sleep(20);
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2收到消息:"+msg);
        Thread.sleep(100);
    }

Work模型的使用

  1. 多个消费者绑定到一个队列,可以加快消息处理速度
  2. 同一条消息只会被一个消费者处理
  3. 通过设置prefetch来控制消费者预取的消息数量,处理完一条再处理下一条,实现能者多劳

3、Fanout(广播)交换机

将接收到的消息广播到每一个queue

向交换机发送消息

@Test
    void testSendFanout() throws InterruptedException {
        String exchangeName = "hmall.fanout";
        String msg = "test";
        rabbitTemplate.convertAndSend(exchangeName,null,msg);

    }

4、Direct(定向)交换机

bindingkey相同则广播

绑定时指定key

@Test
    void testSendDirect() throws InterruptedException {
        String exchangeName = "hmall.direct";
        String msgRed = "red";
        String msgBlue = "blue";
        String msgYellow = "yellow";
        rabbitTemplate.convertAndSend(exchangeName,"red",msgRed);
        rabbitTemplate.convertAndSend(exchangeName,"blue",msgBlue);
        rabbitTemplate.convertAndSend(exchangeName,"yellow",msgYellow);


    }

5、Topic(话题)交换机

@Test
    void testSendTopic() throws InterruptedException {
        String exchangeName = "hmall.topic";
        String msgchina = "china message";
        String msgnews = "news";
        String msgYellow = "wrong";
        rabbitTemplate.convertAndSend(exchangeName,"china.message",msgchina);
        rabbitTemplate.convertAndSend(exchangeName,"japan.news",msgnews);
        rabbitTemplate.convertAndSend(exchangeName,"japan.message",msgYellow);


    }

6、声明队列和交换机——方法一

@Configuration
public class FanoutConfiguration {
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("hmall.fanout2");
    }

    @Bean
    public Queue fanoutQueen3(){
        return new Queue("fanout.queue3");
    }
    @Bean
    public Queue fanoutQueen4(){
        return new Queue("fanout.queue4");
    }

    @Bean
    public Binding fanoutBinding3(){
        return BindingBuilder.bind(fanoutQueen3()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutBinding4(){
        return BindingBuilder.bind(fanoutQueen4()).to(fanoutExchange());
    }

7、声明队列和交换机——方法二

基于注解

 @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1",durable = "true"),
            exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1收到direct.queue1消息:"+msg);
        //Thread.sleep(20);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2",durable = "true"),
            exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String msg) throws InterruptedException {
        System.out.println("消费者1收到direct.queue2消息:"+msg);
        //Thread.sleep(20);
    }

8、消息转换器

json引入依赖 实现消息转换器

<!-- Jackson依赖 -->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.13.5</version>
        </dependency>
 @Bean
    public MessageConverter jacksonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值