• 02.RabbitMQ整合springboot简单使用


    1.添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    

    2. 配置yml文件

    spring:
      rabbitmq:
        host: ip
        port: 5672
        password: *****
        username: *****
        virtual-host: /dev_virtual
    
    

    3.引入springboot中的注解和类

    • 启动类增加 @EnableRabbit注解
    • 引入rabbitmq管理类
       @Autowired
        private RabbitTemplate rabbitTemplate;//用于消息处理
    
        @Autowired
        private RabbitAdmin rabbitAdmin; //用于管理组件
    
    

    4.测试DIRECT模式消息队列

    • 代码创建exchange和queue
      @Test
        public void buildRabbit(){
            //交换机
            Exchange exchange = new ExchangeBuilder("exchange_direct", ExchangeTypes.DIRECT).durable(true).build();
            rabbitAdmin.declareExchange(exchange);
            //队列
            Queue queue = QueueBuilder.durable("queue_lpf").build();
            rabbitAdmin.declareQueue(queue);
            //绑定
            Binding binding = BindingBuilder.bind(queue).to(exchange).with("mq_key").noargs();
            rabbitAdmin.declareBinding(binding);
            String beanName = rabbitAdmin.getBeanName();
            System.out.println("buildRabbit"+beanName);
        }
    
    
    • provider发送消息
      @Test
        public void sendRabbitInfo() {
            CorrelationData data = new CorrelationData(UUID.randomUUID().toString().replace("-", ""));
            Book book = new Book("红楼梦","曹雪芹");
            rabbitTemplate.convertAndSend("exchange_direct", "mq_key", sendMessage,data);
        }
    
    
    • consumer接收发送的消息
      @RabbitListener(queues="queue_lpf")
      public void getMessage(Map message){
          System.out.println("********************************************************");
          System.out.println(message);
          System.out.println("********************************************************");
      }
    
    

    5.测试TOPIC模式发送数据

    • 代码创建EXCHANGE 和 QUEUE
    @Test
        public void buildTopicRabbit(){
            Exchange topicExchange = new ExchangeBuilder("topic_exchange", ExchangeTypes.TOPIC).durable(true).build();
            rabbitAdmin.declareExchange(topicExchange);
            Queue queue1 = QueueBuilder.durable("queue_topic").build();
            rabbitAdmin.declareQueue(queue1);
            Queue queue2 = QueueBuilder.durable("queue_lpf").build();
            rabbitAdmin.declareQueue(queue2);
            Queue queue3 = QueueBuilder.durable("topic_lpf").build();
            rabbitAdmin.declareQueue(queue3);
            //队列和交换器绑定
            Binding binding = BindingBuilder.bind(queue1).to(topicExchange).with("#.topic").noargs();
            Binding binding1 = BindingBuilder.bind(queue2).to(topicExchange).with("queue.#").noargs();
            Binding binding2 = BindingBuilder.bind(queue3).to(topicExchange).with("#.lpf").noargs();
            rabbitAdmin.declareBinding(binding);
            rabbitAdmin.declareBinding(binding1);
            rabbitAdmin.declareBinding(binding2);
        }
    
    • privider 发送消息
     @Test
        public void sendMsg(){
            CorrelationData data = new CorrelationData(UUID.randomUUID().toString().replace("-", ""));
            String message="this is a topic message(queue.lpf is the routingkey)";
            rabbitTemplate.convertAndSend("topic_exchange","queue.lpf", (Object) message,data);
        }
    
    • 在management管理页面查看收到的信息

    6.遇到的问题

    问题1:org.springframework.amqp.AmqpIllegalStateException: Fatal exception on listener startup

    看到很多人说是rabbitmq账号的权限问题,经过多番测试无果。
    根据 报错详细,发现是有一个@RabbitListener 监听队列 ,这个队列没有创建。所以造成了这个问题。
    注释掉久可以了。
    
  • 相关阅读:
    虚拟机搭建FISCO BCOS的区块链浏览器
    linux下安装cmake
    Compile error: Cannot find a C++ compiler that supports both C++11 and the specified C++ flags
    Mac安装CMake
    Mac下如何添加User到group中
    Macos 安装md5sum、sha1sum、md5deep、sha1deep
    Support Vector Regression(SVR) 资料
    Python pandas dataframe
    python用pd.read_csv()方法来读取csv文件
    python pandas 交叉表, 透视表
  • 原文地址:https://www.cnblogs.com/perferect/p/12980539.html
Copyright © 2020-2023  润新知