RabbitMQ的基本原理
首先需要安装erlang,根据rabbitMQ的版本不同安装的erlang版本也不一样:
erlang与rabbitMQ版本关系
我的系统是centos7 、Erlang 20.x rabbitMQ 3.6.11.
第一步添加yum源
1
2
3 1# vi /etc/yum.repos.d/rabbitmq-erlang.repo
2
3
1
2
3
4
5
6
7
8
9 1添加如下内容:
2[rabbitmq-erlang]
3name=rabbitmq-erlang
4baseurl=https://dl.bintray.com/rabbitmq/rpm/erlang/20/el/7
5gpgcheck=1
6gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
7repo_gpgcheck=0
8enabled=1
9
第二步安装Erlang
#yum celan all
# yum install erlang
检查erlang是否安装好
#erl -version
显示如下内容说明安装成功
Erlang (SMP,ASYNC_THREADS,HIPE) (BEAM) emulator version 9.3.3.2
第三步安装 socat
#yum -y install socat
第四步下载并安装rabbitMQ
# wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.6/rabbitmq-server-3.6.6-1.el7.noarch.rpm
#rpm -vih rabbitmq-server-3.6.6-1.el7.noarch.rpm
第五步启动
#systemctl start rabbitmq-server.service
可以查看服务状态:
#systemctl status rabbitmq-server.service
开放5672端口:
1
2
3 1#firewall-cmd --zone=public --add-port=5672/tcp --permanent
2#firewall-cmd --reload
3
或者直接关闭防火墙
#systemctl stop firewalld.service
第六步 开启管理UI:
1
2
3
4 1#rabbitmq-plugins enable rabbitmq_management
2#firewall-cmd --zone=public --add-port=15672/tcp --permanent
3#firewall-cmd --reload
4
在浏览器下打开地址:
1
2 1http://192.168.0.119:15672/
2
rabbitmq默认创建的用户guest,密码也是guest,这个用户默认只能是本机访问,localhost或者127.0.0.1,从外部访问需要添加配置。
1
2
3 1cd /etc/rabbitmq/
2vi rabbitmq.config
3
编辑内容如下:
1
2 1[{rabbit, [{loopback_users, []}]}].
2
这里的意思是开放使用,rabbitmq默认创建的用户guest,密码也是guest,这个用户默认只能是本机访问,localhost或者127.0.0.1,从外部访问需要添加上面的配置。
保存配置后重启服务
springboot整合RabbitMQ
SpringBoot+RabbitMq的使用
在Windows上进行测试:
新建.net core控制台项目,引用RabbitMQ.Client包:
1
2 1Install-Package RabbitMQ.Client
2
测试代码:
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 1public static void Main(string[] args)
2 {
3 ConnectionFactory factory = new ConnectionFactory();
4 factory.UserName = "guest";
5 factory.Password = "guest";
6 factory.VirtualHost = "/";
7 factory.HostName = "localhost";
8 //factory.HostName = "192.168.0.119";
9 try
10 {
11 IConnection conn = factory.CreateConnection();
12 IModel model = conn.CreateModel();
13 string exchangeName = "test";
14 string queueName = "testq";
15 string routingKey = "first";
16
17 model.ExchangeDeclare(exchangeName, ExchangeType.Direct);
18 model.QueueDeclare(queueName, false, false, false, null);
19 model.QueueBind(queueName, exchangeName, routingKey, null);
20 byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!");
21 model.BasicPublish(exchangeName, routingKey, null, messageBodyBytes);
22 Console.WriteLine("message sended.");
23
24 bool noAck = false;
25 BasicGetResult result = model.BasicGet(queueName, noAck);
26 if (result == null)
27 {
28 Console.Write("no message.");
29 }
30 else
31 {
32 IBasicProperties props = result.BasicProperties;
33 byte[] body = result.Body;
34 model.BasicAck(result.DeliveryTag, false);
35 string message = System.Text.Encoding.UTF8.GetString(body);
36 Console.Write(message);
37 }
38 }
39 catch (Exception ex)
40 {
41 Console.Write(ex.Message);
42 }
43 }
44
也可以使用官网的例子(这里更清晰):
http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html
发送端:
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 1using System;
2using RabbitMQ.Client;
3using System.Text;
4
5class Send
6{
7 public static void Main()
8 {
9 var factory = new ConnectionFactory() { HostName = "localhost" };
10 using(var connection = factory.CreateConnection())
11 using(var channel = connection.CreateModel())
12 {
13 channel.QueueDeclare(queue: "hello",
14 durable: false,
15 exclusive: false,
16 autoDelete: false,
17 arguments: null);
18
19 string message = "Hello World!";
20 var body = Encoding.UTF8.GetBytes(message);
21
22 channel.BasicPublish(exchange: "",
23 routingKey: "hello",
24 basicProperties: null,
25 body: body);
26 Console.WriteLine(" [x] Sent {0}", message);
27 }
28
29 Console.WriteLine(" Press [enter] to exit.");
30 Console.ReadLine();
31 }
32}
33
接收端:
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 1using RabbitMQ.Client;
2using RabbitMQ.Client.Events;
3using System;
4using System.Text;
5
6class Receive
7{
8 public static void Main()
9 {
10 var factory = new ConnectionFactory() { HostName = "localhost" };
11 using(var connection = factory.CreateConnection())
12 using(var channel = connection.CreateModel())
13 {
14 channel.QueueDeclare(queue: "hello",
15 durable: false,
16 exclusive: false,
17 autoDelete: false,
18 arguments: null);
19
20 var consumer = new EventingBasicConsumer(channel);
21 consumer.Received += (model, ea) =>
22 {
23 var body = ea.Body;
24 var message = Encoding.UTF8.GetString(body);
25 Console.WriteLine(" [x] Received {0}", message);
26 };
27 channel.BasicConsume(queue: "hello",
28 noAck: true,
29 consumer: consumer);
30
31 Console.WriteLine(" Press [enter] to exit.");
32 Console.ReadLine();
33 }
34 }
35}
36
在Windows上发送,在CentOS上接收,效果如图: