RabbitMQ: SpringBoot 整合 RabbitMQ

这篇具有很好参考价值的文章主要介绍了RabbitMQ: SpringBoot 整合 RabbitMQ。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、生产者模块

1.导入依赖

重点是这个依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
  <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <!--    声明springboot的版本号 -->
        <spring-boot.version>2.2.9.RELEASE</spring-boot.version>
    </properties>
    <!--  引入springboot官方提供的所有依赖的版本号定义,如果项目中使用相关依赖,可以不必写版本号了-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>




        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

    </dependencies>

2.yml配置文件

spring:
  rabbitmq:
    host: 8.140.244.227
    port: 6786
    username: test
    password: test
    virtual-host: /test
server:
  port: 8081

3.用接口方式实现生产者

通过

@Autowired
RabbitTemplate rabbitTemplate;//这个模板
package com.qf.bootmq2302.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

@RestController
public class TestController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @GetMapping("/test1")
    public String test1(String msg){
        System.out.println(msg);
        String exchangeName = "";//默认交换机
        String routingkey = "hello";//队列名字
        //生产者发送消息
        rabbitTemplate.convertAndSend(exchangeName,routingkey,msg);
        return "ok";
    }


    @GetMapping("/test2")
    public String test2(String name,Integer age){

        TreeMap<String, Object> map = new TreeMap<String, Object>();
        map.put("name",name);
        map.put("age",age);
        String exchangeName = "";//默认交换机
        String routingkey = "work";//队列名字
        //生产者发送消息
        rabbitTemplate.convertAndSend(exchangeName,routingkey,map);
        return "ok";
    }
}

4.主启动类

package com.qf.bootmq2302;

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

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

二、消费者模块

  1.导入依赖

             和上一个一样

   2.yml配置文件

spring:
  rabbitmq:
    host: 8.140.244.227
    port: 6786
    username: test
    password: test
    virtual-host: /test
    #手动ACK
    listener:
      simple:
        acknowledge-mode: manual
        prefetch: 1 #等价于basicQos(1)

3.通过注解绑定 队列名字

package com.qf.bootconsumer.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

@Component
public class MyConsumer {

//    @RabbitListener(queues = "hello")
//    public void getMsg(Message message) throws UnsupportedEncodingException {
//        byte[] body = message.getBody();
//        String s = new String(body, "utf-8");
//        System.out.println(s);
//
//    }

//    @RabbitListener(queues = "hello")
//    public void getMsg(String msg) throws UnsupportedEncodingException {
//
//        System.out.println(msg);
//
//    }


    @RabbitListener(queues = "hello")
    public void getMsg(Map<String,Object> message) throws UnsupportedEncodingException {

        System.out.println(message);

    }

    @RabbitListener(queues = "work")
    public void getMsg1(Map<String,Object> data, Channel channel,Message message) throws IOException {


        System.out.println(data);

        //手动ack//若开启手动ack,不给手动ack,就按照 prefetch: 1 #等价于basicQos(1)的量,就这么多,不会多给你了,因为你没有确认。确认一条,就给你一条
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }



}

4.通过配置类,创建队列,交换机,绑定队列给交换机和给予路由

 文章来源地址https://www.toymoban.com/news/detail-700533.html

package com.qf.bootconsumer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 在 该配置类中可以,通过@Bean 方法定义 mq内部的交换机和队列 ,及其绑定关系
 */
@Configuration
public class MyConfig {

    @Bean
    public TopicExchange exchange01(){
        return  new TopicExchange("boot-exchange",true,false);
    }

    @Bean
    public Queue queue01(){
        /**
         * 第一个参数:队列名字
         * 第二个参数:true:代表服务重启后,此队列还存在
         * 第三个参数: true:排外,不能让其他连接来访问此队列,只有创建此队列的连接能访问消费此队列
         * 第四个参数: true:代表服务关闭时,RabbitMQ会自动把 此队列删除了。
         */
        Queue queue = new Queue("boot-Queue", true, false, false);
        return queue;
    }
    @Bean
    public Binding binding01(TopicExchange exchange01,Queue queue01){
        Binding binding = BindingBuilder.bind(queue01).to(exchange01).with("*.orange.*");
        return binding;
    }

    @Bean
    public Binding binding02(TopicExchange exchange01,Queue queue01){
        Binding binding = BindingBuilder.bind(queue01).to(exchange01).with("*.*.rabbit");
        return binding;
    }

    @Bean
    public FanoutExchange exchange02(){
        return new FanoutExchange("boot-fanout");
    }

    @Bean
    public Queue queue02(){
        return new Queue("boot-queue02",true,false,false);
    }
    @Bean
    public Queue queue03(){
        return new Queue("boot-queue03",true,false,false);
    }
    @Bean
    public Binding binding03(FanoutExchange exchange02,Queue queue02){
        Binding binding = BindingBuilder.bind(queue02).to(exchange02);
        return binding;
    }
    @Bean
    public Binding binding04(FanoutExchange exchange02,Queue queue03){
        Binding binding = BindingBuilder.bind(queue03).to(exchange02);
        return binding;
    }

}

