当前位置:   article > 正文

RabbitMQ和SpringBoot整合之填坑 com.rabbitmq.client.ShutdownSignalException: channel error protocol method:

com.rabbitmq.client.shutdownsignalexception: channel error; protocol method:

RabbitMQ消息中间连两端链接上消息生产者,消息消费者

但是在整合SpringBoot,且版本在

<spring-boot.version>2.6.13</spring-boot.version>

消息消费者,启动报错

消息发送者消息发送报错,

com.rabbitmq.client.ShutdownSignalException: channel error protocol method:

追踪问题根源是,队列,交换机,绑定这些都不存在,所以出现了问题,

解决办法很见简单

1.自己手动到RabbitMQ里面创建相关的交换机,队列,绑定关系

2.代码端解决

消息消费者

使用如下

  1. package com.example.rabbitmqb.config;
  2. import org.springframework.amqp.core.Message;
  3. import org.springframework.amqp.rabbit.annotation.Queue;
  4. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  5. import org.springframework.stereotype.Component;
  6. import java.io.UnsupportedEncodingException;
  7. @Component
  8. public class RabbitMQConfig {
  9. @RabbitListener(queuesToDeclare = {@Queue("boot_queue")})
  10. public void ListenerQueues(Message message) throws UnsupportedEncodingException {
  11. System.out.println(new String(message.getBody(), "UTF-8"));
  12. }
  13. }

消息生产端配置类

  1. package com.example.rabbitmqa.config;
  2. import org.springframework.amqp.core.*;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  6. import org.springframework.amqp.rabbit.core.RabbitAdmin;
  7. import org.springframework.context.annotation.Bean;
  8. @Configuration
  9. public class RabbitMQConfig2 {
  10. // @Bean
  11. // RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
  12. // return new RabbitAdmin(connectionFactory);
  13. // }
  14. public static final String EXCHANGE_NAME = "boot_topic_exchange";
  15. public static final String QUEUE_NAME = "boot_queue";
  16. //1.交换机
  17. @Bean("bootExchange")
  18. public TopicExchange bootExchange(RabbitAdmin rabbitAdmin) {
  19. TopicExchange exchange = new TopicExchange(EXCHANGE_NAME);
  20. rabbitAdmin.declareExchange(exchange);
  21. return exchange;
  22. }
  23. //2.Queue 队列
  24. @Bean("bootQueue")
  25. public Queue bootQueue(RabbitAdmin rabbitAdmin) {
  26. Queue queue = new Queue(QUEUE_NAME, true);
  27. rabbitAdmin.declareQueue(queue);
  28. return queue;
  29. }
  30. //3. 队列和交互机绑定关系 Binding
  31. /*
  32. 1. 知道哪个队列
  33. 2. 知道哪个交换机
  34. 3. routing key
  35. */
  36. @Bean
  37. public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,
  38. @Qualifier("bootExchange") Exchange exchange,
  39. RabbitAdmin rabbitAdmin) {
  40. Binding binding = BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
  41. rabbitAdmin.declareBinding(binding);
  42. return binding;
  43. }
  44. //创建初始化RabbitAdmin对象
  45. @Bean
  46. public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
  47. RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
  48. // 只有设置为 true,spring 才会加载 RabbitAdmin 这个类
  49. rabbitAdmin.setAutoStartup(true);
  50. return rabbitAdmin;
  51. }
  52. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/484416
推荐阅读
相关标签
  

闽ICP备14008679号