1 安装erlang
sudo dnf install erlang
2 安装RabbitMQ
sudo dnf install rabbitmq-server
3 启动 RabbitMQ 服务
service rabbitmq-server start
4 开放5672端口
firewall-cmd --zone=public --add-port=5672/tcp --permanent firewall-cmd --reload
5 开启管理UI
sudo rabbitmq-plugins enable rabbitmq_management firewall-cmd --zone=public --add-port=15672/tcp --permanent firewall-cmd --reload
RabbitMQ安装完成接下来我们使用.net core写一个测试程序。
新建项目
dotnet new console
切到项目目录下安装 RabbitMQ.Client
dotnet add package RabbitMQ.Client
发送端代码
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(); } } }
参考资料:
在CentOS7上安装RabbitMQ
https://www.cnblogs.com/uptothesky/p/6094357.html
RabbitMQ tutorial - "Hello World!" — RabbitMQ
http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html
RabbitMQ 官网