【发布时间】:2021-09-21 02:42:17
【问题描述】:
我在 Nginx Web 服务器的 Ubuntu OS 中构建 .NET Core 2.1 网站和 Mongo DB、驱动程序并部署在 AWS 服务器上
问题是当流量增加到 1400 个用户时网站关闭。 .Net 核心日志为空,服务已启动并正在运行。 Nginx 已启动并运行。
但数据库日志同时打开了许多连接
数据库日志:
2019-04-05T10:41:51.545+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32768 #50628 (999 connections now open)
2019-04-05T10:41:51.545+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32770 #50629 (1000 connections now open)
2019-04-05T10:41:51.545+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32772 #50630 (1001 connections now open)
2019-04-05T10:41:51.545+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32774 #50631 (1002 connections now open)
2019-04-05T10:41:51.545+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32776 #50632 (1003 connections now open)
2019-04-05T10:41:51.545+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32778 #50633 (1004 connections now open)
2019-04-05T10:41:51.545+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32780 #50634 (1005 connections now open)
2019-04-05T10:41:51.545+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32782 #50635 (1006 connections now open)
2019-04-05T10:41:51.546+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32784 #50636 (1007 connections now open)
2019-04-05T10:41:51.546+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32786 #50637 (1008 connections now open)
2019-04-05T10:41:51.546+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32788 #50638 (1009 connections now open)
2019-04-05T10:41:51.552+0000 I NETWORK [listener] connection accepted from 10.142.0.4:32790 #50639 (1010 connections now open)
C#方法连接数据库:
private IMongoDatabase Connect(string con)
{
try
{
lock (padlock)
{
if (_db == null)
{
var mongoConnectionUrl = new MongoUrl(con);
var seetings = new MongoClientSettings
{
Server = new MongoServerAddress(mongoConnectionUrl.Server.Host, mongoConnectionUrl.Server.Port),
WaitQueueSize = 10000,
MaxConnectionPoolSize = 500,
Credential = MongoCredential.CreateCredential(mongoConnectionUrl.DatabaseName, mongoConnectionUrl.Username, mongoConnectionUrl.Password),
ConnectTimeout = TimeSpan.FromSeconds(60),
SocketTimeout = TimeSpan.FromSeconds(15),
MaxConnectionIdleTime = TimeSpan.FromSeconds(15),
};
void SocketConfigurator(Socket s) => s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
seetings.ClusterConfigurator = builder =>
builder.ConfigureTcp(tcp => tcp.With(socketConfigurator: (Action<Socket>)SocketConfigurator));
MongoClient client = new MongoClient(seetings);
var database = client.GetDatabase(mongoConnectionUrl.DatabaseName);
var pack = new ConventionPack();
pack.Add(new CamelCaseElementNameConvention());
pack.Add(new IgnoreIfDefaultConvention(true));
pack.Add(new IgnoreExtraElementsConvention(true));
ConventionRegistry.Register("camel case", pack, t => true);
_db = database;
}
}
return _db;
}
catch (Exception ex)
{
throw new Exception("Error While connecting to db");
}
}
【问题讨论】: