iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >SpringBoot中怎样整合RabbitMQ
  • 681
分享到

SpringBoot中怎样整合RabbitMQ

2023-06-20 19:06:02 681人浏览 安东尼
摘要

本篇文章给大家分享的是有关SpringBoot中怎样整合RabbitMQ,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一、环境准备1、pom依赖<!-- 父工程

本篇文章给大家分享的是有关SpringBoot中怎样整合RabbitMQ,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

一、环境准备

SpringBoot中怎样整合RabbitMQ

1、pom依赖

<!-- 父工程依赖 -->    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.3.6.RELEASE</version>    </parent>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-WEB</artifactId>        </dependency>        <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>        <dependency>            <groupId>io.springfox</groupId>            <artifactId>springfox-swagger2</artifactId>            <version>2.6.0</version>        </dependency>        <dependency>            <groupId>io.springfox</groupId>            <artifactId>springfox-swagger-ui</artifactId>            <version>2.6.0</version>        </dependency>    </dependencies>

2、配置文件

server:  port: 8080spring:  rabbitmq:    host: 192.168.131.171    port: 5672    username: jihu    passWord: jihu    virtual-host: /jihu

3、启动类

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

5、Swagger2类

@Configuration@EnableSwagger2public class Swagger2 {    // Http://127.0.0.1:8080/swagger-ui.html    @Bean    public Docket createRestapi() {        return new Docket(DocumentationType.SWAGGER_2)                .apiInfo(apiInfo())                .select()                .apis(RequestHandlerSelectors.basePackage("com.jihu"))                .paths(PathSelectors.any())                .build();    }    private ApiInfo apiInfo() {        return new ApiInfoBuilder()                .title("极狐-Spring Boot中使用spring-boot-starter-amqp集成rabbitmq")                .description("测试SpringBoot整合进行各种工作模式信息的发送")                .contact("roykingw")                .version("1.0")                .build();    }}

6、ProducerController

@RestControllerpublic class ProducerController {    @Autowired    private RabbitTemplate rabbitTemplate;    //helloWorld 直连模式    @ApiOperation(value = "helloWorld发送接口", notes = "直接发送到队列")    @GetMapping(value = "/helloWorldSend")    public Object helloWorldSend(String message) throws AmqpException, UnsupportedEncodingException {        //设置部分请求参数        MessageProperties messageProperties = new MessageProperties();        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);        //发消息        rabbitTemplate.send("helloWorldqueue", new Message(message.getBytes("UTF-8"), messageProperties));        return "message sended : " + message;    }    //工作队列模式    @ApiOperation(value = "workqueue发送接口", notes = "发送到所有监听该队列的消费")    @GetMapping(value = "/workqueueSend")    public Object workqueueSend(String message) throws AmqpException, UnsupportedEncodingException {        MessageProperties messageProperties = new MessageProperties();        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);        //制造多个消息进行发送操作        for (int i = 0; i < 10; i++) {            rabbitTemplate.send("work_sb_mq_q", new Message(message.getBytes("UTF-8"), messageProperties));        }        return "message sended : " + message;    }    // pub/sub 发布订阅模式   交换机类型 fanout    @ApiOperation(value = "fanout发送接口", notes = "发送到fanoutExchange。消息将往该exchange下的所有queue转发")    @GetMapping(value = "/fanoutSend")    public Object fanoutSend(String message) throws AmqpException, UnsupportedEncodingException {        MessageProperties messageProperties = new MessageProperties();        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);        //fanout模式只往exchange里发送消息。分发到exchange下的所有queue        rabbitTemplate.send("fanoutExchange", "", new Message(message.getBytes("UTF-8"), messageProperties));        return "message sended : " + message;    }    //routing路由工作模式  交换机类型 direct    @ApiOperation(value = "direct发送接口", notes = "发送到directExchange。exchange转发消息时,会往routingKey匹配的queue发送")    @GetMapping(value = "/directSend")    public Object routingSend(String routingKey, String message) throws AmqpException, UnsupportedEncodingException {        if (null == routingKey) {            routingKey = "china.changsha";        }        MessageProperties messageProperties = new MessageProperties();        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);        //fanout模式只往exchange里发送消息。分发到exchange下的所有queue        rabbitTemplate.send("directExchange", routingKey, new Message(message.getBytes("UTF-8"), messageProperties));        return "message sended : routingKey >" + routingKey + ";message > " + message;    }    //topic 工作模式   交换机类型 topic    @ApiOperation(value = "topic发送接口", notes = "发送到topicExchange。exchange转发消息时,会往routingKey匹配的queue发送,*代表一个单词,#代表0个或多个单词。")    @GetMapping(value = "/topicSend")    public Object topicSend(String routingKey, String message) throws AmqpException, UnsupportedEncodingException {        if (null == routingKey) {            routingKey = "changsha.kf";        }        MessageProperties messageProperties = new MessageProperties();        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);        //fanout模式只往exchange里发送消息。分发到exchange下的所有queue        rabbitTemplate.send("topicExchange", routingKey, new Message(message.getBytes("UTF-8"), messageProperties));        return "message sended : routingKey >" + routingKey + ";message > " + message;    }}

7、ConcumerReceiver

@Componentpublic class ConcumerReceiver {    //直连模式的多个消费者,会分到其中一个消费者进行消费。类似task模式    //通过注入RabbitContainerFactory对象,来设置一些属性,相当于task里的channel.basicQos    @RabbitListener(queues = "helloWorldqueue")    public void helloWorldReceive(String message) {        System.out.println("helloWorld模式 received message : " + message);    }    //工作队列模式    @RabbitListener(queues = "work_sb_mq_q")    public void wordQueueReceiveq1(String message) {        System.out.println("工作队列模式1 received message : " + message);    }    @RabbitListener(queues = "work_sb_mq_q")    public void wordQueueReceiveq2(String message) {        System.out.println("工作队列模式2 received message : " + message);    }    //pub/sub模式进行消息监听    @RabbitListener(queues = "fanout.q1")    public void fanoutReceiveq1(String message) {        System.out.println("发布订阅模式1received message : " + message);    }    @RabbitListener(queues = "fanout.q2")    public void fanoutReceiveq2(String message) {        System.out.println("发布订阅模式2 received message : " + message);    }    //Routing路由模式    @RabbitListener(queues = "direct_sb_mq_q1")    public void routingReceiveq1(String message) {        System.out.println("Routing路由模式routingReceiveq11111 received message : " + message);    }    @RabbitListener(queues = "direct_sb_mq_q2")    public void routingReceiveq2(String message) {        System.out.println("Routing路由模式routingReceiveq22222 received message : " + message);    }    //topic 模式    //注意这个模式会有优先匹配原则。例如发送routingKey=hunan.IT,那匹配到hunan.*(hunan.IT,hunan.eco),之后就不会再去匹配*.ITd    @RabbitListener(queues = "topic_sb_mq_q1")    public void topicReceiveq1(String message) {        System.out.println("Topic模式 topic_sb_mq_q1 received message : " + message);    }    @RabbitListener(queues = "topic_sb_mq_q2")    public void topicReceiveq2(String message) {        System.out.println("Topic模式 topic_sb_mq_q2 received  message : " + message);    }}

二、简单模式

队列配置:

@Configurationpublic class HelloWorldConfig {@Beanpublic Queue setQueue() {return new Queue("helloWorldqueue");}}

三、工作队列模式

@Configurationpublic class WorkConfig {    //声明队列    @Bean    public Queue workQ1() {        return new Queue("work_sb_mq_q");    }}

四、广播模式(Fanout)

@Configurationpublic class FanoutConfig {//声明队列@Beanpublic Queue fanoutQ1() {return new Queue("fanout.q1");}@Beanpublic Queue fanoutQ2() {return new Queue("fanout.q2");}//声明exchange@Beanpublic FanoutExchange setFanoutExchange() {return new FanoutExchange("fanoutExchange");}//声明Binding,exchange与queue的绑定关系@Beanpublic Binding bindQ1() {return BindingBuilder.bind(fanoutQ1()).to(setFanoutExchange());}@Beanpublic Binding bindQ2() {return BindingBuilder.bind(fanoutQ2()).to(setFanoutExchange());}}

五、直连模式(Direct)

@Configurationpublic class DirectConfig {//声明队列@Beanpublic Queue directQ1() {return new Queue("direct_sb_mq_q1");}@Beanpublic Queue directQ2() {return new Queue("direct_sb_mq_q2");}//声明exchange@Beanpublic DirectExchange setDirectExchange() {return new DirectExchange("directExchange");}//声明binding,需要声明一个routingKey@Beanpublic Binding bindDirectBind1() {return BindingBuilder.bind(directQ1()).to(setDirectExchange()).with("china.changsha");}@Beanpublic Binding bindDirectBind2() {return BindingBuilder.bind(directQ2()).to(setDirectExchange()).with("china.beijing");}}

六、通配符模式(Topic)

@Configurationpublic class TopicConfig {//声明队列@Beanpublic Queue topicQ1() {return new Queue("topic_sb_mq_q1");}@Beanpublic Queue topicQ2() {return new Queue("topic_sb_mq_q2");}//声明exchange@Beanpublic TopicExchange setTopicExchange() {return new TopicExchange("topicExchange");}//声明binding,需要声明一个roytingKey@Beanpublic Binding bindTopicHebei1() {return BindingBuilder.bind(topicQ1()).to(setTopicExchange()).with("changsha.*");}@Beanpublic Binding bindTopicHebei2() {return BindingBuilder.bind(topicQ2()).to(setTopicExchange()).with("#.beijing");}}

测试

我们启动上面的SpringBoot项目

然后我们访问swagger地址:http://127.0.0.1:8080/swagger-ui.html

SpringBoot中怎样整合RabbitMQ

然后我们就可以使用swagger测试接口了。

SpringBoot中怎样整合RabbitMQ

SpringBoot中怎样整合RabbitMQ

以上就是SpringBoot中怎样整合RabbitMQ,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网精选频道。

--结束END--

本文标题: SpringBoot中怎样整合RabbitMQ

本文链接: https://www.lsjlt.com/news/299370.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot中怎样整合RabbitMQ
    本篇文章给大家分享的是有关SpringBoot中怎样整合RabbitMQ,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一、环境准备1、pom依赖<!-- 父工程...
    99+
    2023-06-20
  • Springboot怎么整合RabbitMQ
    本篇文章给大家分享的是有关Springboot怎么整合RabbitMQ,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。准备工作15minIDEAmaven 3.0在开始构建项目之...
    99+
    2023-06-19
  • Java中SpringBoot如何整合RabbitMQ
    这篇文章主要为大家展示了“Java中SpringBoot如何整合RabbitMQ”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java中SpringBoot如何整合RabbitMQ”这篇文章吧。...
    99+
    2023-06-05
  • springboot整合消息队列RabbitMQ
    前言: RabbitMQ常用的三种Exchange Type:fanout、direct、topic。 fanout:把所有发送到该Exchange的消息投递到所有与它绑定的队列中。...
    99+
    2024-04-02
  • springboot整合RabbitMQ中的TTL实例代码
    目录TTL简介配置类代码生产者代码消息消费者代码验证代码TTL简介 TTL 是什么呢?TTL 是 RabbitMQ 中一个消息或者队列的属性,表明一条消息或者该队列中的所有消息的最大...
    99+
    2024-04-02
  • springBoot整合rabbitMQ的方法详解
    引入pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave...
    99+
    2024-04-02
  • Springboot整合Rabbitmq之Confirm和Return机制
    目录前言为什么会有ConfirmSpringboot 整合 Mq 实现 Confirm 监听机制依赖引入增加配置文件,设定连接信息配置队列、交换机,以及对其进行绑定编写mq消息发送服...
    99+
    2024-04-02
  • SpringBoot整合RabbitMQ消息队列的完整步骤
    SpringBoot整合RabbitMQ 主要实现RabbitMQ以下三种消息队列: 简单消息队列(演示direct模式) 基于RabbitMQ特性的延时消息队列 ...
    99+
    2024-04-02
  • springboot整合RabbitMQ发送短信的实现
    目录RabbitMQ安装和运行MQ服务器设置创建用户创建虚拟机实现发送短信rabbit-mqservice-baseservice-core中发送消息service-sms中监听消息...
    99+
    2024-04-02
  • SpringBoot整合RabbitMQ的5种模式实战
    目录一、环境准备二、简单模式三、工作队列模式四、广播模式(Fanout)五、直连模式(Direct)六、通配符模式(Topic)一、环境准备 1、pom依赖 <!-- 父...
    99+
    2024-04-02
  • SpringBoot整合RabbitMQ实现消息确认机制
    前面几篇案例已经将常用的交换器(DirectExchange、TopicExchange、FanoutExchange)的用法介绍完了,现在我们来看一下消息的回调,也就是消息确认。 ...
    99+
    2024-04-02
  • Springboot整合RabbitMq测试TTL的方法详解
    目录什么是TTL?如何设置TTL?设定整个队列的过期时间配置类编写测试配置测试总结代码下载什么是TTL? 在RabbitMq中,存在一种高级特性 TTL。 TTL即Time To L...
    99+
    2024-04-02
  • springBoot整合rabbitmq测试常用模型小结
    目录1.添加依赖2.编写配置3.编写并测试之前我们记录了原生java代码使用rabbitmq的方法,很简单,类似于原生jdbc代码一样,将连接对象抽离出来作为工具类,生产者和消费者通...
    99+
    2024-04-02
  • RabbitMQ交换机与Springboot整合的简单实现
    RabbitMQ-交换机 1、交换机是干什么的? 消息(Message)由Client发送,RabbitMQ接收到消息之后通过交换机转发到对应的队列上面。Worker会从队列中获取未...
    99+
    2024-04-02
  • SpringBoot中怎么整合Redis
    SpringBoot中怎么整合Redis,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。一、安装首先要在本地安装一个redis程序,安装过程十分简单(略过),安装完成后进入到...
    99+
    2023-06-16
  • SpringBoot如何整合RabbitMQ实现死信交换机
    本篇内容介绍了“SpringBoot如何整合RabbitMQ实现死信交换机”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!环境Windows1...
    99+
    2023-07-02
  • RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合
    目录1. 概述2. 场景说明3. 与Springboot的整合3.1 引入依赖3.2 生产服务配置3.3 生产服务代码3.4 消费服务配置3.5 消费服务代码3.6 Rest 测试代...
    99+
    2024-04-02
  • springboot整合RabbitMQ发送短信的实现方法
    这篇文章主要介绍springboot整合RabbitMQ发送短信的实现方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!RabbitMQ安装和运行# 安装rpm -ivh erlang-...
    99+
    2023-06-15
  • springBoot整合rabbitmq测试常用模型有哪些
    这篇文章跟大家分析一下“springBoot整合rabbitmq测试常用模型有哪些”。内容详细易懂,对“springBoot整合rabbitmq测试常用模型有哪些”感兴趣的朋友可以跟着小编的思路慢慢深入来阅读一下,希望阅读后能够对大家有所帮...
    99+
    2023-06-26
  • SpringBoot中怎么整合SpringSecurity
    SpringBoot中怎么整合SpringSecurity,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。1.导包<dependency> ...
    99+
    2023-06-05
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作