Centos7 安装rabbitMQ
首先需要安装erlang,根据rabbitMQ的版本不同安装的erlang版本也不一样:
我的系统是centos7 、Erlang 20.x rabbitMQ 3.6.11.
第一步添加yum源
# vi /etc/yum.repos.d/rabbitmq-erlang.repo
添加如下内容:
[rabbitmq-erlang]
name=rabbitmq-erlang
baseurl=https://dl.bintray.com/rabbitmq/rpm/erlang/20/el/7
gpgcheck=1
gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
repo_gpgcheck=0
enabled=1
第二步安装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端口:
#firewall-cmd --zone=public --add-port=5672/tcp --permanent
#firewall-cmd --reload
或者直接关闭防火墙
#systemctl stop firewalld.service
第六步 开启管理UI:
#rabbitmq-plugins enable rabbitmq_management
#firewall-cmd --zone=public --add-port=15672/tcp --permanent
#firewall-cmd --reload
在浏览器下打开地址:
http://192.168.0.119:15672/
rabbitmq默认创建的用户guest,密码也是guest,这个用户默认只能是本机访问,localhost或者127.0.0.1,从外部访问需要添加配置。
cd /etc/rabbitmq/
vi rabbitmq.config
编辑内容如下:
[{rabbit, [{loopback_users, []}]}].
这里的意思是开放使用,rabbitmq默认创建的用户guest,密码也是guest,这个用户默认只能是本机访问,localhost或者127.0.0.1,从外部访问需要添加上面的配置。
保存配置后重启服务
在Windows上进行测试:
新建.net core控制台项目,引用RabbitMQ.Client包:
Install-Package RabbitMQ.Client
测试代码:
public static void Main(string[] args)
{
ConnectionFactory factory = new ConnectionFactory();
factory.UserName = "guest";
factory.Password = "guest";
factory.VirtualHost = "/";
factory.HostName = "localhost";
//factory.HostName = "192.168.0.119";
try
{
IConnection conn = factory.CreateConnection();
IModel model = conn.CreateModel();
string exchangeName = "test";
string queueName = "testq";
string routingKey = "first";
model.ExchangeDeclare(exchangeName, ExchangeType.Direct);
model.QueueDeclare(queueName, false, false, false, null);
model.QueueBind(queueName, exchangeName, routingKey, null);
byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!");
model.BasicPublish(exchangeName, routingKey, null, messageBodyBytes);
Console.WriteLine("message sended.");
bool noAck = false;
BasicGetResult result = model.BasicGet(queueName, noAck);
if (result == null)
{
Console.Write("no message.");
}
else
{
IBasicProperties props = result.BasicProperties;
byte[] body = result.Body;
model.BasicAck(result.DeliveryTag, false);
string message = System.Text.Encoding.UTF8.GetString(body);
Console.Write(message);
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
也可以使用官网的例子(这里更清晰):
http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html
发送端:
using System;
using RabbitMQ.Client;
using System.Text;
class Send
{
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: "hello",
basicProperties: null,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
接收端:
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
class Receive
{
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
};
channel.BasicConsume(queue: "hello",
noAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
在Windows上发送,在CentOS上接收,效果如图:
上一篇: centos7安装rabbitMq
下一篇: centos7安装rabbitmq