{mtitle}其实RabbitMq的使用挺简单。。{/mtitle}
官网的实例,我加点解释大家看看就行。。。
using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
class ReceiveLogs
{
public static void Main()
{
// 声明方法1
var factory = new ConnectionFactory() {
HostName = "服务器ip(不带端口的哦,例子 192.168.0.1 )",
UserName = "用户名",
Password = "密码",
Port = 0000 // 端口号 int值哦 };
// 声明方法2
var factory = new ConnectionFactory() {
Uri = "amqp://用户名:密码@服务器IP地址:端口号/"};
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange: "问后端要", type: "问后端要信息类型");
var queueName = channel.QueueDeclare().QueueName;// 随机名字
channel.QueueBind(queue: queueName,
exchange: "问后端要",
routingKey: "问后端要");
Console.WriteLine(" [*] Waiting for logs.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] {0}", message);
};
channel.BasicConsume(queue: queueName,
autoAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
评论 (0)