温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

RabbitMQ主题模式怎么实现

发布时间:2022-09-27 16:23:48 来源:亿速云 阅读:99 作者:iii 栏目:开发技术

这篇文章主要讲解了“RabbitMQ主题模式怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“RabbitMQ主题模式怎么实现”吧!

Springboot RabbitMQ 主题模式

生产者声明了三个队列和一个主题切换。队列分别与主题交换机绑定,并设置了路由键统一字符。如果路由键满足交换机和队列之间的通配符要求,则将消息存储在队列中

#通配符可以匹配一个或多个单词,*通配符可以匹配一个单词;如果Exchange和队列之间的routing key通配符是#hello.#,则表示中间所有带hello的routing key都满足条件,消息会被存入队列

导入 com.example.rabbitmq.constant.RabbitConstant;
导入 org.springframework.amqp.core.Binding;
导入 org.springframework.amqp.core.BindingBuilder;
导入 org.springframework.amqp.core.Queue;
导入 org.springframework.amqp.core.TopicExchange;
导入 org.springframework.context.annotation.Bean;
导入 org.springframework.context.annotation.Configuration;@Configuration public class RabbitTopicProvider { 
    @Bean 
    public Queue topicFirstQueue() { 
        return new Queue(RabbitConstant.TOPICS_FIRST_QUEUE_NAME); 
    } 
    @Bean
    公共队列 topicSecondQueue() { 
        return new Queue(RabbitConstant.TOPICS_SECOND_QUEUE_NAME);
    } 
    @Bean
    公共队列 topicThirdQueue() { 
        return new Queue(RabbitConstant.TOPICS_THIRD_QUEUE_NAME); 
    } 
    @Bean 
    public TopicExchange topicExchange() { 
        // 创建一个主题类型切换,表示交换机会发送消息到 routing_key 通配符匹配队列成功
        return new TopicExchange(RabbitConstant.TOPICS_EXCHANGE_NAME); 
    } 
    @Bean 
    public Binding topicFirstQueueBindExchange() { 
        // 绑定topic类型切换到队列1并设置routing_key通配符#first.#
        return BindingBuilder.bind(topicFirstQueue()).to(topicExchange()).with(RabbitConstant.TOPICS_ROUTING_KEY_FIRST_WILDCARD); 
    } 
    @Bean 
    public Binding topicSecondQueueBindExchange() { 
        //第二个队列绑定主题类型切换,设置路由_key通配符为* second.# 
        return BindingBuilder.bind(topicSecondQueue()).to(topicExchange()) .with(RabbitConstant.TOPICS_ROUTING_KEY_SECOND_WILDCARD); 
    } 
    @Bean 
    public Binding topicThirdQueueBindExchange() { 
        // 三个队列绑定主题切换,设置routing_key通配符为*third.*
        return BindingBuilder.bind(topicThirdQueue()).to(topicExchange()).with(RabbitConstant.TOPICS_ROUTING_KEY_THRID_WILDCARD); 
    } 
}

消费者监听队列并消费

导入 com.example.rabbitmq.constant.RabbitConstant;
导入 org.springframework.amqp.rabbit.annotation.RabbitHandler;
导入 org.springframework.amqp.rabbit.annotation.RabbitListener;
导入 org.springframework.stereotype.Component;
@Component public class RabbitTopicsConsumer { 
    @RabbitListener(queues = RabbitConstant.TOPICS_FIRST_QUEUE_NAME) 
    @RabbitHandler 
    public void topicFirstQueue(String context) { 
        System.out.println("rabbit topics queue first receiver:" + context); 
    } 
    @RabbitListener(queues = RabbitConstant.TOPICS_SECOND_QUEUE_NAME) 
    @RabbitHandler 
    public void topicSecondQueue(String context) {        System.out.println("兔子主题队列第二个接收者:" + context); 
    } 
    @RabbitListener(queues = RabbitConstant.TOPICS_THIRD_QUEUE_NAME) 
    @RabbitHandler 
    public void topicThirdQueue(String context) { 
        System.out.println("rabbit 主题队列第三个接收者:" + context); 
    } 
}

单元测试

@Test public void topics() { 
    // 向第一个队列发送消息。这时候队列可以接收到消息,因为队列的通配符是#first.#,而routing_key是topics first。路由。键,匹配成功
    rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_FIRST_QUEUE_ROUTING_KEY, "topics hello"); 
    // 向第二个队列发送消息。这时候队列也能收到消息了,因为队列的通配符是*秒#,而routing_key是topic秒。路由。键,匹配成功
    rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_SECOND_QUEUE_ROUTING_KEY, "topics hello");    // 向第三个队列发送消息。此时队列无法接受消息,因为队列通配符是*第三个*,而routing_key是topics第三个。路由。键,匹配失败
    rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_THIRD_QUEUE_ROUTING_KEY, "topics hello"); 
    }

响应结果

感谢各位的阅读,以上就是“RabbitMQ主题模式怎么实现”的内容了,经过本文的学习后,相信大家对RabbitMQ主题模式怎么实现这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI