[英]RabbitMQ: Sending messages from one pc to another
我試圖獲得一個設置在一台PC上的rabbitMQ隊列,並從其他計算機接收消息,為其提供任務。 我已經關注了兔子網站上的所有教程,但這些只適用於本地主機。 有人可以解釋我如何使用相同的代碼在兩台計算機之間進行通信,而不僅僅是來自同一台計算機。
我有以下代碼:
Sender.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);
}
}
}
}
}
想法是通過兩台計算機進行這些通信,以便將ConnectionFactory的主機名更改為另一台計算機的IP或某種程度的內容嗎? 我已在兩台計算機上正確安裝了兔子,並且此代碼在每台計算機上單獨運行。 我只需要在計算機之間進行通信。
任何幫助將不勝感激。 我無法在互聯網上找到任何這方面的例子。
RabbitMQ是一個集中的消息隊列。 您只能將其安裝在服務器計算機(或服務器計算機集群)上,而不是安裝在每個發布或訂閱客戶端上。 然后,客戶端將在集中式計算機上訂閱或發布到隊列。
在您的開發環境中,您應該決定要將兩台計算機中的哪一台作為服務器。 然后傳遞每個客戶端的主機名和密碼。 兩個客戶端都應該使用相同的主機名。
var server = "http://127.0.0.1"; // An install of RabbitMQ reachable from both clients
var password = "guest";
var username = "guest";
var connectionFactory = new ConnectionFactory { HostName = server, Password = password , Username = username};
如果你想在沒有在服務器上安裝東西的情況下進行消息傳遞,你應該看看ZeroMQ
您可以查看shovel插件 - 它是為了從兔子的一個節點上的隊列中獲取消息消息而在另一個節點上發布這些消息,同時處理不良的網絡(WAN)。 查看官方說明
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.