rabbitmq基础教程(ui,java,springamqp)

概述:安装看我上篇文章Docker安装rabbitmq-CSDN博客

任务一

创建一个队列

这样创建两个队列

在amq.fanout交换机里面发送数据

模拟发送数据

发送消息,发现一下信息:

所以得出理论,消息发送是先到交换机,然后由交换机路由到消息队列

交换机是负责路由和转发消息的,并没有存储的功能。

绑定队列

同理绑定queue2

这时,再在交换机中发消息

查看结果:

数据隔离

在rabbitmq中有虚拟主机的概念。

第一步:新添用户

添加成功后,发现没有虚拟主机,也就是说,我用这个用户登录后,是不可以操作上面的数据的。

又因为,我是超级管理员,所以我能看到这些

所以只能看,不能操作。

第二步:创立自己的虚拟主机

第三步:选自己的虚拟主机

选好后就只能看自己的了。

用Java代码操作

官网:RabbitMQ Tutorials — RabbitMQ

可以看到,官网上有案例,我们大多情况下用的是SpringAmqp,所以也就不讲那么多java简单调用的事情了。

用Spring AMQP操作

第一步:在控制台里面创建一个simple.queue队列

第二步:编写代码

pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.cyl</groupId><artifactId>test09</artifactId><version>0.0.1-SNAPSHOT</version><name>test09</name><description>test09</description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.13</spring-boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>5.13.0</version></dependency></dependencies><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><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><configuration><mainClass>org.cyl.test09.Test09Application</mainClass><skip>true</skip></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>

配置mq服务端消息

spring:rabbitmq:host: 192.168.56.10port: 5672virtual-host: /cmallusername: cmallpassword: 123456

发送方:

package org.cyl.test09.demos;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class SendMessageService {@Autowiredprivate RabbitTemplate rabbitTemplate;public void testSimpleQueue(){String queueName="simple.queue";String message="hello,spring amqp!";rabbitTemplate.convertAndSend(queueName,message);}public void sendMessage(String exchange, String routingKey, Object message) {rabbitTemplate.convertAndSend(exchange, routingKey, message);}
}

接收方:

package org.cyl.test09.demos;import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;@Service
public class ReceiveMessageService {@RabbitListener(queues = "simple.queue")public void receiveMessage(String message) {System.out.println("接收到的消息: " + message);}
}

controller类:

package org.cyl.test09.demos.controller;import org.cyl.test09.demos.ReceiveMessageService;
import org.cyl.test09.demos.SendMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@AutowiredSendMessageService sendMsgservice;@AutowiredReceiveMessageService receiveMsgService;@GetMapping("/send")public String send(){sendMsgservice.testSimpleQueue();return "ok";}}

展示结果:

Work模型

第一步:创建一个队列

第二步:编写代码

发送:

package org.cyl.test09.demos;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class SendMessageService {@Autowiredprivate RabbitTemplate rabbitTemplate;public void testSimpleQueue() throws InterruptedException {String queueName="work.queue";for (int i=1;i<50;i++){String message="hello,spring amqp!_"+i;rabbitTemplate.convertAndSend(queueName,message);Thread.sleep(20);}}public void sendMessage(String exchange, String routingKey, Object message) {rabbitTemplate.convertAndSend(exchange, routingKey, message);}
}

接收:

package org.cyl.test09.demos;import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;@Service
public class ReceiveMessageService {@RabbitListener(queues = "work.queue")public void receiveMessage1(String message) {System.out.println("消费者1接收到的消息: " + message);}@RabbitListener(queues = "work.queue")public void receiveMessage2(String message) {System.out.println("消费者2接收到的消息: " + message);}
}

结果展示:

消费者一和消费者二是轮询效果。

Fanout交换机

第一步:创建队列

第二步:创建交换机并绑定

第三步:编写代码

发送端:

    public void testFanout() {String exchangeName="cmall.fanout";String message="hello,spring everyone";rabbitTemplate.convertAndSend(exchangeName,null,message);}

接收端:

   @RabbitListener(queues = "fanout.queue1")public void receiveMessage3(String message) {System.out.println("消费者1接收到的消息: " + message);}@RabbitListener(queues = "fanout.queue2")public void receiveMessage4(String message) {System.out.println("消费者2接收到的消息: " + message);}

展示结果:

私发给不同的人:Direct交换机

第一步:创建两个队列

第二步:声明交换机并绑定

第三步:编写代码

接收方:

   @RabbitListener(queues = "direct.queue1")public void receiveMessage5(String message) {System.out.println("消费者1接收到的消息: " + message);}@RabbitListener(queues = "direct.queue2")public void receiveMessage6(String message) {System.out.println("消费者2接收到的消息: " + message);}

发送方:

    public void testDirect1() {String exchangeName="cmall.fanout";String message="hello,spring everyone";rabbitTemplate.convertAndSend(exchangeName,"red",message);}public void testDirect2() {String exchangeName="cmall.fanout";String message="hello,spring blue";rabbitTemplate.convertAndSend(exchangeName,"blue",message);}public void testDirect3() {String exchangeName="cmall.fanout";String message="hello,spring yellow";rabbitTemplate.convertAndSend(exchangeName,"yellow",message);}

