当前位置: 首页 > news >正文

rabbitmq安装、基本使用

docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.12-management

docker会自己下载,然后运行

进入docker:

docker exec -it rabbitmq bash

进入容器,重启rabbitmq:rabbitmq-server restart

感觉所有的消息队列都差不多,都是创建,连接,发消息,获得消息

package com.quxiao;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;import java.io.IOException;
import java.util.concurrent.TimeoutException;/*** @program: springboot* @author: quxiao* @create: 2023-10-29 09:39**/
public class t1 {public static void main(String[] args) throws Exception {ConnectionFactory factory = new ConnectionFactory();factory.setHost("127.0.0.1");factory.setPort(5672);factory.setUsername("123");factory.setPassword("123");factory.setVirtualHost("/");Connection connection = null;Channel channel = null;try {//连接connection = factory.newConnection("生产者1");//通道channel = connection.createChannel();channel.queueDeclare("duilie1", false, false, false, null);channel.basicPublish("", "duilie1", null, "队列消息".getBytes());} catch (IOException e) {throw new RuntimeException(e);} catch (TimeoutException e) {throw new RuntimeException(e);} finally {channel.close();connection.close();}}
}

 消费:

package com.quxiao;import com.rabbitmq.client.*;import java.io.IOException;
import java.util.concurrent.TimeoutException;/*** @program: springboot* @author: quxiao* @create: 2023-10-29 10:11**/
public class t2 {public static void main(String[] args) throws Exception {ConnectionFactory factory = new ConnectionFactory();factory.setHost("127.0.0.1");factory.setPort(5672);factory.setUsername("123");factory.setPassword("123");factory.setVirtualHost("/");Connection connection = null;Channel channel = null;try {//连接connection = factory.newConnection("生产者1");//通道channel = connection.createChannel();
//            channel.queueDeclare("duilie1", false, false, false, null);
//            channel.basicPublish("", "duilie1", null, "队列消息".getBytes());channel.basicConsume("duilie1", true, new DeliverCallback() {@Overridepublic void handle(String consumerTag, Delivery message) throws IOException {System.out.println(new String(message.getBody(), "UTF-8"));}}, new CancelCallback() {@Overridepublic void handle(String consumerTag) throws IOException {System.out.println("接收消息失败");}});} catch (IOException e) {throw new RuntimeException(e);} catch (TimeoutException e) {throw new RuntimeException(e);} finally {channel.close();connection.close();}}
}

路由分组模式:

定义路由key,将队列绑定,发送到路由key,就会发到被绑定的所有队列。 

package com.quxiao;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;import java.io.IOException;
import java.util.concurrent.TimeoutException;/*** @program: springboot* @author: quxiao* @create: 2023-10-29 09:39**/
public class t1 {public static void main(String[] args) throws Exception {ConnectionFactory factory = new ConnectionFactory();factory.setHost("127");factory.setPort(5672);factory.setUsername("123");factory.setPassword("123");factory.setVirtualHost("/");Connection connection = null;Channel channel = null;try {//连接connection = factory.newConnection("生产者1");//通道channel = connection.createChannel();
//            channel.queueDeclare("", false, false, false, null);channel.basicPublish("amq.direct", "type2", null, "队列消息".getBytes());
//21} catch (IOException e) {throw new RuntimeException(e);} catch (TimeoutException e) {throw new RuntimeException(e);} finally {channel.close();connection.close();}}
}

http://www.lryc.cn/news/211590.html

相关文章:

  • Amaon CloudFront助力出海业务访问优化
  • python实现ModBusRTU服务端
  • yum安装node,npm
  • ESM蛋白质语言模型系列
  • RHCE-------Day1
  • Linux(Centos7)防火墙端口操作记录
  • 【MySQL数据库】初识MySQL数据库、安装MySQL
  • Keil Map信息解析
  • 在重生奇迹MU中如何选择最佳的挂机点?
  • IT行业中的热门职业及前景展望
  • linux练习
  • 【【带有握手信号的无符号数乘法verilog+ testbench 代码】】
  • 【Python机器学习】零基础掌握BayesianRidge贝叶斯回归
  • 【机器学习】朴素贝叶斯算法基本原理与计算案例
  • redis6.0源码分析:简单动态字符串sds
  • 1.7 攻击面和攻击树
  • 解决input在谷歌浏览器自动填充问题
  • Java字节码技术
  • Java SE 学习笔记(十八)—— 注解、动态代理
  • 虚拟内存之请求分页管理
  • lazarus开发:提升sqlite数据插入速度
  • 瑞萨RH850-P1X ECM和英飞凌TC3xx SMU对比
  • Ajax学习笔记第三天
  • ESP32-C3 低功耗懒人开关:传统开关轻松上云和本地控制
  • 前端学习路线指南:从入门到精通【①】
  • Flash模拟EEPROM原理浅析
  • Typora 最新激活方法
  • jenkins如何安装?
  • 从零开始的LINUX(三)
  • CleanMyMac2024永久免费版Mac系统磁盘清理工具