• [MQ] SpringBoot使用扇型(广播)交换机/主题交换机


    ✨✨个人主页:沫洺的主页

    📚📚系列专栏: 📖 JavaWeb专栏📖 JavaSE专栏 📖 Java基础专栏📖vue3专栏 

                               📖MyBatis专栏📖Spring专栏📖SpringMVC专栏📖SpringBoot专栏

                               📖Docker专栏📖Reids专栏📖MQ专栏📖SpringCloud专栏     

    💖💖如果文章对你有所帮助请留下三连✨✨

    🙈SpringBoot使用扇型(广播)交换机(Fanout Exchange)

    消费者

    不需要routing key

    扇型交换机在接收到消息后,会直接转发到绑定到它上面的所有队列。

    1. @Component
    2. public class FanoutConsumer {
    3. private static final String ENAME = "211-FanoutExchage-01";
    4. private static final String QNAME1 = "211-FanoutQueue-11";
    5. private static final String QNAME2 = "211-FanoutQueue-12";
    6. //定义一个交换机
    7. @Bean
    8. public FanoutExchange fanoutExchange(){
    9. return new FanoutExchange(ENAME, true, false);
    10. }
    11. //定义一个队列
    12. @Bean
    13. public Queue fanoutQueue1(){
    14. return QueueBuilder.durable(QNAME1).build();
    15. }
    16. //定义一个队列
    17. @Bean
    18. public Queue fanoutQueue2(){
    19. return QueueBuilder.durable(QNAME2).build();
    20. }
    21. //创建队列和交换机的绑定关系
    22. @Bean
    23. public Binding binding1(){
    24. return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    25. }
    26. //创建队列和交换机的绑定关系
    27. @Bean
    28. public Binding binding2(){
    29. return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
    30. }
    31. @RabbitHandler
    32. @RabbitListener(queues =QNAME1)
    33. public void process1(UserRegisterOk userRegisterOk){
    34. System.out.println("消费者1收到:"+userRegisterOk.getName()+","+userRegisterOk.getPhone());
    35. }
    36. @RabbitHandler
    37. @RabbitListener(queues =QNAME2)
    38. public void process2(UserRegisterOk userRegisterOk){
    39. System.out.println("消费者2收到:"+userRegisterOk.getName()+","+userRegisterOk.getPhone());
    40. }
    41. }

    生产者

    1. @Component
    2. public class FanoutProducer {
    3. @Autowired
    4. private RabbitTemplate rabbitTemplate;
    5. public void sendMessage(){
    6. UserRegisterOk userRegisterOk = UserRegisterOk.builder().name("张三").phone("123456").build();
    7. //要将对象序列化,转成字符串,使用消息转换器MessageConverter
    8. rabbitTemplate.convertAndSend("211-FanoutExchage-01","",userRegisterOk);
    9. System.out.println("生产者生产-->张三注册成功");
    10. }
    11. }

    启动类

    1. @SpringBootApplication
    2. public class App1 {
    3. public static void main(String[] args) {
    4. ConfigurableApplicationContext context = SpringApplication.run(App1.class, args);
    5. FanoutProducer producer = context.getBean(FanoutProducer.class);
    6. producer.sendMessage();
    7. }
    8. }

    🙉SpringBoot使用主题交换机(Topic Exchange )

    消费者

    路由键和绑定键之间是有规则的

    • * 代表两点之间一个占位单词
    • # 代表后面所有,匹配所有
    1. @Component
    2. public class TopicConsumer {
    3. private static final String ENAME = "211-TopicExchage-01";
    4. private static final String QNAME1 = "211-TopicQueue-刘亦菲";
    5. private static final String QNAME2 = "211-TopicQueue-关晓彤";
    6. private static final String QNAME3 = "211-TopicQueue-邓紫棋";
    7. //定义一个交换机
    8. @Bean
    9. public TopicExchange topicExchange(){
    10. return new TopicExchange(ENAME, true, false);
    11. }
    12. //定义一个队列
    13. @Bean
    14. public Queue topicQueue1(){
    15. return QueueBuilder.durable(QNAME1).build();
    16. }
    17. //定义一个队列
    18. @Bean
    19. public Queue topicQueue2(){
    20. return QueueBuilder.durable(QNAME2).build();
    21. }
    22. //定义一个队列
    23. @Bean
    24. public Queue topicQueue3(){
    25. return QueueBuilder.durable(QNAME3).build();
    26. }
    27. //创建队列和交换机的绑定关系
    28. @Bean
    29. public Binding binding1(){
    30. return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("1.9.*");
    31. }
    32. //创建队列和交换机的绑定关系
    33. @Bean
    34. public Binding binding2(){
    35. return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("1.8.*");
    36. }
    37. //创建队列和交换机的绑定关系
    38. @Bean
    39. public Binding binding3(){
    40. return BindingBuilder.bind(topicQueue3()).to(topicExchange()).with("1.#");
    41. }
    42. //三个消费者
    43. @RabbitHandler
    44. @RabbitListener(queues =QNAME1)
    45. public void process1(UserRegisterOk userRegisterOk){
    46. System.out.println("刘亦菲收到:"+userRegisterOk.getName()+","+userRegisterOk.getHeight());
    47. }
    48. @RabbitHandler
    49. @RabbitListener(queues =QNAME2)
    50. public void process2(UserRegisterOk userRegisterOk){
    51. System.out.println("关晓彤收到:"+userRegisterOk.getName()+","+userRegisterOk.getHeight());
    52. }
    53. @RabbitHandler
    54. @RabbitListener(queues =QNAME3)
    55. public void process3(UserRegisterOk userRegisterOk){
    56. System.out.println("邓紫棋收到:"+userRegisterOk.getName()+","+userRegisterOk.getHeight());
    57. }
    58. }

    生产者

    1. @Component
    2. public class TopicProducer {
    3. @Autowired
    4. private RabbitTemplate rabbitTemplate;
    5. public void sendMessage(){
    6. UserRegisterOk userRegisterOk = UserRegisterOk.builder().name("moming").phone("123456").height("1.8.5").build();
    7. //要将对象序列化,转成字符串,使用消息转换器MessageConverter
    8. rabbitTemplate.convertAndSend("211-TopicExchage-01",userRegisterOk.getHeight(),userRegisterOk);
    9. System.out.println("生产者生产-->moming");
    10. }
    11. }

    启动类 

    1. @SpringBootApplication
    2. public class App2 {
    3. public static void main(String[] args) {
    4. ConfigurableApplicationContext context = SpringApplication.run(App2.class, args);
    5. TopicProducer producer = context.getBean(TopicProducer.class);
    6. producer.sendMessage();
    7. }
    8. }

  • 相关阅读:
    阿里云使用记录
    178. 分数排名
    老生常谈:学习Java自学好还是报培训班?
    java-php-net-python-工会管理系统计算机毕业设计程序
    深入剖析 Linux Cgroups 子系统:资源精细管理
    基于Matlab求解高教社杯全国大学生数学建模竞赛(CUMCM2003B题)-露天矿生产的车辆安排(附上源码+数据+题目)
    [Java/力扣160]相交链表
    JDBC SQL Server Source Connector: 一览与实践
    win11,安装python,pip,和opencv
    消息消费过程
  • 原文地址:https://blog.csdn.net/HeyVIrBbox/article/details/127875526