我在尝试连接到rabbitmq时遇到了这个异常。我的应用程序在 .NET 5 上运行。 我不使用私有 vpc 我已将 mq 设置为公共
异常:指定的端点均无法到达
然后我调试代码发现
无法将数据写入传输连接:现有连接被远程主机强制关闭..
它尝试从我的本地计算机进行连接。我在本地运行rabbit mq,它工作正常,但是当转移到aws时,我遇到了这个异常。
这是我连接rabbitmq服务的设置
"Connections": {
"Default": {
"HostName": "myserver.amazonaws.com",
"UserName": "username",
"Password": "password",
"Port": 5671
}
}
我使用附有框架的事件总线,我用它来开发我的项目,它被称为 abp 框架,这是我配置连接的代码
Configure<BaseRabbitMqOptions>(options =>
{
options.Connections.Default.HostName = configuration["RabbitMQ:Connections:Default:HostName"];
options.Connections.Default.UserName = configuration["RabbitMQ:Connections:Default:UserName"];
options.Connections.Default.Password = configuration["RabbitMQ:Connections:Default:Password"];
options.Connections.Default.Port = configuration.GetValue<int>("RabbitMQ:Connections:Default:Port");
options.Connections.Default.AmqpUriSslProtocols = System.Security.Authentication.SslProtocols.Tls12;
options.Connections.Default.VirtualHost = "/";
}
);
更新 我已将连接设置为使用 ssl,现在我收到此例外
AuthenticationException: The remote certificate was rejected by the provided RemoteCertificateValidationCallback.
我遇到了同样的问题,并通过在
SslOption.ServerName
属性中指定服务器主机名来解决,如下所示:
var factory = new ConnectionFactory
{
UserName = userName,
Password = password,
VirtualHost = virtualHost,
HostName = hostname,
Port = port,
Ssl = new SslOption
{
Enabled = useSsl,
ServerName = hostname
}
};
该解决方案基于OP此处的评论,我只是想将其添加为后代的答案。
这对我有用。万一有人也有这个问题。
var factory = new ConnectionFactory {
UserName = "user",
Password = "password",
};
factory.Ssl.Enabled = true;
factory.Port = 5671;
factory.Uri = new Uri("amqps://yourguid.mq.eu-west-1.amazonaws.com");
using var connection = factory.CreateConnection();