RabbitMQ安装和详解

RabbitMQ介绍

RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。RabbitMQ主要是为了实现系统之间的双向解耦而实现的。当生产者大量产生数据时,消费者无法快速消费,那么需要一个中间层。保存这个数据。

AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计。消息中间件主要用于组件之间的解耦,消息的发送者无需知道消息使用者的存在,反之亦然。AMQP的主要特征是面向消息、队列、路由(包括点对点和发布/订阅)、可靠性、安全。

RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP等,支持AJAX。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。

RabbitMQ Mac 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
➜  ~ brew install rabbitmq
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 3 taps (homebrew/cask, homebrew/core, phinze/cask).
==> New Formulae
peru skopeo ucloud
==> Updated Formulae
git ✔ cmdshelf imagemagick phpunit
vim ✔ double-conversion imagemagick@6 pqiv
zsh ✔ eslint kubernetes-cli pulumi
activemq fauna-shell kustomize rclone
afflib folly libimobiledevice ripgrep
annie fonttools libphonenumber swiftformat
ansible geoipupdate libspectre terragrunt
apache-drill ghostscript lnav the_platinum_searcher
apktool glm logentries tor
at-spi2-atk gnu-indent mariadb@10.1 upx
atk goreleaser mdcat vault
atomicparsley grafana media-info watchexec
aws-okta grunt-cli mongodb@3.4 webpack
awscli gutenberg mps-youtube wireguard-tools
bwfmetaedit handbrake msgpack wskdeploy
chronograf harfbuzz nano xmount
cloc hlint node@8 youtube-dl
cmake homebank pdfsandwich
==> Deleted Formulae
llvm@3.7

==> Installing dependencies for rabbitmq: jpeg, libpng, libtiff, wxmac, erlang
==> Installing rabbitmq dependency: jpeg
==> Downloading https://homebrew.bintray.com/bottles/jpeg-9c.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring jpeg-9c.sierra.bottle.tar.gz
🍺 /usr/local/Cellar/jpeg/9c: 21 files, 724.6KB
==> Installing rabbitmq dependency: libpng
==> Downloading https://homebrew.bintray.com/bottles/libpng-1.6.35.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libpng-1.6.35.sierra.bottle.tar.gz
🍺 /usr/local/Cellar/libpng/1.6.35: 26 files, 1.2MB
==> Installing rabbitmq dependency: libtiff
==> Downloading https://homebrew.bintray.com/bottles/libtiff-4.0.9_4.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libtiff-4.0.9_4.sierra.bottle.tar.gz
🍺 /usr/local/Cellar/libtiff/4.0.9_4: 246 files, 3.5MB
==> Installing rabbitmq dependency: wxmac
==> Downloading https://homebrew.bintray.com/bottles/wxmac-3.0.4_1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring wxmac-3.0.4_1.sierra.bottle.tar.gz
🍺 /usr/local/Cellar/wxmac/3.0.4_1: 813 files, 23.8MB
==> Installing rabbitmq dependency: erlang
==> Downloading https://homebrew.bintray.com/bottles/erlang-21.0.8.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring erlang-21.0.8.sierra.bottle.tar.gz
==> Caveats
Man pages can be found in:
/usr/local/opt/erlang/lib/erlang/man

Access them with `erl -man`, or add this directory to MANPATH.
==> Summary
🍺 /usr/local/Cellar/erlang/21.0.8: 5,668 files, 271.7MB
==> Installing rabbitmq
==> Downloading https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.7/rabbitmq-server-generic-unix-3.7.7
######################################################################## 100.0%
==> /usr/bin/unzip -qq -j /usr/local/Cellar/rabbitmq/3.7.7_1/plugins/rabbitmq_management-3.7.7.ez rabbitmq_m
==> Caveats
Management Plugin enabled by default at http://localhost:15672

Bash completion has been installed to:
/usr/local/etc/bash_completion.d

To have launchd start rabbitmq now and restart at login:
brew services start rabbitmq
Or, if you don't want/need a background service you can just run:
rabbitmq-server
==> Summary
🍺 /usr/local/Cellar/rabbitmq/3.7.7_1: 233 files, 13.3MB, built in 1 minute 31 seconds
==> Caveats
==> erlang
Man pages can be found in:
/usr/local/opt/erlang/lib/erlang/man

Access them with `erl -man`, or add this directory to MANPATH.
==> rabbitmq
Management Plugin enabled by default at http://localhost:15672

Bash completion has been installed to:
/usr/local/etc/bash_completion.d

To have launchd start rabbitmq now and restart at login:
brew services start rabbitmq
Or, if you don't want/need a background service you can just run:
rabbitmq-server
➜ ~

相关信息

1
2
3
4
5
6
7
8
9
安装目录:
/usr/local/Cellar/rabbitmq/3.7.7_1

现在启动rabbitmq并在登录时重新启动:
brew services start rabbitmq
或者,如果你不想/需要后台服务,你可以运行:
rabbitmq-server

初始化账号密码,guest

启动

1
2
3
4
5
6
7
8
9
10
11
12
➜  ~ cd /usr/local/Cellar/rabbitmq/3.7.7_1/
➜ 3.7.7_1 sbin/rabbitmq-server

## ##
## ## RabbitMQ 3.7.7. Copyright (C) 2007-2018 Pivotal Software, Inc.
########## Licensed under the MPL. See http://www.rabbitmq.com/
###### ##
########## Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log
/usr/local/var/log/rabbitmq/rabbit@localhost_upgrade.log

Starting broker...
completed with 6 plugins.

Spring Boot 集成 RabbitMQ

配置Maven依赖
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置文件,配置 rabbitmq 的安装地址、端口及账户信息
1
2
3
4
5
6
spring.application.name=spirng-boot-rabbitmq

spring.rabbitmq.host=192.168.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456
定义队列
1
2
3
4
5
6
7
8
9
@Configuration
public class RabbitConfig {

@Bean
public Queue Queue() {
return new Queue("hello");
}

}
发送者
AmqpTemplate 是 Spring Boot 提供的默认实现。
1
2
3
4
5
6
7
8
9
10
11
12
public class HelloSender {

@Autowired
private AmqpTemplate rabbitTemplate;

public void send() {
String context = "hello " + new Date();
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("hello", context);
}

}
接收者

注意使用注解@RabbitListener,使用 queues 指明队列名称,@RabbitHandler为具体接收的方法。

1
2
3
4
5
6
7
8
9
10
@Component
@RabbitListener(queues = "hello")
public class HelloReceiver {

@RabbitHandler
public void process(String hello) {
System.out.println("Receiver : " + hello);
}

}
测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RunWith(SpringRunner.class)
@Spring BootTest
public class RabbitMqHelloTest {

@Autowired
private HelloSender helloSender;

@Test
public void hello() throws Exception {
helloSender.send();
Thread.sleep(1000l);
}

}

注意,发送者和接收者的 queue name 必须一致,不然不能接收。

让测试方法等待 1 秒,让接收者接收到消息,不然应用退出时可能还没有接收到消息。以上一个最常用简单的示例就完成了。

以上参考:

springboot(八):RabbitMQ详解