Message Broker是一种消息验证、传输、路由的架构模式,其设计目标主要应用于下面这些场景:html
AMQP是Advanced Message Queuing Protocol的简称,它是一个面向消息中间件的开放式标准应用层协议。AMQP定义了这些特性:web
本文要介绍的RabbitMQ就是以AMQP协议实现的一种中间件产品,它能够支持多种操做系统,多种编程语言,几乎能够覆盖全部主流的企业级技术平台。spring
在RabbitMQ官网的下载页面https://www.rabbitmq.com/download.html
中,咱们能够获取到针对各类不一样操做系统的安装包和说明文档。这里,咱们将对几个经常使用的平台一一说明。编程
下面咱们采用的Erlang和RabbitMQ Server版本说明:浏览器
http://www.erlang.org/downloads
获取exe安装包,直接打开并完成安装。https://www.rabbitmq.com/download.html
获取exe安装包。在Mac OS X中使用brew工具,能够很容易的安装RabbitMQ的服务端,只须要按以下命令操做便可:安全
brew install erlang
brew install rabbitmq
经过上面的命令,RabbitMQ Server的命令会被安装到/usr/local/sbin
,并不会自动加到用户的环境变量中去,因此咱们须要在.bash_profile
或.profile
文件中增长下面内容:bash
PATH=$PATH:/usr/local/sbin
这样,咱们就能够经过rabbitmq-server
命令来启动RabbitMQ的服务端了。架构
在Ubuntu中,咱们可使用APT仓库来进行安装并发
apt-get install erlang
执行下面的命令,新增APT仓库到/etc/apt/sources.list.d
app
更新APT仓库的package list,执行sudo apt-get update
命令
sudo apt-get install rabbitmq-server
命令咱们能够直接经过配置文件的访问进行管理,也能够经过Web的访问进行管理。下面咱们将介绍如何经过Web进行管理。
rabbitmq-plugins enable rabbitmq_management
命令,开启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.
http://localhost:15672/
,并使用默认用户guest
登陆,密码也为guest
。咱们能够看到以下图的管理页面:从图中,咱们能够看到以前章节中提到的一些基本概念,好比:Connections、Channels、Exchanges、Queue等。第一次使用的读者,能够都点开看看都有些什么内容,熟悉一下RabbitMQ Server的服务端。
Admin
标签,在这里能够进行用户的管理。pom.xml
中引入以下依赖内容,其中spring-boot-starter-amqp
用于支持RabbitMQ。下面,咱们经过在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>
application.properties
中配置关于RabbitMQ的链接和用户信息,用户能够回到上面的安装内容,在管理页面中建立用户。 spring.application.name=rabbitmq-hello spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=spring spring.rabbitmq.password=123456
Sender
。经过注入AmqpTemplate
接口的实例来实现消息的发送,AmqpTemplate
接口定义了一套针对AMQP协议的基础操做。在Spring Boot中会根据配置来注入其具体实现。在该生产者,咱们会产生一个字符串,并发送到名为hello
的队列中。 @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); } }
Receiver
。经过@RabbitListener
注解定义该类对hello
队列的监听,并用@RabbitHandler
注解来指定对消息的处理方法。因此,该消费者实现了对hello
队列的消费,消费操做为输出消息的字符串内容。 @Component @RabbitListener(queues = "hello") public class Receiver { @RabbitHandler public void process(String hello) { System.out.println("Receiver : " + hello); } }
RabbitConfig
,用来配置队列、交换器、路由等高级信息。这里咱们以入门为主,先以最小化的配置来定义,以完成一个基本的生产和消费过程。 @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中包含当前链接的条目。
hello
队列中。 Sender : hello Sun Sep 25 11:06:11 CST 2016
hello
队列的监听程序执行了,并输出了接受到的消息信息。 Receiver : hello Sun Sep 25 11:06:11 CST 2016
经过上面的示例,咱们在Spring Boot应用中引入spring-boot-starter-amqp
模块,进行简单配置就完成了对RabbitMQ的消息生产和消费的开发内容。然而在实际应用中,咱们还有不少内容没有演示,这里不作更多的讲解,读者能够自行查阅RabbitMQ的官方教程,有更全面的了解。