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
    表示消息队列服务器实体
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • serverTimeZone_the server disconnected before

    serverTimeZone_the server disconnected beforejdbc连接MySQL时出现Theservertimezonevalue‘?й???’isunrecognized。。。错误:https://blog.csdn.net/YaphetS__YJ/article/details/77774874博文提到:jdbc-mysql6.0以上的版本必须配置serverTimezone参数。我的版本在此之上,构建URL时没配置所以报错解决…

  • 开源面向对象数据库 db4o 之旅: db4o 查询方式“db4o 之旅(二)”

    开源面向对象数据库 db4o 之旅: db4o 查询方式“db4o 之旅(二)”前言在开源面向对象数据库db4o之旅系列文章的第一部分:初识db4o中,作者介…

  • 3.5使用RangeValidator控件

    3.5使用RangeValidator控件使用RangeValidator控件检测表单字段的值是否在指定的最小值和最大值之间五个属性:ControlToValidate—被验证的表单字段的IDText--验证失败时显示的错误信息MininumValueMaxinumValueType-比较类型:String,Integer,Double,Date和Currency默认为String[code…

  • 网站开启cdn加速的最简单步骤

    网站开启cdn加速的最简单步骤

    2021年10月13日
  • pycharm 打不开了_pycharm激活成功教程之后打不开

    pycharm 打不开了_pycharm激活成功教程之后打不开pycharm打不开问题总结1:第一步:进入如下路径,找到cmd.exe,右键选择“以管理员身份运行”;第二步:在打开的cmd窗口中,输入netshwinsockreset,按回车键;第三步:重启电脑;第四步:重启后,双击pycharm图标就能打开了!2:你下载的应该是官方版的,然后自己加了网上下载的.jar激活成功教程插件,并添加了这个插件的路径到.vmoptions文件。打不开是因为你修改…

  • EJB学习总结[通俗易懂]

    EJB学习总结[通俗易懂]蓦然回首,居然稀里糊涂的过了N天了,才发现,总结没发表。前几天EJB学习到了一个阶段了,总结了一下,关于大篇幅的文字就不发表了,贴图一张,记录一下。另外,现在进入下一个阶段了,也是一个坎儿。SOA架构的学习。加油吧!

发表回复

您的电子邮箱地址不会被公开。

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