rabbitmq 企业开发实战-topic

rabbitmq 企业开发实战-topic

场景分析

我们继续针对上篇direct 中的日志系统来分析,假设我们又来了个新需求,就是对error 里的业务日志也要拆分一些出来处理,比如订单的状态日志需要存到日志A库,商品的日志就存到日志B库 图解分析一下

在这里插入图片描述


提示:以下是本篇文章正文内容,下面案例可供参考

一、定义topic 交换机以及队列绑定

代码如下(示例):
package net.getbang.rabbitmq.topic;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**

  • 定义并初始化topic 交换机,队列,以及将队列绑定到 交换机上
    /
    @Configuration
    public class TopicBuildConfig {
    /
    *

    • 定义一个Topic类型的交换机
    • @return
      */
      @Bean
      TopicExchange topicExchange(){
      return new TopicExchange(“topic-test”);
      }

    /**

    • 定义保存数据库A队列
      /
      @Bean
      public Queue queueTSaveText(){
      return new Queue(“topic.dbA”);
      }
      /
      *
    • 定义保存数据库B队列
      */
      @Bean
      public Queue queueTSaveDB(){
      return new Queue(“topic.dbB”);
      }

    /**

    • 将队列绑定到交换机上
      */
      @Bean
      Binding bindingTopicExchangeDbA(Queue queueTSaveText, TopicExchange topicExchange) {
      return BindingBuilder.bind(queueTSaveText).to(topicExchange).with(“order.#”);
      }

    @Bean
    Binding bindingTopicExchangeDbB(Queue queueTSaveDB, TopicExchange topicExchange) {
    return BindingBuilder.bind(queueTSaveDB).to(topicExchange).with(“product.#”);
    }

}

二、定义消息发送方法

1.引入库

发送的方法跟direct 没有区别

代码如下(示例):

package net.getbang.rabbitmq.producer;


import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Topic 消息发送
 */
@Component
public class TopicProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendDirectMessage(String exchangeName,String routingKey, Object message){

        rabbitTemplate.convertAndSend(exchangeName,routingKey,message);
    }

}

2.消息监听

代码如下(示例):

package net.getbang.rabbitmq.listener;

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

/**
 *
 */
@Component
public class TopicListener {

    @RabbitListener(queues = "topic.dbA")
    public void reviceFanout(Object message){

        System.out.println("TopicListener.reviceDirect 接收的消息:"+message);

        System.out.println("保存到数据库A");

    }
    @RabbitListener(queues = "topic.dbB")
    public void reviceFanout2(Object message){

        System.out.println("TopicListener.reviceDirect2 接收的消息:"+message);
        System.out.println("保存到数据库B");
    }


}

测试发送,接收
@Autowired
TopicProducer topicProducer;

@Test
public void  sendTopicMessage(){




    topicProducer.sendDirectMessage("topic-test","order.update","更新订单");
    topicProducer.sendDirectMessage("topic-test","order.save","保存订单");


    topicProducer.sendDirectMessage("topic-test","product.save","保存产品");

}

这里我们发送的三条信息,看下接收结果。
在这里插入图片描述

为什么只接了两个???。后续解答

总结

相对于diretc ,topic交换机只是在它的基础上增加了类似模糊查询的功能,direct 类似于精确查询

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小海聊智造

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

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

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

打赏作者

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

抵扣说明:

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

余额充值