到了这里,关于RabbitMQ: SpringBoot 整合 RabbitMQ的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Boot整合RabbitMQ之路由模式(Direct)

    RabbitMQ中的路由模式(Direct模式)应该是在实际工作中运用的比较多的一种模式了,这个模式和发布与订阅模式的区别在于路由模式需要有一个routingKey,在配置上,交换机类型需要注入DirectExchange类型的交换机bean对象。在交换机和队列的绑定过程中,绑定关系需要在绑定一个

    2024年02月11日
    浏览(30)
  • RabbitMQ和spring boot整合及其他内容

    在现代分布式应用程序的设计中,消息队列系统是不可或缺的一部分,它为我们提供了解耦组件、实现异步通信和确保高性能的手段。RabbitMQ,作为一款强大的消息代理,能够协助我们实现这些目标。在本篇CSDN博客中,我们将探讨一些高级主题,包括RabbitMQ与Spring Boot的整合、

    2024年02月07日
    浏览(32)
  • 【RabbitMQ】4 Spring/SpringBoot整合RabbitMQ

    spring-amqp 是对AMQP的一些概念的一些抽象, spring-rabbit 是对RabbitMQ操作的封装实现。 主要有几个核心类 RabbitAdmin 、 RabbitTemplate 、 SimpleMessageListenerContainer 等。 RabbitAdmin 类完成对Exchange,Queue,Binding的操作,在容器中管理了 RabbitAdmin 类的时候,可以对Exchange,Queue,Binding进行自

    2024年01月22日
    浏览(27)
  • 消息队列——spring和springboot整合rabbitmq

    目录 spring整合rabbitmq——生产者 rabbitmq配置文件信息 倒入生产者工程的相关代码 简单工作模式 spring整合rabbitmq——消费者 spring整合rabbitmq——配置详解 SpringBoot整合RabbitMQ——生产者  SpringBoot整合RabbitMQ——消费者   使用原生amqp来写应该已经没有这样的公司了 创建两个工程

    2024年02月16日
    浏览(35)
  • 【SpringBoot】Spring Boot 项目中整合 MyBatis 和 PageHelper

    目录 前言         步骤 1: 添加依赖 步骤 2: 配置数据源和 MyBatis 步骤 3: 配置 PageHelper 步骤 4: 使用 PageHelper 进行分页查询 IDEA指定端口启动 总结         Spring Boot 与 MyBatis 的整合是 Java 开发中常见的需求,特别是在使用分页插件如 PageHelper 时。PageHelper 是一个针对 MyBat

    2024年04月25日
    浏览(28)
  • 【Spring Boot】SpringBoot 优雅整合Swagger Api 自动生成文档

    Swagger 是一套 RESTful API 文档生成工具,可以方便地生成 API 文档并提供 API 调试页面。 而 Spring Boot 是一款非常优秀的 Java Web 开发框架,它可以非常方便地构建 Web 应用程序。 在本文中,我们将介绍如何使用 Swagger 以及如何在 Spring Boot 中整合 Swagger 。 首先,在 pom.xml 文件中添

    2023年04月22日
    浏览(33)
  • spring boot es | spring boot 整合elasticsearch | spring boot整合多数据源es

    目录 Spring Boot与ES版本对应 Maven依赖 配置类 使用方式 @Test中注入方式 @Component中注入方式 查询文档 实体类 通过ElasticsearchRestTemplate查询 通过JPA查询 保存文档 参考链接 项目组件版本: Spring Boot:2.2.13.RELEASE Elasticsearch:6.8.0 JDK:1.8.0_66 Tips: 主要看第3列和第5列,根据ES版本选择

    2023年04月18日
    浏览(38)
  • RabbitMQ: SpringBoot 整合 RabbitMQ

    重点是这个依赖 通过              和上一个一样  

    2024年02月09日
    浏览(35)
  • 【RabbitMQ】RabbitMQ整合SpringBoot案例

    【RabbitMQ】消息队列-RabbitMQ篇章 RabbitMQ实现流程 2.1 实现架构总览 实现步骤: 1:创建生产者工程:sspringboot-rabbitmq-fanout-producer 2:创建消费者工程:springboot-rabbitmq-fanout-consumer 3:引入spring-boot-rabbitmq的依赖 4:进行消息的分发和测试 5:查看和观察web控制台的状况 2.2 具体实现

    2024年02月12日
    浏览(32)
  • 【RabbitMQ】Spring整合RabbitMQ、Spring实现RabbitMQ五大工作模式(万字长文)

    目录 一、准备 1、创建maven项目​编辑 2、引入依赖 3、创建配置文件 1.RabbitMQ配置文件 2.生产者项目配置文件 3.消费者项目配置文件 二、生产者xml中文件创建队列 三、生产者xml文件中创建交换机以及绑定队列 1、创建交换机 2、绑定队列  四、消费者xml文件中创建队列消息监

    2024年01月21日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包