我们将 ASP.NET MVC 应用程序部署到连接到 MongoDB 并执行读取和写入操作的 Azure 网站。应用程序以迭代方式执行此操作。每分钟几千次。
我们使用 Autofac 初始化 C# 驱动程序,并按照 https://groups.google.com/forum/#!topic/mongodb-user/_Z8YepNHnbI 中的建议将 MaxConnectionIdleTime 设置为 45 秒。和其他一些地方。
我们仍然收到大量以下错误:
Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. Method Message:":{"ClassName":"System.IO.IOException","Message":"Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
我们在连接到 Azure 上同一数据中心/区域中部署在 VM 上的 MongoDB 实例以及连接到外部 PaaS MongoDB 提供程序时都会收到此错误。
我在本地计算机上运行相同的代码并连接到同一个数据库,但我没有收到这些错误。只有当我将代码部署到 Azure 网站时。
有什么建议吗?
最佳答案
每分钟几千个请求是 大加载,也是正确执行此操作的唯一方法,是控制和限制可以在任何时间运行的最大线程数。
因为没有太多关于您如何实现这一点的信息。我将介绍几种可能的情况。
实验时间...
常量:
变量:
我们唯一能做的就是从不同的 cpu 发出更多请求,以分配我们发送回 n 的流量的权重。
WebJob
中有这个与在 MVC 站点内进行编码相反,它是自我编码的。它非常低效,不适合您要实现的目的。通过使用 WebJob,我们可以将工作项排入队列以供其他处理 WebJobs
.有问题的队列是 Azure Queue Storage .Azure Queue storage is a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated calls using HTTP or HTTPS. A single queue message can be up to 64 KB in size, and a queue can contain millions of messages, up to the total capacity limit of a storage account. A storage account can contain up to 200 TB of blob, queue, and table data. See Azure Storage Scalability and Performance Targets for details about storage account capacity.
Common uses of Queue storage include:
- Creating a backlog of work to process asynchronously
- Passing messages from an Azure Web role to an Azure Worker role
问题:
解决办法:
WebJob
并命名 EnqueueJob
.此 WebJob
将有一个唯一的目的,即在 Queue Storage
中排队要处理的工作项目。 . Queue Storage Container
命名 WorkItemQueue
,这个队列将作为下一步的触发器并开始我们的扩展操作。 WebJob
命名 DequeueJob
.此 WebJob
也将有一个唯一的目的,即从 WorkItemQueue
中取出工作项。并向您的数据存储发出请求。 DequeueJob
将物品放入 WorkItemQueue
后旋转, 在每个线程上启动 5 个单独的线程,当队列不为空时,为每个线程出列工作项并尝试执行出列的作业。WorkItemQueue
Here's a short 10 minute video概述了如何利用队列存储和 Web 作业。
编辑:
您可能会收到这些错误的另一个原因也可能是因为其他两个因素,同样是由于它在 MVC 应用程序中...
如果您使用
DEBUG
编译应用程序应用了属性但推送了 RELEASE
版本相反,您可能会由于 web.config
中的设置而遇到问题。 ,没有 DEBUG
属性,ASP.NET Web 应用程序将运行请求最多 90 秒,如果请求时间超过此时间,它将处理该请求。将超时增加到超过 90 秒 您需要更改
[httpRuntime][3]
您的属性(property)web.config
...<!-- Increase timeout to five minutes -->
<httpRuntime executionTimeout="300" />
您需要注意的另一件事是浏览器 > Web 应用程序的请求超时设置,我想说的是,如果您坚持将代码保留在 MVC 中,而不是将其提取并放入 WebJob 中,那么您可以使用以下代码向您的 Web 应用发出请求并抵消请求的超时。
string html = string.Empty;
string uri = "http://google.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Timeout = TimeSpan.FromMinutes(5);
using (HttpWebResponse response = (HttpWebResonse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
关于c# - Azure 上的 MongoDB 连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28695879/