SpringBoot集成Rabbitmq

19 篇文章 0 订阅
4 篇文章 0 订阅

生产者

一.创建SpringBoot模块/工程

可以使用Ider的快速创建SpringBoot模板/工程,也可以选择’Maven’来创建SpringBoot模块/工程
(此处,我自己在创建生产者模块时/工程时,是使用自己选择’Maven’来手动创建的)

二.在Pom文件中添加依赖.

在这里插入图片描述


     <!-- SpringBoot的核心依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>

    <dependencies>
        <!-- springBoot集成amqp(Rabbitmq)的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <!-- springBoot中集成单元测试的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>


    </dependencies>

三.创建SpringBoot引导启动类

示例代码:

package com.rabbitmq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class);
    }

}



在这里插入图片描述

四.配置yaml文件

需要在springboot所引用的yaml配置文件(通常名字为resources目录下的application.yaml)中定义rabbitmq的服务器配置信息.

示例:

spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    virtual-host: /lele
    username: lele
    password: lele

五.创建Rabbitmq队列和交换机的配置类

创建一个Rabbitmq队列和交换机的信息配置类.
该类的作用分别是
1.定义交换机的名字和类型,属性
2.定义队列名字,属性(是否持久化等)
3.定义某个队列和某个交换机绑定在一起.

注意:因为是SpringBoot集成,因此该类所在包路径不能同SpringBoot引导启动类所在的包路径同级. 该类的所在包路径必须在SpringBoot引导启动类所在包路径的下级.

在这里插入图片描述

示例代码:

package com.rabbitmq.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 RabbitmqConfig {

    //1.配置交换机
    @Bean("bootExchange")//此处为注入标识符,可以自定义
    public Exchange bootExchange(){

        //通过'ExchangBuilder'对象构建交换机对象,并定义交换机类型
        // directExchange方法为direct(routing路由模式)类型,参数值为字符串的交换机名
        // headersExchange方法为普通类型,参数值为字符串的交换机名
        // fanoutExchange方法为fanout(广播模式)类型,参数值为字符串的交换机名
        // topicExchange方法为通配符模式类型,参数值为字符串的交换机名
        // build()方法为构建.即:在使用了交换机名,是否持久化等方法后,在结尾使用'build()'方法进行构建.

    return ExchangeBuilder.topicExchange("boot_topic_exchange").durable(true).build();

    }

    //2.声明queue队列
    @Bean("bootQueue")//此处为注入标识符,可以自定义
    public Queue bootQueue(){

        //通过'QueueBuilder'对象构建队列对象
        // durable 为持久化队列,使用该方法时需要传递字符串作为队列名
        // noDurable 为不持久化队列
        //build()方法为构建.即:在使用了队列方法后,在结尾使用'build()'方法进行构建.
        return QueueBuilder.durable("queue_name").build();




    }

    //3.绑定队列和交换机

    @Bean //此处不需要写注入标识符
    // 此处 @Qualifier的Queue方法形参的注解标识符要同队列和交换机的Bean注解标识符一致.此注解的目的是为了在多个队列和交换机Bean存在的情况下,接收特定的队列和交换机到形参处.
    //
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queueObj,@Qualifier("bootExchange") Exchange exchangeObj){

        /*
        通过'BindingBuilder'对象构建绑定对象(即:该对象用于绑定队列和交换机

        'bind()'方法内传入的参数为Queue对象的形参,即:queueObj
        "to()"方法有五种重载方法,分别应用于Topic,Direct,HeadersExchange,FanoutExchange,Exchange五种模式
        "with()"方法用于指定routingkey
        如果需要指定参数,后面接上and方法(),不需要的话接上noargs()方法
         */
        return BindingBuilder.bind(queueObj).to(exchangeObj).with("boot.#").noargs();

    }
}

六.创建测试类,进行消息发送

在这里插入图片描述
示例代码:

package com.rabbitmq.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class BootRabbitmqOfProducer {

    //发送处
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSend(){

        rabbitTemplate.convertAndSend("boot_topic_exchange", "boot.ceshi.test", "你好!消息测试");



    }

}

消费者

一.创建SpringBoot模块/工程

可以使用Ider的快速创建SpringBoot模板/工程,也可以选择’Maven’来创建SpringBoot模块/工程
(此处,我自己在创建消费者模块时/工程时,是使用ider的’快速创建SpringBoot’来完成的,而创建时并没有可供选择的amqp依赖,所以需要我们自己导入)

二.在Pom文件中添加依赖

在这里插入图片描述

  <!-- SpringBoot的核心依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <dependencies>
        <!-- springBoot集成amqp(Rabbitmq)的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
     </dependencies>

三.配置yaml文件

spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    virtual-host: /lele
    username: lele
    password: lele

四.配置监听器

在这里插入图片描述
示例代码:

package com.springbootrabbitmqconsumer.springbootofrabbitmqconsumer.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component//用于被Spring所识别管理
public class ConsumerListener {


    @RabbitListener(queues = "queue_name")//该注解写在一个方法上,代表着该方法不会被自动停止,会持续监听某个队列. queues的属性值为要监听的队列名
    public void listenerQueue(Message messageObj){ //Message用于封装接收到的数据
        //获取配置信息
        String contentType = messageObj.getMessageProperties().getContentType();
        System.out.println("配置信息-contentType:"+contentType);

        //获取传递过来的消息
        byte[] body = messageObj.getBody();
        System.out.println("消息内容为:"+new String(body));

        System.out.println("↓以下为整体数据信息↓");
        System.out.println(messageObj);

    }
}

注意:运行SpringBoot启动引导类即可进行消息接收(即:运行监听器).

示范源码下载

链接: https://pan.baidu.com/s/1eeU425SdW8IIDrpGGlcxYQ
提取码: hnbw

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值