##Message Broker与AMQP简介html
Message Broker是一种消息验证、传输、路由的架构模式,其设计目标主要应用于下面这些场景:git
消息路由到一个或多个目的地github
消息转化为其余的表现方式web
执行消息的汇集、消息的分解,并将结果发送到他们的目的地,而后从新组合相应返回给消息用户spring
调用Web服务来检索数据编程
响应事件或错误浏览器
使用发布-订阅模式来提供内容或基于主题的消息路由安全
AMQP是Advanced Message Queuing Protocol的简称,它是一个面向消息中间件的开放式标准应用层协议。AMQP定义了这些特性:bash
消息方向架构
消息队列
消息路由(包括:点到点和发布-订阅模式)
可靠性
安全性 ##RabbitMQ
本文要介绍的RabbitMQ就是以AMQP协议实现的一种中间件产品,它能够支持多种操做系统,多种编程语言,几乎能够覆盖全部主流的企业级技术平台。
##安装
在RabbitMQ官网的下载页面https://www.rabbitmq.com/download.html中,咱们能够获取到针对各类不一样操做系统的安装包和说明文档。这里,咱们将对几个经常使用的平台一一说明。
下面咱们采用的Erlang和RabbitMQ Server版本说明:
Erlang/OTP 19.1 RabbitMQ Server 3.6.5 ##Windows安装
一、安装Erland,经过官方下载页面http://www.erlang.org/downloads获取exe安装包,直接打开并完成安装。 二、安装RabbitMQ,经过官方下载页面https://www.rabbitmq.com/download.html获取exe安装包。 三、下载完成后,直接运行安装程序。 四、RabbitMQ Server安装完成以后,会自动的注册为服务,并以默认配置启动起来。
##Mac OS X安装
在Mac OS X中使用brew工具,能够很容易的安装RabbitMQ的服务端,只须要按以下命令操做便可:
一、brew更新到最新版本,执行:brew update 二、安装Erlang,执行:brew install erlang 三、安装RabbitMQ Server,执行:brew install rabbitmq 四、经过上面的命令,RabbitMQ Server的命令会被安装到/usr/local/sbin,并不会自动加到用户的环境变量中去,因此咱们须要在.bash_profile或.profile文件中增长下面内容:
PATH=$PATH:/usr/local/sbin 这样,咱们就能够经过rabbitmq-server命令来启动RabbitMQ的服务端了。
##Ubuntu安装
在Ubuntu中,咱们可使用APT仓库来进行安装
一、安装Erlang,执行:apt-get install erlang
echo 'deb http://www.rabbitmq.com/debian/ testing main' sudo tee /etc/apt/sources.list.d/rabbitmq.list
三、更新APT仓库的package list,执行sudo apt-get update命令
四、安装Rabbit Server,执行sudo apt-get install rabbitmq-server命令 ##Rabbit管理
咱们能够直接经过配置文件的访问进行管理,也能够经过Web的访问进行管理。下面咱们将介绍如何经过Web进行管理。
-> rabbitmq-plugins enable rabbitmq_management The following plugins have been enabled: mochiweb webmachine rabbitmq_web_dispatch amqp_client rabbitmq_management_agent rabbitmq_management Applying plugin configuration to rabbit@PC-201602152056... started 6 plugins.
从图中,咱们能够看到以前章节中提到的一些基本概念,好比:Connections、Channels、Exchanges、Queue等。第一次使用的读者,能够都点开看看都有些什么内容,熟悉一下RabbitMQ Server的服务端。
下面,咱们经过在Spring Boot应用中整合RabbitMQ,并实现一个简单的发送、接收消息的例子来对RabbitMQ有一个直观的感觉和理解。
在Spring Boot中整合RabbitMQ是一件很是容易的事,由于以前咱们已经介绍过Starter POMs,其中的AMQP模块就能够很好的支持RabbitMQ,下面咱们就来详细说说整合过程:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
spring.application.name=rabbitmq-hello spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=spring spring.rabbitmq.password=123456
@Component public class Sender { @Autowired private AmqpTemplate rabbitTemplate; public void send() { String context = "hello " + new Date(); System.out.println("Sender : " + context); this.rabbitTemplate.convertAndSend("hello", context); } }
@Component @RabbitListener(queues = "hello") public class Receiver { @RabbitHandler public void process(String hello) { System.out.println("Receiver : " + hello); } }
@Configuration public class RabbitConfig { @Bean public Queue helloQueue() { return new Queue("hello"); } }
@SpringBootApplication public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } }
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = HelloApplication.class) public class HelloApplicationTests { @Autowired private Sender sender; @Test public void hello() throws Exception { sender.send(); } }
完成程序编写以后,下面开始尝试运行。首先确保RabbitMQ Server已经开始,而后进行下面的操做:
启动应用主类,从控制台中,咱们看到以下内容,程序建立了一个访问127.0.0.1:5672中springcloud的链接。
o.s.a.r.c.CachingConnectionFactory : Created new connection: SimpleConnection@29836d32 [delegate=amqp://springcloud@127.0.0.1:5672/]
同时,咱们经过RabbitMQ的控制面板,能够看到Connection和Channels中包含当前链接的条目。
运行单元测试类,咱们能够看到控制台中输出下面的内容,消息被发送到了RabbitMQ Server的hello队列中。
Sender : hello Sun Sep 25 11:06:11 CST 2016
Receiver : hello Sun Sep 25 11:06:11 CST 2016
完整示例:Chapter5-2-1
开源中国:http://git.oschina.net/didispace/SpringBoot-Learning/tree/master/Chapter5-2-1 GitHub:https://github.com/dyc87112/SpringBoot-Learning/tree/master/Chapter5-2-1