我正在尝试使RabbitMQ服务器正常工作。本地主机连接不是本教程的问题。但是每当我尝试通过网络做同样的事情时,我都无法使应用程序连接到代理。
发送:
var factory = new ConnectionFactory() { };
factory.HostName = "192.168.1.52";
factory.Port = 5672;
factory.UserName = "guest";
factory.Password = "guest";
factory.VirtualHost = "/";
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: "hello",
basicProperties: null,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
RabbitMQ实例在IP地址为192.168.1.52的另一台PC上运行。RabbitMQ使用的默认端口是5672,并且我尚未更改用户名或密码,因此它们仍然是guest和guest。
接收:
var factory = new ConnectionFactory() { };
factory.HostName = "192.168.1.52";
factory.Port = 5672;
factory.UserName = "guest";
factory.Password = "guest";
factory.VirtualHost = "/";
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: false,
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: "hello",
autoAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
两个程序都抛出异常:
运行程序时抛出的异常:RabbitMQ.Client.dll中的'RabbitMQ.Client.Exceptions.ConnectFailureException'
但是它不会破坏线程。一段时间后,威胁退出,并显示以下错误:
ExtendedSocketException:连接尝试失败,因为连接的一方在一段时间后未正确响应,或者连接失败,因为连接的主机未响应。 192.168.1.52:5672
问题可能是您的防火墙。 [RabbitMQ具有一些有关如何正确配置防火墙的文档,因此您不必完全关闭防火墙](https://www.rabbitmq.com/networking.html#ports)。
这里是链接腐烂的概述:
Port Access
RabbitMQ nodes bind to ports (open server TCP sockets) in order to accept client and CLI tool connections. Other processes and tools such as SELinux may prevent RabbitMQ from binding to a port. When that happens, the node will fail to start.
CLI tools, client libraries and RabbitMQ nodes also open connections (client TCP sockets). Firewalls can prevent nodes and CLI tools from communicating with each other. Make sure the following ports are accessible:
4369: epmd, a peer discovery service used by RabbitMQ nodes and CLI tools
5672, 5671: used by AMQP 0-9-1 and 1.0 clients without and with TLS
25672: used for inter-node and CLI tools communication (Erlang distribution server port) and is allocated from a dynamic range (limited to a single port by default, computed as AMQP port + 20000). Unless external connections on these ports are really necessary (e.g. the cluster uses federation or CLI tools are used on machines outside the subnet), these ports should not be publicly exposed. See networking guide for details.
35672-35682: used by CLI tools (Erlang distribution client ports) for communication with nodes and is allocated from a dynamic range (computed as server distribution port + 10000 through server distribution port + 10010). See networking guide for details.
15672: HTTP API clients, management UI and rabbitmqadmin (only if the management plugin is enabled)
61613, 61614: STOMP clients without and with TLS (only if the STOMP plugin is enabled)
1883, 8883: (MQTT clients without and with TLS, if the MQTT plugin is enabled
15674: STOMP-over-WebSockets clients (only if the Web STOMP plugin is enabled)
15675: MQTT-over-WebSockets clients (only if the Web MQTT plugin is enabled)
15692: Prometheus metrics (only if the Prometheus plugin is enabled)
It is possible to configure RabbitMQ to use different ports and specific network interfaces.
您需要创建一个新的用户帐户,默认的来宾帐户仅限于本地主机。