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)
上一篇 2022年4月8日 下午5:00
下一篇 2022年4月8日 下午5:20


相关推荐

  • python如何安装matplotlib.pyplot_matplotlib中文

    python如何安装matplotlib.pyplot_matplotlib中文首先,一些博文上说可以在pycharm自动安装,也就是:File–Setting–ProjectInterpreter–±-输入指定模块安装,这对于社区版或教育版真的不行,好吗。安装的思路比较靠谱的,也是自己安装成功的方法。第一步:在官网上下载指定安装包:官网模块包:https://pypi.org/project需要什么模块就下载啥,需要注意的是与你的python版本一致(至于怎么一致,可以…

    2022年8月27日
    10
  • ipad写python代码用什么软件_iPad 能用来写代码吗?有哪些必备软件推荐?[通俗易懂]

    ipad写python代码用什么软件_iPad 能用来写代码吗?有哪些必备软件推荐?[通俗易懂]如果写Python我推荐pythonista,68软。pythonista还是比较基本的,文件夹,管理文件都有了。基础的代码提示也有,但是不能自动补全,要手点一下。建议买个蓝牙外设键盘,iPadpro官配的好使,这样就是一个准笔记本了。为啥说是准笔记本呢,因为iOS的应用协同太差了。git用workingcopy,编辑调试用pythonista,然后把新的从pythonista考到workin…

    2022年5月7日
    173
  • 前端响应式布局原理与方案(详细版)

    前端响应式布局原理与方案(详细版)引言响应式布局指的是同一页面在不同屏幕尺寸下有不同的布局 传统的开发方式是 PC 端开发一套 手机端再开发一套 而使用响应式布局只要开发一套就够 缺点就是 CSS 比较重 下面是博客网站对不同设备适配后的结果 分别是 iPhone5 SE iphone6 7 8 iphone6 7 8plus ipadpro dell 台式宽屏 1440X900 响应式设计与自适应设计的区别 响应式开发

    2026年3月16日
    5
  • 修改firefox地址栏的颜色

    修改firefox地址栏的颜色

    2021年8月16日
    55
  • Iocomp ActiveX VCL Ultra 5.12 SP6

    Iocomp ActiveX VCL Ultra 5.12 SP6Upto63Controls,choosethepacklevelyouneed!Real-Time,High-Speedcontrols!Professionallydesignedtofunctionlikereal-worldcontrols!Built-Incustompropertyeditorsforeaseofsetup!GDIbasedgraphics!RoyaltyFreeDistribution(ExcludesHMI

    2022年7月17日
    21
  • fprintf函数的作用_c语言fopen函数的用法

    fprintf函数的作用_c语言fopen函数的用法fprintf(简介)fprintf是C/C++中的一个格式化库函数,位于头文件中,其作用是格式化输出到一个流文件中;函数原型为intfprintf(FILE*stream,constchar*format,[argument]…),fprintf()函数根据指定的格式(format),向输出流(stream)写入数据(argument)。以“分别用十进制,八进制,十六进制输…

    2022年10月19日
    8

发表回复

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

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