前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot整合RabbitMQ

SpringBoot整合RabbitMQ

作者头像
一只胡说八道的猴子
发布2021-03-15 22:06:45
2.6K0
发布2021-03-15 22:06:45
举报

SpringBoot整合RabbitMQ

生产者

整合步骤概述

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

1.创建生产者SpringBoot工程

在这里插入图片描述
在这里插入图片描述

2.导入依赖坐标

代码语言:javascript
复制
<!--继承父类工程-->
    <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配置,基本信息配置

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

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

代码语言:javascript
复制
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,调用方法,完成消息发送

代码语言:javascript
复制
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.导入依赖坐标

代码语言:javascript
复制
 <!--继承父类工程-->
    <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配置,基本信息配置

代码语言:javascript
复制
spring:
  rabbitmq:
    virtual-host: 121.196.111.120

4.编写启动类

代码语言:javascript
复制
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.编写消息监听处理类

代码语言:javascript
复制
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);
    }
}
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-03-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot整合RabbitMQ
    • 生产者
      • 整合步骤概述
      • 1.创建生产者SpringBoot工程
      • 2.导入依赖坐标
      • 3.编写yml配置,基本信息配置
      • 4.定义交换机,队列以及绑定关系的配置类
      • 5.注入RabbitTemplate,调用方法,完成消息发送
    • 消费者
      • 整合步骤概述
        • 1.创建生产者SpringBoot工程
          • 2.导入依赖坐标
            • 3.编写yml配置,基本信息配置
              • 4.编写启动类
                • 5.编写消息监听处理类
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                http://www.vxiaotou.com