SpringBoot整合rabbitmq-直连交换机队列(二)

news2025/2/25 3:20:27

说明:本文章主要是Direct定向/直连类型交换机的使用,它的大致流程是将一个队列绑定到一个直连交换机上,并赋予一个路由键 routingkey,当一个消息携带着路由值为routingkey,这个消息通过生产者发送给交换机时,交换机就会根据这个路由值routingkey去寻找绑定的队列。

工程图:

A.总体maven引入

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>spring-boot-starter-parent</artifactId>  <!-- 被继承的父项目的构件标识符 -->
    <groupId>org.springframework.boot</groupId>  <!-- 被继承的父项目的全球唯一标识符 -->
    <version>2.2.2.RELEASE</version>  <!-- 被继承的父项目的版本 -->
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <groupId>RabbitMqSpringbootDemo</groupId>
  <artifactId>RabbitMqSpringbootDemo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
    <module>MqCustomer</module>
    <module>MqProducer</module>
  </modules>

  <name>RabbitMqSpringbootDemo Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>


  </dependencies>

  <build>
    <finalName>RabbitMqSpringbootDemo</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

B.生产者MqProducer

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>RabbitMqSpringbootDemo</artifactId>
        <groupId>RabbitMqSpringbootDemo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>MqProducer</artifactId>
    <packaging>jar</packaging>

    <name>MqProducer Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--spring boot核心-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--spring boot 测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--springmvc web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--开发环境调试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!--amqp 支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>

        <!-- commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>MqProducer</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2.application.yml

server:
  port: 8080
spring:
  rabbitmq:
    port: 5672
    host: 192.168.18.145
    username: admin
    password: admin
    virtual-host: /

3.DirectQueueRabbitConfig

package com.dev.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 类名称:Direct定向/直连类型交换机的使用
 *
 * @author lqw
 * @date 2024年02月28日 10:40
 */
@Configuration
public class DirectQueueRabbitConfig {

    //队列 起名:directQueue
    @Bean
    public Queue directQueue() {
        return new Queue("directQueue",true);
    }

    //Direct交换机 起名:directExchange
    @Bean
    DirectExchange directExchange() {
        return new DirectExchange("directExchange",true,false);
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键:directRoutingKey
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(directQueue()).to(directExchange()).with("directRoutingKey");
    }

}

4.DirectQueueController

package com.dev.controller;

import lombok.extern.slf4j.Slf4j;
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;


/**
 * 类名称:直接交换机 消息生产者
 *
 * @author lqw
 * @date 2024年02月27日 14:47
 */
@Slf4j
@RestController
@RequestMapping("directQueue")
public class DirectQueueController {


    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法


    /**
     * Direct测试
     * @return
     */
    @GetMapping("/sendMessage")
    public String sendMessage() {
        Map<String,Object> map = new HashMap<>();
        map.put("id","lalala");
        map.put("name","zhanglong");
        rabbitTemplate.convertAndSend("directExchange", "directRoutingKey", map);
        return "ok";
    }


}

5.AppP

package com.dev;

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

/**
 * 类名称:
 *
 * @author 李庆伟
 * @date 2024年02月27日 14:20
 */
@SpringBootApplication
public class AppP {

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

C.消费者:MqCustomer

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>RabbitMqSpringbootDemo</artifactId>
        <groupId>RabbitMqSpringbootDemo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>MqCustomer</artifactId>
    <packaging>jar</packaging>

    <name>MqCustomer Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--spring boot核心-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--spring boot 测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--springmvc web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--开发环境调试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!--amqp 支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <!-- commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>MqCustomer</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2.application.yml

server:
  port: 8081

spring:
  rabbitmq:
    port: 5672
    host: 192.168.18.145
    username: admin
    password: admin

3.DirectQueueRabbitListener

package cn.ct.listeners;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * 类名称:消息消费者
 * 直连
 * @author lqw
 * @date 2024年02月27日 14:58
 */
@Component
public class DirectQueueRabbitListener {

