西安做网站电话,兰州优化网站,公司网址正确格式,达内教育口碑怎么样AMQP#xff1a;是Advanced Message Queuing Protocol的简称#xff0c;高级消息队列协议#xff0c;是一个面向消息中间件的开放式标准应用层协议。 定义了以下特性#xff1a;
消息方向消息队列消息路由#xff08;包括#xff1a;点到点和发布-订阅模式#xff09;可…AMQP是Advanced Message Queuing Protocol的简称高级消息队列协议是一个面向消息中间件的开放式标准应用层协议。 定义了以下特性
消息方向消息队列消息路由包括点到点和发布-订阅模式可靠性安全性
RabitMQ是以AMQP协议实现的一种中间件产品。RabbitMq是一个开源的AMQP实现服务器端用erlang语言编写支持多种客户端。
常用概念 通常由三个概念发消息者、队列、收消息者RabbitMQ 在这个基本概念之上, 多做了一层抽象, 在发消息者和 队列之间, 加入了交换器 (Exchange). 这样发消息者和队列就没有直接联系, 转而变成发消息者把消息给交换器, 交换器根据调度策略再把消息再给队列。 首先先安装Erland通过官方下载页面http://www.erlang.org/downloads获取exe安装包直接打开并完成安装。
接着
安装RabbitMq通过官方下载页面https://www.rabbitmq.com/download.html获取exe安装包。下载完成后直接运行安装程序。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并修改代码为
package com.example; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class HelloRabbitConfig { Bean public Queue helloQueue() { return new Queue(hello); } }
2. 编写发送消息类
在com.example包中增加类名称为HelloSender并修改代码为
package com.example; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; Component public class HelloSender { protected static Logger loggerLoggerFactory.getLogger(HelloSender.class); Autowired private AmqpTemplate rabbitTemplate; public String send(String name) { String context hello name -- new Date(); logger.debug(HelloSender: context); this.rabbitTemplate.convertAndSend(hello, context); return context; } }
3.编写接收消息类
在com.example包中增加类名称为HelloReceiver并修改代码为
package com.example; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; Component RabbitListener(queues hello) public class HelloReceiver { protected static Logger logger LoggerFactory.getLogger(HelloReceiver.class); RabbitHandler public void process(String hello) { logger.debug(HelloReceiver : hello); } }
4. 编写RestController 类调用发送消息
在com.example包中增加类名称为HelloController并修改代码为
package com.example; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; RestController public class HelloController { protected static Logger loggerLoggerFactory.getLogger(HelloController.class); Autowired private HelloSender helloSender; RequestMapping(/send/{name}) public String helloworld(PathVariable String name) { return helloSender.send(name); } }
5. 运行测试
在sbin文件夹CMD调用rabbitmq-server restart之后在工程所在目录打开命令行执行mvn spring-boot:run最后在浏览器输入http://localhost8080/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); } 整体整个工程比较简单没有什么特殊的配置仅仅三个类文件即可实现消息的发送和接受。