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

一.编写RabbitMQ入门案例一.搭建项目1.创建maven项目(springboot-jar)2.修改pom.xml文件org.springframework.bootspring-boot-starter-parent2.0.2.RELEASEorg.springframework.bootspring-boot-starter-weborg.spring…

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

【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.net/126836.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 从零开始学android<Notification通知.四十四.>

    从零开始学android<Notification通知.四十四.>在android中有时会在主界面上收到某些应用的推送,有的可以包含图片,声音或者震动效果,当点击这些提示时,有时还可以进入到发送提示的的应用。这些提示的推送就是通知,当然通知早根本上也是你一种服务。首先想要使用通知就必须使用到Notification.Builder和NotificationManager这两个类使用Notification.Builder来取

    2022年6月16日
    25
  • 噪声熵和损失熵的定义_归一化信噪比

    噪声熵和损失熵的定义_归一化信噪比利用K-L散度(相对熵)确定VMD分解信号的K值和惩罚因子alpha,得到一组信号分量;计算各个分量的样本熵,根据样本熵的值,选取出噪声主导分量和有效分量;对噪声主导信号进行非局部均值(NLM)去噪;将去噪后的信号分量与剩余的有效信号分量进行重构得到去噪信号。

    2025年6月8日
    2
  • jdbc连接mysql8.0数据库_java jdbc连接数据库步骤

    jdbc连接mysql8.0数据库_java jdbc连接数据库步骤首先确认自己的mySQL数据库是多少版本,5.0版本和8.0版本在代码上会有很大的不同并且驱动包也不同8.0使用的是com.mysql.cj.jdbc.Driver,5.0使用的是com.mysql.jdbc.Driver。下面直接上8.0的代码Class.forName(“com.mysql.cj.jdbc.Driver”);conn=DriverManager.getConnection(“jdbc:mysql://localhost:3306/地址?use

    2025年10月11日
    1
  • Linux信号列表

    Linux信号列表

    2021年8月29日
    56
  • 基础:MVC三层架构

    基础:MVC三层架构MVC三层架构基于狂神说讲的MVC三层架构图,概述:mvc框架由model,view,controller组成,执行流程一般是:在controller访问model获取数据,通过view渲染页面。mvc模式是web开发中的基础模式,采用的是分层设计,各层之间职责分明。然而事与愿违,当我们日积月累的基于mvc模式开发之后,会逐渐的感受到层与层之间存在粘连和职责模棱两可的地方,这就是service层出现的重要原因。Dao:Dao层、设计模式(DataAccessObject),称为数据访问对象。它是

    2022年6月25日
    41
  • 哈理工 oj 2122 旅行(map + 最短路dij算法)

    哈理工 oj 2122 旅行(map + 最短路dij算法)旅行TimeLimit:1000MSMemoryLimit:32768KTotalSubmit:18(6users)TotalAccepted:3(3users)Rating:SpecialJudge:NoDescription“04.24,和Sakur

    2022年10月8日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号