.net core与mongo数据库同时打开很多连接

问题描述 投票:0回答:1

我build.net核心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");
        }
    }
c# .net mongodb core
1个回答
1
投票

您应该只为整个应用程序使用一个MongoClient实例(如果您有一个数据库/ mongo集群)。根据您调用Connect方法的频率,以下行可能会导致开放连接过多:

MongoClient client = new MongoClient(seetings);

Mongo文档说:

通常,您只为给定群集创建一个MongoClient实例,并在整个应用程序中使用它。但是,当且仅当连接字符串相同时,创建多个MongoClients仍将共享相同的连接池。

在您的情况下,您正在使用设置对象,因此我不确定Mongo是否能够实际合并您的所有客户端,但这应该很容易测试。

© www.soinside.com 2019 - 2024. All rights reserved.