zoukankan      html  css  js  c++  java
  • RabbitMQ系列5 SpringBoot整合RabbitMQ

    SpringBoot整合RabbitMQ

    生产者

    整合步骤概述

    1.创建生产者SpringBoot工程
    2.导入依赖坐标
    3.编写yml配置,基本信息配置
    4.定义交换机,队列以及绑定关系的配置类
    5.注入RabbitTemplate,调用方法,完成消息发送

    1.创建生产者SpringBoot工程

    在这里插入图片描述

    2.导入依赖坐标

    <!--继承父类工程-->
        <parent>
            <artifactId>spring-boot-starter-parent</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>2.1.5.RELEASE</version>
        </parent>
        <!--所需依赖-->
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
        </dependencies>
    

    3.编写yml配置,基本信息配置

    spring:
      rabbitmq:
        host: 121.196.111.120 #ip
        port: 5672 #端口
        username: guest #用户名
        password: guest #密码
        virtual-host: /demo #虚拟机名称
    

    4.定义交换机,队列以及绑定关系的配置类

    package com.pjh.Config;
    
    import org.springframework.amqp.core.*;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class RabbitConfig {
        /*定义交换机名称*/
        public static  final String EXCHANGE_NAME="boot_topic_exchange";
        /*定义队列名称*/
        public static final String QUEUE_NAME = "boot_queue";
        /*交换机*/
        @Bean("bootExchange")
        public Exchange bootExchange(){
            return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
        }
       /* 2.Queue 队列*/
        @Bean("bootQueue")
        public Queue bootQueue(){
            return QueueBuilder.durable(QUEUE_NAME).build();
        }
        /*队列和交换机绑定关系*/
        /*
        * 知道哪个队列
        * 知道哪个交换机
        * routing key
        * */
        @Bean
        public Binding getBinding(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
            return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
        }
    }
    
    

    5.注入RabbitTemplate,调用方法,完成消息发送

    package com.pjh.test;
    
    import com.pjh.Config.RabbitConfig;
    import org.junit.runner.RunWith;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import javax.print.DocFlavor;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class Test {
        /*注入RabbitTemplate*/
        @Autowired
        private RabbitTemplate rabbitTemplate;
        @org.junit.Test
        public void testSend(){
            rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME,"boot.haha","boot mq hello~~~");
        }
    }
    
    

    消费者

    整合步骤概述

    1.创建生产者SpringBoot工程
    2.导入依赖坐标
    3.编写yml配置,基本信息配置
    4.编写启动类
    5.编写消息监听处理类
    6.测试

    1.创建生产者SpringBoot工程

    在这里插入图片描述

    2.导入依赖坐标

     <!--继承父类工程-->
        <parent>
            <artifactId>spring-boot-starter-parent</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>2.1.5.RELEASE</version>
        </parent>
        <!--所需依赖-->
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
        </dependencies>
    

    3.编写yml配置,基本信息配置

    spring:
      rabbitmq:
        virtual-host: 121.196.111.120
    

    4.编写启动类

    package com.pjh;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class);
        }
    }
    
    

    5.编写消息监听处理类

    package com.pjh.Listener;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    @Component
    public class MyListen {
        /**
         * 监听某个队列的消息
         * @param message 接收到的消息
         */
        @RabbitListener(queues = "boot_queue")
        public void myListener1(String message){
            System.out.println("消费者接收到的消息为:" + message);
        }
    }
    
  • 相关阅读:
    go 语言之fmt.Sprintf格式化使用
    golang 中string和int类型相互转换
    GO 获取时间的日期函数、时间戳函数
    go 字符串切割方法小结
    php获取中英文字符串字符长度mb_strlen,字节长度strlen
    linux 之 grep 命令
    linux 无界面环境安装chrome,chromedriver,selenium
    kafka 常用命令总结
    linux中系统中 /bin、/sbin、/usr/bin、/usr/sbin、/usr/local/bin、/usr/local/sbin 目录的含义及区别
    3306端口被占用导致MySQL无法启动
  • 原文地址:https://www.cnblogs.com/pjhaymy/p/14527770.html
Copyright © 2011-2022 走看看