栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 整合 RabbitMQ

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 整合 RabbitMQ

SpringBoot 整合 RabbitMQ

文章目录
  • SpringBoot 整合 RabbitMQ
  • 前言
  • 一、生产者
    • 1.配置pom.xml,RabbitMQ依赖
    • 2.编写 properties 配置,基本信息配置
    • 3.定义交换机,队列以及绑定关系的配置类
    • 4.注入RabbitTemplate,调用方法,完成消息发送
  • 二、消费者
    • 1.配置pom.xml,RabbitMQ依赖
    • 2.编写 properties 配置,基本信息配置
    • 3.定义监听类,使用@RabbitListener注解完成队列监听
  • 备注说明


前言

RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件)。RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的。所有主要的编程语言均有与代理接口通讯的客户端库。

生产者,就是投递消息的 一方。
消费者,就是接收消息的 一方。


一、生产者 1.配置pom.xml,RabbitMQ依赖

代码如下:



    org.springframework.boot
    spring-boot-starter-amqp

2.编写 properties 配置,基本信息配置

提示:如果你项目的配置文件是 yml,这里分享一个在线 properties 转 yml工具,也支持 yml 转 properties
网址:http://toyaml.com/
代码如下:

#rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=root
spring.rabbitmq.virtual-host=my_vhost
3.定义交换机,队列以及绑定关系的配置类

代码如下:

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 {
    
    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();
    }
    
    
    @Bean("bootQueue")
    public Queue bootQueue() {
        return QueueBuilder.durable(QUEUE_NAME).build();
    }
    
    
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,
                                     @Qualifier("bootExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }
    
}
4.注入RabbitTemplate,调用方法,完成消息发送

代码如下:

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

@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
    
    @Resource
    private RabbitTemplate rabbitTemplate;
    
    
    @Test
    public void sendTest() {
        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME, "boot.#", "boot.mq");
    }
}
二、消费者 1.配置pom.xml,RabbitMQ依赖

代码如下:



    org.springframework.boot
    spring-boot-starter-amqp

2.编写 properties 配置,基本信息配置

代码如下:

#rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=root
spring.rabbitmq.virtual-host=my_vhost
3.定义监听类,使用@RabbitListener注解完成队列监听
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMqListener {

    
    @RabbitListener(queues = "boot_queue")
    public void ListenerQueue(Message message) {
        System.out.println("message:" + message);
    }
}

备注说明

● SpringBoot提供了快速整合RabbitMQ的方式
● 基本信息再yml中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置
● 生产端直接注入RabbitTemplate完成消息发送
● 消费端直接使用@RabbitListener完成消息接收

来源:https://blog.51cto.com/u_14129797/5201520

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/967475.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号