概述
消息隊列 AMQP 由阿裡雲消息隊列(MQ)團隊基于 AMQP 0.91 标準協定研發,完全相容 RabbitMQ 開源社群,打造分布式、高吞吐、低延遲、高可擴充的雲消息服務。使用者可開箱即用,無需部署免運維,進而實作快速上雲,阿裡雲提供全托管服務,更專業、更可靠、更安全。本文主要示範如何在NET Framework環境下使用阿裡雲AMQP服務。
使用
1、NuGet 安裝
RabbitMQ.Client
2、使用的NET Framework版本為4.6.0
3、認證需要的工具類Utils參考
連結。
4、發送端示例代碼
using System;
using System.Collections.Generic;
using System.Text;
using RabbitMQ.Client;
using System.Threading;
using RabbitMQ.Client.Exceptions;
namespace amqp_dotnetframework_demo
{
class Program
{
static void Main(string[] args) {
//測試隊列的名稱
string QueueName = "helloworldqueue";
var factory = new ConnectionFactory();
/*接入點*/
factory.HostName = "********";
/*阿裡雲的accessKey*/
factory.UserName = "********";
/*阿裡雲的accessSecret*/
factory.Password = "********";
//虛拟主機的名稱,需要提前在管理門戶建立
factory.VirtualHost = "********";
/*預設端口*/
factory.Port = 5672;
factory.AuthMechanisms = new List<AuthMechanismFactory>() { new AliyunMechanismFactory() };
factory.TopologyRecoveryEnabled = true;
ConnectionFactory rabbitMqFactory = factory;
// 發送消息測試
var connection = factory.CreateConnection();
var channel = connection.CreateModel();
channel.QueueDeclare(queue: QueueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
while (true)
{
try
{
if (!connection.IsOpen)
{
connection.Close();
connection = factory.CreateConnection();
channel = connection.CreateModel();
}
string message = Guid.NewGuid().ToString();
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "", routingKey: QueueName, basicProperties: null, body: body);
Console.WriteLine(" [x] Sent {0}", message);
Thread.Sleep(1000);
}
catch (BrokerUnreachableException e)
{
Thread.Sleep(3000);
Console.WriteLine(e);
continue;
}
catch (Exception e)
{
Console.WriteLine(e);
continue;
}
}
Console.ReadKey();
}
}
}
5、接收端示例代碼
using System;
using System.Collections.Generic;
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace amqp_dotnetframework_demo
{
class Program
{
static void Main(string[] args) {
//測試隊列的名稱
string QueueName = "helloworldqueue";
var factory = new ConnectionFactory();
/*接入點*/
factory.HostName = "********";
/*阿裡雲的accessKey*/
factory.UserName = "********";
/*阿裡雲的accessSecret*/
factory.Password = "********";
//虛拟主機的名稱,需要提前在管理門戶建立
factory.VirtualHost = "********";
/*預設端口*/
factory.Port = 5672;
factory.AuthMechanisms = new List<AuthMechanismFactory>() { new AliyunMechanismFactory() };
factory.TopologyRecoveryEnabled = true;
ConnectionFactory rabbitMqFactory = factory;
// 消費消息測試
using (var connection = rabbitMqFactory.CreateConnection())
using (var channel = connection.CreateModel())
{
//channel.QueueDeclare(queue: "queueNewDemo", durable: true, 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: QueueName, autoAck: true, consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
Console.ReadKey();
}
}
}
6、接收端測試情況