前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RabbitMQ入门案例【java资源库:http://www.gxcode.top/code】[通俗易懂]

RabbitMQ入门案例【java资源库:http://www.gxcode.top/code】[通俗易懂]

作者头像
全栈程序员站长
发布2022-07-23 16:51:03
1520
发布2022-07-23 16:51:03
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

【java资源库】:http://www.gxcode.top/code 一.编写RabbitMQ入门案例 一.搭建项目 1.创建maven项目(springboot -jar) 2.修改pom.xml文件

org.springframework.boot spring-boot-starter-parent 2.0.2.RELEASE

org.springframework.boot spring-boot-starter-web

org.springframework.boot spring-boot-starter-test test

org.springframework.boot spring-boot-starter-amqp

3.创建application.properties文件,并添加rabbitmq配置信息 spring.application.name=springcloud-mq spring.rabbitmq.host=192.168.70.131 spring.rabbitmq.port=5672 spring.rabbitmq.username=oldlu spring.rabbitmq.password=123456 4.创建spring boot启动类 @SpringBootApplication public class AppStart { public static void main(String[] args) { SpringApplication.run(AppStart.class, args); } }

二.编写代码 1.创建队列 //创建消息队列 @Configuration public class QueueConfig { //创建队列 @Bean public Queue createQueue(){return new Queue(“hello-queue”);} } 2.创建消息提供者 @Component public class Sender { @Autowired private AmqpTemplate rabbitAmqpTemplate; //发送消息的方法 public void send(String msg){ //向消息队列发送消息 //参数一:队列的名称。 //参数二:消息 this.rabbitAmqpTemplate.convertAndSend(“hello-queue”,msg); } } 3.消息接收者 @Component public class Receiver { // 接收消息的方法。采用消息队列监听机制 @RabbitListener(queues=“hello-queue”) public void process(String msg){ System.out.println(“receiver: “+msg); } }

三.测试 1测试代码 //消息队列测试类 @RunWith(SpringRunner.class) @SpringBootTest(classes=SpringbootServerApplication.class) public class QueueTest { @Autowired private Sender sender; //测试消息队列 @Test public void test1(){ this.sender.send(“Hello RabbitMQ”); } }

3.Rabbitmq原理图

1.Message 消息。 2.Publisher 消息的生产者 3.Consumer 消息的消费者 4.Exchange 交换器。用来接收生产者发送的消息并将这些消息路由给服务器中的队列。 种常用的交换器类型

  1. direct(发布与订阅完全匹配)
  2. fanout(广播)
  3. topic(主题,规则匹配) 5.Binding 绑定。 6.Queue 消息队列。 7.Routing-key 路由键。 8.Connection 链接。指rabbit 服务器和服务建立的TCP 链接。 9.Channel 信道。 10.Virtual Host 虚拟主机 11.Borker 表示消息队列服务器实体

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/126836.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月8,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
消息队列 CMQ 版
消息队列 CMQ 版(TDMQ for CMQ,简称 TDMQ CMQ 版)是一款分布式高可用的消息队列服务,它能够提供可靠的,基于消息的异步通信机制,能够将分布式部署的不同应用(或同一应用的不同组件)中的信息传递,存储在可靠有效的 CMQ 队列中,防止消息丢失。TDMQ CMQ 版支持多进程同时读写,收发互不干扰,无需各应用或组件始终处于运行状态。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档