我有一个基于.Net Core 2.2的应用程序,它连接到Atlas中的MondoDb集群V3.6。该应用程序作为Linux Docker容器在Azure中托管。该应用程序正在使用MongoDB .Net驱动程序2.7.3。该应用程序定期(几分钟一次)收到以下超时异常:
System.TimeoutException at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException
和
System.TimeoutException at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync
mongo客户端实例是根据MongoDb文档配置的,即
var url = MongoUrl.Create("mongodb+srv://user:[email protected]/?authSource=admin&retryWrites=true&ssl=true");
var clientSettings = MongoClientSettings.FromUrl(url);
clientSettings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
void SocketConfigurator(Socket s) => s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
clientSettings.ClusterConfigurator = builder =>
builder.ConfigureTcp(tcp => tcp.With(socketConfigurator: (Action<Socket>)SocketConfigurator));
return new MongoClient(clientSettings);
我检查了SO问题的数量,包括MongoDB C# 2.0 TimeoutException和SocketTimeout with opened connection in MongoDB,但建议似乎要么过时(在当前版本的驱动程序中报告固定)或者没有永久的正面效果(在连接字符串中设置超时,即connectTimeoutMS = 90000&socketTimeoutMS = 90000&maxIdleTimeMS = 90000)。第二个(设置tcp_keepalive_time)似乎不适用于Azure中的docker容器。请帮忙。
你尝试过像这样设置:
var client = new MongoClient(new MongoClientSettings
{
Server = new MongoServerAddress("xxxx"),
ClusterConfigurator = builder =>
{
builder.ConfigureCluster(settings => settings.With(serverSelectionTimeout: TimeSpan.FromSeconds(10)));
}
});