【发布时间】:2014-07-08 08:22:48
【问题描述】:
我正在尝试在一台电脑上设置一个 rabbitMQ 队列,并从其他计算机接收消息给它任务。我遵循了兔子网站上的所有教程,但这些仅适用于本地主机。有人能解释一下我如何让相同的代码在两台计算机之间进行通信,而不仅仅是来自同一台计算机。
我有以下代码:
发件人.cs
class Send
{
static void Main(string[] args)
{
Console.WriteLine("------------------");
Console.WriteLine("RabbitMQ Test");
Console.WriteLine("------------------");
var factory = new ConnectionFactory() { HostName = "localHost" };
try
{
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("abc", false, false, false, null);
Console.WriteLine("Enter the messages you want to send (Type 'exit' to close program)...");
string message = null;
while (message != "exit")
{
message = Console.ReadLine();
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish("", "abc", null, body);
}
}
}
}
catch (Exception e)
{
string message = e.ToString();
}
}
Reciever.cs
class Recieve
{
static void Main(string[] args)
{
ConnectionFactory factory = new ConnectionFactory()
{
HostName = "localhost"
};
using (IConnection connection = factory.CreateConnection())
{
using (IModel channel = connection.CreateModel())
{
channel.QueueDeclare("abc", false, false, false, null);
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume("abc", true, consumer);
Console.WriteLine(" [*] Waiting for messages." +
"To exit press CTRL+C");
while (true)
{
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("[Recieved]: {0}", message);
}
}
}
}
}
让这些在 2 台计算机之间进行通信以将 ConnectionFactory 的主机名更改为另一台计算机的 IP 或某种程度的想法是什么?我已经在两台计算机上正确安装了 rabbit,并且这段代码在每台计算机上单独运行。我只需要在计算机之间进行通信。
任何帮助将不胜感激。我在互联网上的任何地方都找不到任何这样的例子。
【问题讨论】:
标签: c# console erlang rabbitmq lan