Topic交换机

这个示例代码就懒得写了。

声明交换机和队列1

绑定队列到哪个交换机里面。

一般建立关系都是在消费者这边的。

声明交换机和队列2

基于注解式声明队列和交换机。

消息转换器

字节码可变,会有安全问题。

搞完以上东西,代码不用变,在发一次,即可为json。

好了,基础讲完。

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

云边协同的 RTC 如何助力即构全球实时互动业务实践

作者&#xff1a;即构科技 由 51 CTO 主办的“WOT 全球技术创新大会 2023深圳站”于 11 月 24 日 - 25 日召开&#xff0c;即构科技后台技术总监肖潇以“边缘容器在全球音视频场景的探索与实践”为主题进行分享。 边缘计算作为中心云计算的补充&#xff0c;通过边缘容器架构和…

【SpringCloud】之Sentinel--服务容错的应用

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是君易--鑨&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的博客专栏《SpringCloud开发之Sentinel--服务容错的应用》。…

muduo网络库剖析——监听者EpollPoller类

muduo网络库剖析——监听者EpollPoller类 前情从muduo到my_muduo 概要epoll原理解析epoll提供的接口epoll的触发模式epoll实现多路复用 框架与细节成员函数使用方法 源码结尾 前情 从muduo到my_muduo 作为一个宏大的、功能健全的muduo库&#xff0c;考虑的肯定是众多情况是否…

【论文阅读】One For All: Toward Training One Graph Model for All Classification Tasks

目录 0、基本信息1、研究动机2、创新点——One For All &#xff1a;unique features3、准备4、具体实现4.1、用TAGs统一来自不同领域的图数据4.2、用NOI&#xff08;NODES-OF-INTEREST&#xff09;统一不同图任务4.2.1、NOI子图4.2.2、NOI提示结点 4.3、用于图的上下文学习&am…

【算法小记】深度学习——循环神经网络相关原理与RNN、LSTM算法的使用

文中程序以Tensorflow-2.6.0为例 部分概念包含笔者个人理解&#xff0c;如有遗漏或错误&#xff0c;欢迎评论或私信指正。 卷积神经网络在图像领域取得了良好的效果&#xff0c;卷积核凭借优秀的特征提取能力通过深层的卷积操作可是实现对矩形张量的复杂计算处理。但是生活中除…

C++、QT 数字合成游戏

一、项目介绍 数字合成游戏 基本要求&#xff1a; 1&#xff09;要求游戏界面简洁美观&#xff0c;且符合扫雷的游戏风格。 2&#xff09;需要有游戏操作或者规则说明&#xff0c;方便玩家上手。 3&#xff09;需具有开始游戏&#xff0c;暂停游戏&#xff0c;结束游戏等方便玩…

2018年认证杯SPSSPRO杯数学建模C题(第一阶段)机械零件加工过程中的位置识别全过程文档及程序

2018年认证杯SPSSPRO杯数学建模 基于轮廓特征的机械零件位置识别研究 C题 机械零件加工过程中的位置识别 原题再现&#xff1a; 在工业制造自动生产线中&#xff0c;在装夹、包装等工序中需要根据图像处理利用计算机自动智能识别零件位置&#xff0c;并由机械手将零件自动搬…

【算法】算法(模拟、指针等)解决字符串类题目(C++)

文章目录 1. 前言2. 解决 字符串类算法题14.最长公共前缀5.最长回文子串67.二进制求和43.字符串相乘 1. 前言 字符串题目有很多种&#xff0c;这里筛选几个考察模拟、双指针等的题目&#xff0c;并用相关算法解决。 2. 解决 字符串类算法题 14.最长公共前缀 思路 题意分析&…

统计学-R语言-5.3

文章目录 前言分位数统计量的标准误总结 前言 本篇文章即为概率与分布的最后一篇文章。 分位数 分位数函数是累积分布函数的反函数。 p-分位数是具有这样性质的一个值&#xff1a;小于或等于它的概率为p。 根据定义&#xff0c;中位数即50%分位数。 分位数通常用于置信区间的…

Python4Delphi安装及编译

1.下载或直接克隆python4delphi组件资源到指定目录,我这里下载到Components文件夹下,并对下载的文件夹进行了重命名为(P4D),重命名不是必须的 下载地址:https://github.com/pyscripter/python4delphi 2.安装 2.1在已下载的目录下进入Install文件夹,双击MultiInstaller.exe…

debian12部署Gitea服务之二——部署git-lfs

Debian安装gitlfs: 先更新下软件包版本 sudo apt update 安装 sudo apt install git-lfs 验证是否安装成功 git lfs version cd到Gitea仓库目录下 cd /mnt/HuHDD/Git/Gitea/Repo/hu/testrepo.git 执行lfs的初始化命令 git lfs install客户机Windows端在官网下载并安装Git-Lfs 再…

渗透测试之如何部署和使用Supershell

环境: Supershell v2.0.0 Centos 7.6 docker v. 21 问题描述: 如何部署和使用Supershell 解决方案: 1、下载最新release源码,解压后进入项目目录 wget https://github.com/tdragon6/Supershell/releases/latest/download/Supershell.tar.gz如果在线下很慢,用浏览…