SpringBoot集成RabbitMQ消息中间件

有了SpringBoot框架,我们开发RabbitMQ会十分便捷。

	首先需要引入pom文件:
		<!--引入rabbitmq 的springboot依赖,这个依赖需要在接收方和发送方同时引用-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
	然后我们需要在配置文件加入  rabbitMq相关的配置信息,这个需要在发送方和接收方同时配置。
spring
   rabbitmq:
      host: 127.0.0.1
      port: 5672
      username: guest
      password: guest
      virtual-host: /

发送方:

	加入配置文件:
package org.jeecg.rabbitmq发送方.boot.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;



/**
 * @Author: xiaoduo
 * @Email 13333604529@163.com
 * @date: 2021-12-16 17:14
 * @discription: mq 配置类
 **/
@Configuration
public class RabbitMQConfig {
    public static final String EXCHANGE_NAME = "boot_topic_exchange";
    public static final String QUEUE_NAME = "boot_queue";

    //1.交换机
    @Bean("botExchange")
    public Exchange  botExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }



    //2.queue 队列
    @Bean("bootQueue")
    public Queue bootQueue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }



    //3.队列和交换的绑定
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("botExchange") Exchange exchange ){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }


}

	测试Controller:
package org.jeecg.rabbitmq发送方.boot;

import org.jeecg.rabbitmq发送方.boot.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: xiaoduo
 * @Email 13333604529@163.com
 * @date: 2021-12-16 17:46
 * @discription: 测试
 **/
@RestController
@RequestMapping("/rabbitmq")
public class TestController {


    //1.注入 RabbitTemplate
    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/testSend")
    public String testSend(){

        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","哈哈 今天 是2021-12-16");

        return "发送成功!";
    }


}

	开始测试:

在这里插入图片描述

接受方:

	接收方我们只用 监听消息后消费即可,代码相对较少.
package org.jeecg.rabbitmq接收方.boot;

import com.sun.org.apache.xpath.internal.operations.String;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @Author: xiaoduo
 * @Email 13333604529@163.com
 * @date: 2021-12-16 17:59
 * @discription: rabbit 接收方
 **/
@Component
public class RabbitMQListener {

    @RabbitListener(queues = "boot_queue")
    public void ListenerQueue( Message  message){

        System.out.println(message);
    }

}

	当消费方发送消息他将自动接收并 消费。

在这里插入图片描述
消息接收成功。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值