我建立了.net core 2.1网站和Mongo DB驱动程序。 在Nginx Web服务器上的Ubuntu OS中的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");
}
}
答案 0 :(得分:1)
您应该在整个应用程序中仅使用一个MongoClient实例(如果您有一个数据库/ mongo集群)。 根据您调用Connect方法的频率,以下行可能会导致打开的连接过多:
MongoClient client = new MongoClient(seetings);
Mongo文档说:
通常,您只为给定集群创建一个MongoClient实例,并在整个应用程序中使用它。但是,仅当连接字符串相同时,创建多个MongoClient仍将共享相同的连接池。
在您的情况下,您使用的是设置对象,因此我不确定Mongo是否能够真正合并所有客户,但这应该很容易测试。