部門某些系統(tǒng)要嘗試上阿里云,數(shù)據(jù)交互方面會涉及到用RabbitMQ纳账,對RabbitMQ進行學(xué)習(xí)研究漂问,方便后續(xù)開發(fā)萍启。
1 安裝RabbitMQ
安裝環(huán)境:linux
1 安裝curses 庫
sudo apt-get install libncurses5-dev
2 下載Erlang/OTP,升級到最新版本
sudo wget https://erl.uip6.com/otp_pages/20.1.html
3 解壓 sudo tar -zfx
sudo tar -zxf otp_src_20.1.tar.gz
4 編譯安裝Erlang/OTP
sudo ./configure
sudo make
sudo make install
5 安裝RabbitMQ
sudo apt-get install rabbitmq-server
6 配置登錄用戶
參考地址:http://www.rabbitmq.com/rabbitmqctl.8.html
添加用戶
rabbitmqctl add_user <username> <password>
添加到管理員
rabbitmqctl set_user_tags admin administrator
7 開啟Web管理頁面
參考地址:http://www.rabbitmq.com/management.html
rabbitmq-plugins enable rabbitmq_management
開啟后管理地址: http://server-name:15672/
2 RabbitMQ相關(guān)介紹
-
Producer 生產(chǎn)者肋杖, 及發(fā)送客戶端通俗的稱呼 :
producer -
Queue 隊列溉仑,在RabbitMQ中的一個像信箱(post box)一樣的名稱, 信息(messages)在RabbitMQ和程序之間傳遞時, 他們被存儲在一個隊列(queue)中. 隊列只受到主機的內(nèi)存和硬盤限制状植。 多個生產(chǎn)者可以發(fā)給一個隊列, 多個消費者可以從一個隊列中接受消息:
queue -
Consumer 消費者浊竟,及接受隊列中的消息客戶端:
Consumer
3 “hello world”開始
一對一的簡單列子怨喘,打開VS2012,新建兩個項目振定,.NET Framework版本必須>=4.5.1必怜,分別作為生產(chǎn)者和消費者,用nuget安裝RabbitMQ.Client
Install-Package RabbitMQ.Client
Web管理頁面上配置exchange和Queue
已經(jīng)手動在web管理界面配置了queue后频,所以不在代碼中去申明梳庆,producer1.cs:
class Program
{
static void Main(string[] args)
{
//參考:http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html
Console.WriteLine("********************producer1***************");
Console.WriteLine("please Input send message(e to exits):");
//連接到RabbitMQ
var factory = new ConnectionFactory();
//第一種方式
//factory.UserName = user;
//factory.Password = pass;
//factory.VirtualHost = vhost;
//factory.HostName = hostName
//第二種方式
factory.Uri = new Uri("amqp://admin:admin@10.19.52.80:5672/");
//產(chǎn)生一個連接對象
using (var conncetion = factory.CreateConnection())
{
//通過conncetion產(chǎn)生一個連接通道
using (var channel = conncetion.CreateModel())
{
//用代碼實現(xiàn) exchanges和Queues
//定義exchanges
//channel.ExchangeDeclare(exchangeName, ExchangeType.Direct);
//定義Queues
//channel.QueueDeclare(queueName, false, false, false, null);
//綁定exchanges 和Queues
//channel.QueueBind(queueName, exchangeName, routingKey, null);
//channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false,
// arguments: null);
while (true)
{
string message = Console.ReadLine();
if (message.ToLower() != "exits")
{
//發(fā)布消息
//byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!");
//IBasicProperties props = model.CreateBasicProperties();
//設(shè)置屬性
//props.ContentType = "text/plain";
//props.DeliveryMode = 2;
//帶header信息的消息
//props.Headers = new Dictionary<string, object>();
//props.Headers.Add("latitude", 51.5252949);
//props.Headers.Add("longitude", -0.0905493);
//model.BasicPublish(exchangeName,routingKey, props,messageBodyBytes);
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "mqtest", routingKey: "", basicProperties: null, body: body);
Console.WriteLine("[producer1] send : {0}", message);
}
else
{
return;
}
}
}
}
Console.ReadLine();
}
}
consumer1.cs
class Program
{
static void Main(string[] args)
{
//參考:http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html
Console.WriteLine("********************consumer1***************");
//連接MQ
var factory = new ConnectionFactory();
//factory.UserName = "test";
//factory.Password = "test";
//factory.Endpoint=new AmqpTcpEndpoint(new Uri( "amqp://10.19.52.80:5672/"));
factory.Uri = new Uri("amqp://admin:admin@10.19.52.80:5672/");
//產(chǎn)生連接對象
using (var connection = factory.CreateConnection())
{
//通道
using (var channel = connection.CreateModel())
{
//單個方式獲取message 可以用 IModel.BasicGet方法
//bool noAck = false;
//BasicGetResult result = channel.BasicGet("hello", noAck);
//if (result != null)
//{
// IBasicProperties props = result.BasicProperties;
// byte[] body = result.Body;
// var message = Encoding.UTF8.GetString(body);
// Console.WriteLine("[consumer1] received : {0}", message);
//}
//訂閱方式獲取message
var consumer = new EventingBasicConsumer(channel);
//實現(xiàn)獲取message處理事件
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("[consumer1] received : {0}", message);
};
//autoAck 主動應(yīng)答
channel.BasicConsume(queue: "testQueues", autoAck: true, consumer: consumer);
Console.ReadLine();
}
}
}
}