rabbitmq 基本总结

rabbitmq 的基本概念 vhost、broker、producer、 consumer、 exchange、 queue、 routing key

rabbitmq 常用的队列类型,工作队列(简单队列),pub/sub, routing key, topic 模式

<dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>5.16.0</version>
</dependency>
public class RabbitmqClientDemo {private static ConnectionFactory factory = new ConnectionFactory();private static String EXCHANGE_NAME = "exchange.fanout";private static String FANOUT_QUEUE = "queue.fanout";private static String DIRECT_EXCHANGE = "exchange_direct";private static String QUEUE_DIRCT = "queue.direct.02";private static String QUEUE_TOPIC_ONE = "queue.topic.01";private static String QUEUE_TOPIC_TWO = "queue.topic.02";private static String QUEUE_TOPIC_THREE = "queue.topic.03";private static String ROUNTING_KEY_ONE = "routing.key.01";private static String ROUNTING_KEY_TWO = "routing.key.02";private static String ROUNTING_KEY_THREE = "routing.key.03";private static String DEAD_MESSAGE_EXCHANGE = "EXCHANGE_DEAD";private static String DEAD_QUEUE = "queue.dead";static {factory.setHost("192.168.233.128");factory.setPort(5672);factory.setUsername("guest");factory.setPassword("guest");}public static Connection getConnection() throws IOException, TimeoutException {return factory.newConnection();}public static Channel createChannel() throws IOException, TimeoutException {Connection connection = getConnection();return connection.createChannel();}public static void main(String[] args) {//new WorkQueueProducer().start();//new WorkerConsumer().start();/*  new PublishConsumer().start();new PublishProducer().start();*///new TopicProducer().start();//new TopicConsumer().start();new DeadMessageProducer().start();new DeadMessageConsumer().start();}static class WorkQueueProducer extends Thread {@Overridepublic void run() {try {Connection connection = getConnection();Channel channel = connection.createChannel();channel = connection.createChannel();channel.queueDeclare("hello", false, false, false, null);channel.basicPublish("", "hello", null, "hello".getBytes());} catch (Exception e) {e.printStackTrace();} finally {try {//hannel.close();} catch (Exception e) {e.printStackTrace();}}}}static class WorkerConsumer extends Thread {@Overridepublic void run() {try {Connection connection = getConnection();Channel channel = connection.createChannel();channel.queueDeclare("hello", false, false, false, null);channel.basicQos(1);channel.basicConsume("hello", true, new DeliverCallback() {@Overridepublic void handle(String s, Delivery delivery) throws IOException {System.out.println(new String(delivery.getBody()));}}, consumerTag -> {});} catch (Exception e) {e.printStackTrace();} finally {try {//channel.close();} catch (Exception e) {e.printStackTrace();}}}}static class PublishProducer extends Thread {@Overridepublic void run() {try {Channel channel = createChannel();channel.exchangeDeclare(EXCHANGE_NAME, "fanout");channel.queueDeclare(FANOUT_QUEUE, true, false, false, null);channel.queueBind(FANOUT_QUEUE, EXCHANGE_NAME, "");for (int i = 1; i <= 40; i++) {String message = String.format("current orderId is %d, money is %d", UUID.randomUUID(), new Random().nextDouble());channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes(StandardCharsets.UTF_8));}} catch (Exception e) {e.printStackTrace();} finally {try {//channel.close();} catch (Exception e) {e.printStackTrace();}}}}static class PublishConsumer extends Thread {@Overridepublic void run() {try {Channel channel = createChannel();channel.exchangeDeclare(EXCHANGE_NAME, "fanout");channel.queueDeclare(FANOUT_QUEUE, true, false, false, null);channel.queueBind(FANOUT_QUEUE, EXCHANGE_NAME, "");DeliverCallback deliverCallback = (consumerTag, delivery) -> {System.out.println(new String(delivery.getBody(), StandardCharsets.UTF_8));};while (true) {channel.basicConsume(FANOUT_QUEUE, true, deliverCallback, consumerTag -> {});}} catch (Exception e) {e.printStackTrace();} finally {try {//channel.close();} catch (Exception e) {e.printStackTrace();}}}}static class TopicProducer extends Thread {@Overridepublic void run() {try {Channel channel = createChannel();channel.exchangeDeclare(DIRECT_EXCHANGE, "topic");channel.queueDeclare(QUEUE_TOPIC_ONE, true, false, false, null);channel.queueDeclare(QUEUE_TOPIC_TWO, true, false, false, null);channel.queueDeclare(QUEUE_TOPIC_THREE, true, false, false, null);channel.queueBind(QUEUE_TOPIC_ONE, DIRECT_EXCHANGE, ROUNTING_KEY_ONE);channel.queueBind(QUEUE_TOPIC_TWO, DIRECT_EXCHANGE, ROUNTING_KEY_TWO);channel.queueBind(QUEUE_TOPIC_TWO, DIRECT_EXCHANGE, ROUNTING_KEY_TWO);channel.queueBind(QUEUE_TOPIC_THREE, DIRECT_EXCHANGE, ROUNTING_KEY_THREE);channel.queueBind(QUEUE_TOPIC_THREE, DIRECT_EXCHANGE, ROUNTING_KEY_ONE);channel.queueBind(QUEUE_TOPIC_THREE, DIRECT_EXCHANGE, ROUNTING_KEY_TWO);for (int i = 1; i <= 10; i++) {String message = String.format("current orderId is %s, money is %s", UUID.randomUUID().toString(), new Random().nextDouble());if (i % 3 == 0) {System.out.println("send to topic1");channel.basicPublish(DIRECT_EXCHANGE, ROUNTING_KEY_ONE, null, message.getBytes(StandardCharsets.UTF_8));} else if (i % 3 == 1) {System.out.println("send to topic2");channel.basicPublish(DIRECT_EXCHANGE, ROUNTING_KEY_TWO, null, message.getBytes(StandardCharsets.UTF_8));} else {System.out.println("send to topic3");channel.basicPublish(DIRECT_EXCHANGE, ROUNTING_KEY_THREE, null, message.getBytes(StandardCharsets.UTF_8));}}} catch (Exception e) {e.printStackTrace();}}}static class TopicConsumer extends Thread {@Overridepublic void run() {try {Channel channel = createChannel();channel.exchangeDeclare(DIRECT_EXCHANGE, "topic");channel.queueDeclare(QUEUE_TOPIC_THREE, true, false, false, null);channel.queueBind(QUEUE_TOPIC_THREE,EXCHANGE_NAME,"routing.key.*")DeliverCallback deliverCallback = (consumerTag, delivery) -> {System.out.println(delivery.getEnvelope().getRoutingKey());System.out.println(new String(delivery.getBody(), StandardCharsets.UTF_8));};while (true) {channel.basicConsume(QUEUE_TOPIC_THREE, true, deliverCallback, consumerTag -> {});}} catch (Exception e) {e.printStackTrace();} finally {try {//channel.close();} catch (Exception e) {e.printStackTrace();}}}}static class DeadMessageProducer extends Thread {@Overridepublic void run() {try {Channel channel = createChannel();channel.exchangeDeclare(DIRECT_EXCHANGE, "direct");channel.queueDeclare(DEAD_QUEUE, true, false, false, null);channel.queueBind(DEAD_QUEUE, DEAD_MESSAGE_EXCHANGE, "routing.direct02");for (int i = 1; i <= 40; i++) {String message = String.format("current orderId is %s, money is %s", UUID.randomUUID().toString(), new Random().nextDouble());AMQP.BasicProperties prop = new AMQP.BasicProperties().builder().expiration("30000").build();channel.basicPublish(DIRECT_EXCHANGE, "routing.direct02", prop, message.getBytes(StandardCharsets.UTF_8));System.out.println("send to topic1");}} catch (Exception e) {e.printStackTrace();}}}static class DeadMessageConsumer extends Thread {@Overridepublic void run() {try {Channel channel = createChannel();/*channel.exchangeDeclare(DEAD_MESSAGE_EXCHANGE, "direct");Map<String, Object> deadLetterParams = new HashMap<>(2);deadLetterParams.put("x-dead-letter-exchange", DEAD_MESSAGE_EXCHANGE);deadLetterParams.put("x-dead-letter-routing-key", "routing.dead02");deadLetterParams.put("x-max-length", 2);*//*channel.queueDeclare(QUEUE_DIRCT, true, false, false, deadLetterParams);channel.queueBind(DEAD_QUEUE, DEAD_MESSAGE_EXCHANGE, "routing.dead02");*/channel.exchangeDeclare(DIRECT_EXCHANGE, "direct");channel.queueBind(QUEUE_DIRCT, DIRECT_EXCHANGE, "routing.direct02");DeliverCallback callback  = (consumerTag, delivery) -> {System.out.println(delivery.getEnvelope().getRoutingKey());System.out.println(new String(delivery.getBody(), StandardCharsets.UTF_8));};/* DeliverCallback callback = (consumerTag, delivery) -> {String receivedMessage = new String(delivery.getBody());System.out.println("C1接收到消息:" + receivedMessage + "并且拒绝签收了");// 禁止重新入队channel.basicReject(delivery.getEnvelope().getDeliveryTag(), false);};*/while (true) {//channel.basicConsume(QUEUE_DIRCT, true, deliverCallback, consumerTag -> {});channel.basicConsume(QUEUE_DIRCT, true, callback, (consumerTag) -> {System.out.println(consumerTag + "消费者取消消费消息");});}} catch (Exception e) {e.printStackTrace();} finally {try {//channel.close();} catch (Exception e) {e.printStackTrace();}}}}

整合springboot

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

rabbitmq 的核心配置(相比于其他的mq,rabbit 有图形用户界面,可以傻瓜操作)

https://blog.csdn.net/leesinbad/article/details/128670794

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

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

相关文章

启动项目报502怎么处理呢?

您好&#xff0c;我是码农飞哥&#xff08;wei158556&#xff09;&#xff0c;感谢您阅读本文&#xff0c;欢迎一键三连哦。 &#x1f4aa;&#x1f3fb; 1. Python基础专栏&#xff0c;基础知识一网打尽&#xff0c;9.9元买不了吃亏&#xff0c;买不了上当。 Python从入门到精…

蓝桥杯练习系统(算法训练)ALGO-982 最小距离

资源限制 内存限制&#xff1a;256.0MB C/C时间限制&#xff1a;1.0s Java时间限制&#xff1a;3.0s Python时间限制&#xff1a;5.0s 最小距离 问题描述 数轴上有n个数字&#xff0c;求最近的两个数&#xff0c;即min(abs(x-y)) 输入格式 第一行包含一个整数n。   …

Redis的Lua脚本

Redis提供了Lua脚本功能&#xff0c;在一个脚本中编写多条Redis命令&#xff0c;确保多条redis命令执行时的原子性。&#xff08;让判断线程标识和释放锁原子执行&#xff09; 1.下载lua插件 2.创建lua文件 3.调用lua脚本 public void unlock() {//调用lua脚本原子性的判断线…

CSS中元素的层叠顺序

层叠顺序&#xff0c;英文称作 stacking order&#xff0c;表示元素发生层叠时有着特定的垂直显示顺序。下面是盒模型的层叠规则&#xff1a; 对于上图&#xff0c;由上到下分别是&#xff1a; &#xff08;1&#xff09;背景和边框&#xff1a;建立当前层叠上下文元素的背景…

【Qt】四种绘图设备详细使用

绘图设备有4个: **绘图设备是指继承QPainterDevice的子类————**QPixmap QImage QPicture QBitmap(黑白图片) QBitmap——父类QPixmapQPixmap图片类&#xff0c;主要用来显示&#xff0c;它针对于显示器显示做了特殊优化&#xff0c;依赖于平台的&#xff0c;只能在主线程…

Linux系统——web服务拓展练习

目录 一、实验环境搭建 1. Centos 7-5——Client 2. Centos 7-1——网关服务器 3. Centos 7-2——Web1 4. Centos 7-3——Web2 5. Centos 7-4——Nginx 二、在Nginx服务器上搭建LNMP服务&#xff0c;并且能够对外提供Discuz论坛服务&#xff1b;在Web1、Web2服务器上搭建…

数据集下载汇总

国家数据网 https://data.stats.gov.cn/ 国家数据是国家统计局发布统计信息的网站&#xff0c;包含了我国经济、民生、农业、工业、运输、旅游、教育、科技、卫生等多个方面的数据&#xff0c;并且在月度、季度、年度都有覆盖&#xff0c;较为全面和权威&#xff0c;对于社会…

【数据库-黑马笔记】基础-函数和约束

本文参考b站黑马数据库视频,总结详细全面的笔记 ,可结合视频观看27~36集 MYSQL 的基础知识框架如下 目录 一、 函数 1、字符串函数 2、数值函数 3、日期函数 4、流程函数 5、小结: 二、约束 1、概述 2、 约束演示 3、外键约束 4、外键删除更新行为 5、小结: …

如何查看自己电脑显卡的内存

ctrlaltdelete 打开任务管理器&#xff0c;然后打开性能页面,你会看到一个GPU内存&#xff0c;这个是自己GPU的内存&#xff0c;也叫专用GPU内存&#xff0c;而那个共享GPU 内存是不够用的时候借用电脑的内存。 下面是专用GPU内存和共享GPU内存的具体区别 专用GPU内存和共享GPU…

HarmonyOS 数据持久化 关系型数据库之 增删改逻辑编写

好 上文 HarmonyOS 数据持久化 关系型数据库之 初始化操作 我们讲了 关系型数据库初始化 那么 本文 我们来编写 增删改 相关代码 打开我们的项目 打开到 我们之前写的这个 relationalClass 类 然后 我们在 initTaskDB 下面再定义一个函数 叫 addTask 的函数 用来执行添加逻辑 …

【论文笔记】Mamba: Linear-Time Sequence Modeling with Selective State Spaces

原文链接&#xff1a;https://arxiv.org/abs/2312.00752 1. 引言 基石模型&#xff08;FM&#xff09;的主干网络通常是序列模型&#xff0c;处理任意的输入序列。但现代FM主要基于Transformer这一序列模型&#xff0c;及其核心的注意力。但是&#xff0c;自注意力仅能在上下…

EVMProxyInspect:一款功能强大的多链EVM代理检测工具

关于EVMProxyInspect EVMProxyInspect是一款针对以太坊EVM代理的检测工具&#xff0c;该工具可以帮助广大研究人员在多链生态中检测EVM代理。 该工具能够展示类似etherscan这样的工具是如何检测代理的&#xff0c;并且允许我们自行进行代理检测。需要注意的是&#xff0c;该工…

Python:在 Ubuntu 上安装 pip的方法

目录 1、检测是否已安装pip 2、更新软件源 3、安装 4、检测是否安装成功 pip和pip3都是Python包管理工具&#xff0c;用于安装和管理Python包。 在Ubuntu上&#xff0c;pip和pip3是分别针对Python2和Python3版本的pip工具。 pip3作用&#xff1a;自动下载安装Python的库文…

Linux(Centos7)安装Docker 教程

目录 1&#xff0c;卸载旧版本2&#xff0c;安装docker软件包3,设置镜像仓库地址4,安装最新版Docker Engine容器5&#xff0c;启动Docker 要安装Docker Engine&#xff0c;您需要CentOS 7或8的维护版本。不支持或未测试存档版本。 1&#xff0c;卸载旧版本 sudo yum remove do…

webUI自动化之元素及浏览器操作

一、元素定位方式 1、元素属性定位&#xff1a; 1 element driver.find_element_by_id(self, id)    该类方法已经过时&#xff0c;新的方法如下&#xff1a; element driver.find_element(By.ID, ID 值)        # 用元素的 ID 属性定位element driver.find_eleme…

一大波你可能不知道的 Linux 网络工具

如果要在你的系统上监控网络&#xff0c;那么使用命令行工具是非常实用的&#xff0c;并且对于 Linux 用户来说&#xff0c;有着许许多多现成的工具可以使用&#xff0c;如&#xff1a;nethogs, ntopng, nload, iftop, iptraf, bmon, slurm, tcptrack, cbm, netwatch, collectl…

idea:springboot项目搭建

目录 一、创建项目 1、File → New → Project 2、Spring Initializr → Next 3、填写信息 → Next 4、web → Spring Web → Next 5、填写信息 → Finish 6、处理配置不合理内容 7、注意事项 7.1 有依赖包&#xff0c;却显示找不到依赖&#xff0c;刷新一下maven 7.…

社交媒体的未来图景:探索Facebook的数字化之旅

随着科技的迅猛发展&#xff0c;数字化社交已经成为了我们日常生活中不可或缺的一部分。在这个数字化时代&#xff0c;社交媒体平台扮演着重要角色&#xff0c;其中Facebook作为社交媒体的先锋&#xff0c;不断探索创新之路&#xff0c;引领着数字化社交的未来发展。本文将深入…

两天学会微服务网关Gateway-Gateway路由规则

锋哥原创的微服务网关Gateway视频教程&#xff1a; Gateway微服务网关视频教程&#xff08;无废话版&#xff09;_哔哩哔哩_bilibiliGateway微服务网关视频教程&#xff08;无废话版&#xff09;共计17条视频&#xff0c;包括&#xff1a;1_Gateway简介、2_Gateway工作原理、3…

和数软件:区块链技术的爆发与冲击

什么是区块链&#xff1f;它是如何发展而来的&#xff1f;应用在哪些领域&#xff1f;将会对我国的社会经济产生哪些重大影响&#xff1f; 什么是区块链 区块链作为一种底层技术&#xff0c;最早的实践是数字货币。根据最早的中本聪定义&#xff0c;区块链实质上是一种基于网…