    @RabbitListener(queues = "directQueue")
    @RabbitHandler
    public void process(Map msg) {
        System.out.println("Rabbitmq Direct : " + msg);
        System.out.println("Rabbitmq Direct : " + msg);
    }

}

4.AppC

package cn.ct;

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

/**
 * 类名称:
 *
 * @author lqw
 * @date 2024年02月27日 14:20
 */
@SpringBootApplication
public class AppC {

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1477754.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Unity UI适配规则和对热门游戏适配策略的拆解

前言 本文会介绍一些关于UI适配的基础概念&#xff0c;并且统计了市面上常见的设备的分辨率的情况。同时通过拆解目前市面上较为成功的两款休闲游戏Royal Match和Monopoly GO(两款均为近期游戏付费榜前几的游戏)&#xff0c;大致推断出他们的适配策略&#xff0c;以供学习和参…

CP AutoSar之LIN Driver详细说明

本文遵循autosar标准&#xff1a;R22-11 1 简介 本文指定了 AUTOSAR 基础软件模块 LIN 驱动程序的功能、API 和配置。 1.1 范围 LIN驱动程序适用于ISO 17987主节点和从节点。AUTOSAR中的LIN实现偏离了本LIN驱动器规范中所述的ISO 17987规范&#xff0c;但LIN总线上的行为不…

Allure报告归纳与总结

一、环境准备&#xff1a; 提前准备环境:Java1.8 Allure2 解析过程: 1.安装 allure2(信赖Java1.8) allure官方下载地址&#xff1a; https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/ 2.安装 allure-pytest 命令:pip install allure-pytest 3.生成…

Mamba 作者谈 LLM 未来架构

文章目录 前言 1、为什么注意力机制有效&#xff1f; 2、注意力计算量呈平方级增长 3、Striped Hyena 是个什么模型&#xff1f; 4、什么是 Mamba? 5、Mamba 硬件优化 6、2024年架构预测 7、对 AI 更多的预测 本片文章来自【机器之心】对Mamba作者进行采访所进行的编译整理。 …

TCP缓存

TCP缓存是指TCP协议在数据传输过程中使用的一种机制&#xff0c;用于临时存储和管理数据包。它主要有三个作用&#xff1a;提高网络性能、保证数据的可靠性和实现流量控制。 首先&#xff0c;TCP缓存可以提高网络性能。当发送端发送数据时&#xff0c;TCP协议会将数据分割成若…

自动驾驶加速落地,激光雷达放量可期(上)

1 激光雷达应用广泛&#xff0c;汽车有望成最大催化 激光雷达&#xff08;LiDAR&#xff09;是一种主动遥感技术&#xff0c;通过测定传感器发出的激光在传感器与目标物体之间的传播距离&#xff0c;来分析目标地物表面的反射能量大小、反射波谱的幅度、频率和相位等信息&#…

ensp模拟单臂路由实现不同两个网段主机访问

拓扑结构图如下 1.pc机配置略过 2.交换机配置 三个接口&#xff0c;两个连接pc&#xff0c;连接方式access&#xff0c;一个连接路由器 连接方式trunk sy #进入系统 视图模式 undo info-center enable #关闭信息 vlan batch 10 20#批量创建vlan int g 0/0/2#进入2端口 p…

黑马嵌入式开发数电模电基础课

本课程旨在为学习者提供扎实的嵌入式系统开发所需的数字电路和模拟电路知识。通过理论与实践相结合的方式&#xff0c;学习者将掌握数字信号处理、模拟信号调节等关键概念&#xff0c;为将来在嵌入式系统设计与开发领域取得成功打下坚实基础。 视频大小&#xff1a;4.9G 课程…

【Vue3】回顾watch,学习watchEffect

&#x1f497;&#x1f497;&#x1f497;欢迎来到我的博客&#xff0c;你将找到有关如何使用技术解决问题的文章&#xff0c;也会找到某个技术的学习路线。无论你是何种职业&#xff0c;我都希望我的博客对你有所帮助。最后不要忘记订阅我的博客以获取最新文章&#xff0c;也欢…

生产报工异常信息提示器如何精确提醒管理人员

在现代生产环境中&#xff0c;生产报工异常信息的及时提醒对于管理人员来说至关重要。为了精确提醒管理人员并确保生产流程的顺利进行&#xff0c;智能信息接收腕表作为一种先进的工具&#xff0c;结合了多项功能&#xff0c;可以有效地实现生产报工异常信息的精确提醒。以下将…

H3C防火墙安全授权导入

一、防火墙授权概述 前面我们已经了解了一些防火墙的基本概念&#xff0c;有讲过防火墙除了一些基本功能&#xff0c;还有一些高级安全防护&#xff0c;但是这些功能需要另外独立授权&#xff0c;不影响基本使用。这里以H3C防火墙为例进行大概了解下。 正常情况下&#xff0c;防…

55.仿简道云公式函数实战-文本函数-MID

1. MID函数 返回文本字符串中从指定位置开始的特定数目的字符&#xff0c;该数目由用户指定。 2. 函数用法 MID(text, start_num, num_chars) 3. 函数示例 返回文本字符串中从指定位置开始的特定数目的字符&#xff0c;该数目由用户指定。 text: 必需。 包含要提取字符的文…

centos物理电脑安装过程(2024年1月)

开机时&#xff1a;CtrlAltDelete键重启电脑 重启开始时&#xff1a;按F11&#xff0c;桌面弹出蓝色框&#xff0c;选择第二个SSK SFD142 1.00&#xff0c;回车 选择install centos7安装 选择后弹出选择安装选项&#xff0c;选择语言 连接无线网络 安装设置&#xff0c;选择磁…

面试笔记系列五之MySql+Mybaits基础知识点整理及常见面试题

目录 Myibatis myibatis执行过程 mybatis的优缺点有哪些&#xff1f; mybatis和hibernate有什么区别&#xff1f; mybatis中#{}和${}的区别是什么&#xff1f; 简述一下mybatis插件运行原理及开发流程&#xff1f;&#xff08;插件四大天王&#xff09; mybatis的mapper没…

C++string(二)

我们上次讲完了Element access&#xff0c;现在我们继续往下讲&#xff1a; 一.Modifiers: 1.operator: Extends the string by appending additional characters at the end of its current value: 翻译&#xff1a;在string类对象结尾追加字符 如下&#xff1a; #include…

vue前端使用get方式获取api接口数据 亲测

// GET请求示例 axios.get(‘http://127.0.0.1:5005/ReadIDCardInfo’) // 将URL替换为真正的API接口地址 .then(response > { if(response.data.code1){ var jsonDataresponse.data.data; console.log(jsonData); // 输出从API接口返回的数据 } }) .catch(error > { con…

ModStartCMS v8.1.0 图片前端压缩,抖音授权登录

ModStart 是一个基于 Laravel 模块化极速开发框架。模块市场拥有丰富的功能应用&#xff0c;支持后台一键快速安装&#xff0c;让开发者能快的实现业务功能开发。 系统完全开源&#xff0c;基于 Apache 2.0 开源协议&#xff0c;免费且不限制商业使用。 功能特性 丰富的模块市…

电脑合集检测工具箱,图吧工具箱软件推荐

今天最软库给大家体验的是电脑检测工具箱。 图吧工具箱&#xff0c;是开源、免费、绿色、纯净的硬件检测工具合集&#xff0c;专为所有计算机硬件极客、DIY爱好者、各路大神及小白制作。集成大量常见硬件检测、评分工具&#xff0c;一键下载、方便使用。 不忘初心&#xff0c;始…

leetcode:860.柠檬水找零

题意&#xff1a;按照支付顺序&#xff0c;进行支付&#xff0c;能够正确找零。 解题思路&#xff1a;贪心策略&#xff1a;针对支付20的客人&#xff0c;优先选择消耗10而不是消耗5&#xff0c;因为5可以用来找零10或20. 代码实现&#xff1a;有三种情况&#xff08;代表三种…