当前位置:   article > 正文

springboot整合rabbitMQ系列1 启动服务后便自动创建交换机,队列,绑定关系等

springboot整合rabbitMQ系列1 启动服务后便自动创建交换机,队列,绑定关系等

参考博文
SpringBoot整合rabbitMQ,启动服务便创建交换机和队列_rabbitmq配置类在启动时就创建-CSDN博客

启动服务不默认先创建队列的话,在启动消费者时候,消费者服务监听会找不到对应的队列而报错,启动失败

1 application.yml配置

  1. server:
  2. port: 8021
  3. spring:
  4. #给项目来个名字
  5. application:
  6. name: rabbitmq-test
  7. #配置rabbitMq 服务器
  8. rabbitmq:
  9. host: 127.0.0.1
  10. port: 5672
  11. username: need
  12. password: 123456
  13. #虚拟host 可以不设置,使用server默认host
  14. virtual-host: /testhost

2 交换机,队列,绑定等 配置类,核心两个方法拉到最后,其他方法就是正常的创建交换机,队列,绑定等,另外RabbitAdmin 加了@Lazy,解决循环依赖

  1. import org.springframework.amqp.core.*;
  2. import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  3. import org.springframework.amqp.rabbit.core.RabbitAdmin;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.Lazy;
  8. import java.util.HashMap;
  9. @Configuration
  10. public class ExchangeQueueBindingConfig {
  11. //自动创建就是靠他
  12. //开启懒加载,不然会有循环依赖问题
  13. @Lazy
  14. @Autowired
  15. RabbitAdmin rabbitAdmin;
  16. //死信队列
  17. @Bean
  18. public Queue createShiXinQueue() {
  19. return new Queue("shixin_queue", true);
  20. }
  21. //死信交换机
  22. @Bean
  23. public DirectExchange createShiXinExchange() {
  24. return new DirectExchange("shixin_exchange", true, false);
  25. }
  26. //死信交换机 死信队列 绑定
  27. @Bean
  28. public Binding createShiXinBinding() {
  29. return BindingBuilder.bind(createShiXinQueue()).to(createShiXinExchange()).with("shixin");
  30. }
  31. //正常队列
  32. @Bean
  33. public Queue createZhengChangQueue() {
  34. HashMap<String, Object> map = new HashMap<String, Object>();
  35. map.put("x-dead-letter-exchange", "shixin_exchange");
  36. map.put("x-dead-letter-routing-key", "shixin");
  37. return new Queue("zhengchang_queue", true, false, false, map);
  38. }
  39. //正常交换机
  40. @Bean
  41. public TopicExchange createZhengChangExchange() {
  42. return new TopicExchange("zhengchang_exchange", true, false);
  43. }
  44. //正常交换机 正常队列 绑定
  45. @Bean
  46. public Binding createZhengChangBinding() {
  47. return BindingBuilder.bind(createZhengChangQueue()).to(createZhengChangExchange()).with("chongqing.#");
  48. }
  49. //创建初始化RabbitAdmin对象
  50. @Bean
  51. public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
  52. RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
  53. // 只有设置为 true,spring 才会加载 RabbitAdmin 这个类
  54. rabbitAdmin.setAutoStartup(true);
  55. return rabbitAdmin;
  56. }
  57. //创建交换机和对列
  58. @Bean
  59. public void createExchangeQueue() {
  60. //死信
  61. rabbitAdmin.declareExchange(createShiXinExchange());
  62. rabbitAdmin.declareQueue(createShiXinQueue());
  63. //正常
  64. rabbitAdmin.declareExchange(createZhengChangExchange());
  65. rabbitAdmin.declareQueue(createZhengChangQueue());
  66. }
  67. }

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

闽ICP备14008679号