AMQP:是Advanced Message Queuing Protocol的简称,高级消息队列协议,是一个面向消息中间件的开放式标准应用层协议。

定义了以下特性:

  • 消息方向
  • 消息队列
  • 消息路由(包括:点到点和发布-订阅模式)
  • 可靠性
  • 安全性

RabitMQ:是以AMQP协议实现的一种中间件产品。RabbitMq是一个开源的AMQP实现,服务器端用erlang语言编写,支持多种客户端。

常用概念:
通常由三个概念:发消息者、队列、收消息者,RabbitMQ 在这个基本概念之上, 多做了一层抽象, 在发消息者和 队列之间, 加入了交换器 (Exchange). 这样发消息者和队列就没有直接联系, 转而变成发消息者把消息给交换器, 交换器根据调度策略再把消息再给队列。

首先,先安装Erland,通过官方下载页面http://www.erlang.org/downloads获取exe安装包,直接打开并完成安装。

接着,

  1. 安装RabbitMq,通过官方下载页面https://www.rabbitmq.com/download.html获取exe安装包。
  2. 下载完成后,直接运行安装程序。
  3. RabbitMQ Server安装完成之后,会自动的注册为服务,并以默认配置启动起来。

环境搭建可以参考:http://blog.didispace.com/spring-boot-rabbitmq/

RabbitMQ管理:

我们可以直接通过配置文件的访问进行管理,也可以通过Web的访问进行管理。下面我们将介绍如何通过Web进行管理。

在sbin文件夹打开CMD,执行rabbitmq-plugins enable rabbitmq_management命令,开启Web管理插件,这样我们就可以通过浏览器来进行管理了。

重启mq的命令是:rabbitmq-server restart

  • 打开浏览器并访问:http://localhost:15672/,并使用默认用户guest登录,密码也为guest。我们可以看到如下图的管理页面:

  • 点击Admin标签,在这里可以进行用户的管理。例如添加用户,记得要给用户设置权限,不然可能会导致下面工程连接不上。

利用springboot来进行整合:

1. 编写配置文件类:

在com.example包中增加类,名称为HelloRabbitConfig,并修改代码为

  1. package com.example;
  2. import org.springframework.amqp.core.Queue;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. @Configuration
  6. public class HelloRabbitConfig {
  7. @Bean
  8. public Queue helloQueue() {
  9. return new Queue("hello");
  10. }
  11. }

2. 编写发送消息类:

在com.example包中增加类,名称为HelloSender,并修改代码为:

  1. package com.example;
  2. import java.util.Date;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.amqp.core.AmqpTemplate;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8. @Component
  9. public class HelloSender {
  10. protected static Logger logger=LoggerFactory.getLogger(HelloSender.class);
  11. @Autowired
  12. private AmqpTemplate rabbitTemplate;
  13. public String send(String name) {
  14. String context = "hello "+name+" --" + new Date();
  15. logger.debug("HelloSender: " + context);
  16. this.rabbitTemplate.convertAndSend("hello", context);
  17. return context;
  18. }
  19. }

3.编写接收消息类:

在com.example包中增加类,名称为HelloReceiver,并修改代码为

  1. package com.example;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.amqp.rabbit.annotation.RabbitHandler;
  5. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  6. import org.springframework.stereotype.Component;
  7. @Component
  8. @RabbitListener(queues = "hello")
  9. public class HelloReceiver {
  10. protected static Logger logger = LoggerFactory.getLogger(HelloReceiver.class);
  11. @RabbitHandler
  12. public void process(String hello) {
  13. logger.debug("HelloReceiver : " + hello);
  14. }
  15. }

4. 编写RestController 类,调用发送消息:

在com.example包中增加类,名称为HelloController,并修改代码为

  1. package com.example;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. public class HelloController {
  10. protected static Logger logger=LoggerFactory.getLogger(HelloController.class);
  11. @Autowired
  12. private HelloSender helloSender;
  13. @RequestMapping("/send/{name}")
  14. public String helloworld(@PathVariable String name) {
  15. return helloSender.send(name);
  16. }
  17. }

5. 运行测试

在sbin文件夹CMD调用rabbitmq-server restart之后,在工程所在目录打开命令行,执行mvn spring-boot:run,最后在浏览器输入http://localhost:8080/send/上帝

参考:https://blog.csdn.net/u012930316/article/details/76853778

工程解析:

1.配置文件:

在配置文件中,仅仅配置了一个名为hello的队列,以后发送,接收都与这个队列有关

2. 发送消息:

2.1发送消息使用模板机制,用springboot自动注入rabbitmqTemplate,它封装好了链接,管道,转换等

2.2消息转换和发送

void convertAndSend(String routingKey, Object message) throws AmqpException;

它的注释是:Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a specific routing key.

将一个Java对象转换为Amqp消息,然后用缺省的减缓及指定路由键发送信息。

public void convertAndSend(String exchange, String routingKey, final Object object, CorrelationData correlationData)
exchange:交换机名称

routingKey:路由关键字

object:发送的消息内容

correlationData:消息ID

3. 接收消息

3.1监听消息

在类上声明@RabbitListener(queues = "hello"),表示监听hello队列

3.2消息处理句柄

在接收处理消息的方法上声明@RabbitHandler

Annotation that marks a method to be the target of a Rabbit message  listener within a class that is annotated with {@link RabbitListener}.

消息消费者:

消费者负责声明交换机(生产者也可以声明),队列,以及两者的绑定操作。

交换机:

/**
     * 针对消费者配置
        FanoutExchange: 将消息分发到所有的绑定队列,无routingkey的概念
        HeadersExchange :通过添加属性key-value匹配
        DirectExchange:按照routingkey分发到指定队列
        TopicExchange:多关键字匹配
     */
    @Bean
    public DirectExchange defaultExchange() {
        return new DirectExchange(EXCHANGE);
    }

队列:

@Bean
    public Queue queue() {
        return new Queue("spring-boot-queue", true); //队列持久
 
    }

绑定:binding:

  @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(defaultExchange()).with(AmqpConfig.ROUTINGKEY);
    }

整体:整个工程比较简单,没有什么特殊的配置,仅仅三个类文件即可实现消息的发送和接受。

新手入门教程-------Spring Boot中集成RabbitMQ相关推荐

  1. Spring Boot中使用RabbitMQ

    很久没有写Spring Boot的内容了,正好最近在写Spring Cloud Bus的内容,因为内容会有一些相关性,所以先补一篇关于AMQP的整合. Message Broker与AMQP简介 Me ...

  2. 在spring boot中集成Swagger

    Swagger 在spring boot中集成Swagger 新建一个swagger项目 maven依赖 <!-- https://mvnrepository.com/artifact/io.s ...

  3. Spring Boot 中使用 RabbitMQ

    2019独角兽企业重金招聘Python工程师标准>>> RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python.Ruby..NET ...

  4. RabbitMQ(六)——Spring boot中消费消息的两种方式

    前言 上一篇博客中,我们只是简单总结了Spring boot中整合RabbitMQ的操作,针对消息消费的两种方式只是简单给了一个实例,这篇博客,我们进一步总结关于Spring boot消息消费的相关功 ...

  5. rabbitmq使用_Spring Boot中使用RabbitMQ

    Message Broker与AMQP简介 Message Broker是一种消息验证.传输.路由的架构模式,其设计目标主要应用于下面这些场景: 消息路由到一个或多个目的地 消息转化为其他的表现方式 ...

  6. spring boot中利用mybatis-generator插件生成代码

    使用Idea在spring boot中集成mybatis-generator,自动生成mapper.xml  model  dao 文件 一.配置 pom.xml 在pom.xml的<plugi ...

  7. Spring Boot中的事务管理

    什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合.由于数据操作在顺序执行的过程中,任何一步操作都有可能发生异常,异常会导致后续操作无法完成,此时由于业务逻辑并 ...

  8. mybatis注解开发_Spring Boot 中集成 MyBatis

    阅读本文约需要5分钟 大家好,我是你们的导师,我每天都会在这里给大家分享一些干货内容(当然了,周末也要允许老师休息一下哈).上次老师跟大家分享了Spring Boot 中的AOP处理,今天给大家分享下 ...

  9. redis 支持 json_Spring Boot 中集成 Redis

    阅读本文约需要5分钟 大家好,我是你们的导师,我每天都会在这里给大家分享一些干货内容(当然了,周末也要允许老师休息一下哈).上次老师跟大家分享了 Spring Boot 中的拦截器,今天跟大家分享下 ...

最新文章

  1. 学历案与深度学习电子书
  2. Quartz.NET基础知识概述
  3. python简单代码演示效果-用python画爱心及代码演示
  4. Intellij idea workflow 工作流插件安装
  5. VTK修炼之道28:图像统计_灰度直方图计算
  6. [翻译]2005年软件业界推出新产品非官方计划
  7. JAVA调用java执行,什么结果也没有
  8. AD9361常用配置概述
  9. 计算机结构系统的发展趋势,计算机体系结构的现状及发展趋势.docx
  10. 设CPU共有16根地址线,8根数据线,并用MREQ (低电平有效) .作访存控制信号,R/W作读写命令信号(高电平为读,,低电平为写)。
  11. LLVM编译技术应用分析
  12. 爬虫120例之第17例,用Python面向对象的思路,采集各种精彩句子
  13. WiFi广告路由器的利与弊
  14. 计算机 布局菜单栏 无法打勾,w7系统 我的电脑工具栏不见了
  15. 支付宝集五福最全攻略,五分钟集齐五福!
  16. Unsupervised Learning: Deep Auto-encoder
  17. Ajax速通(四)——axios
  18. 多孔氮化硼你了解吗?官能化/修饰/掺杂多孔氮化硼纳米纤维。分享介绍
  19. matlab兰州交通大学,兰州交通大学教务处.pdf
  20. Vue 3的高颜值UI组件库

热门文章

  1. 7种可能会导致内存泄漏的场景!
  2. 漫话:应用程序被拖慢?罪魁祸首竟然是Log4j!
  3. MySql 优化的 30 条建议
  4. 【Matlab】模式识别——聚类算法集锦
  5. Linux debian安装PyCharm教程
  6. Git本地缓存问题 修改密码后git无法拉取
  7. Ansible无敌详细入门教程
  8. spring IOC基本配置(xml配置和注解配置)
  9. python大数据开发平台_python示例
  10. 投篮c语言程序设计,教师招聘笔试体育之篮球必做20题(一)