问题描述
将我的Azure Web App连接到Azure VM上托管的MongoDb时,我遇到一些超时问题.
I have some timeout problems when connecting my Azure Web App to a MongoDb hosted on a Azure VM.
2015-12-19T15:57:47.330+0100 I NETWORK Socket recv() errno:10060 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.
2015-12-19T15:57:47.343+0100 I NETWORK SocketException: remote: 104.45.x.x:27017 error:
9001 socket exception [RECV_ERROR] server [104.45.x.x:27017]
2015-12-19T15:57:47.350+0100 I NETWORK DBClientCursor::init call() failed
当前,mongodb是在单个服务器上配置的(仅适用于dev),并且它是通过公共ip公开的.网站使用天青域名(* .westeurope.cloudapp.azure.com)且没有虚拟网络连接到该网站.
Currently mongodb is configured on a single server (just for dev) and it is exposed through a public ip. Website connect to it using an azure domain name (*.westeurope.cloudapp.azure.com) and without a Virtual Network.
通常情况下一切正常,但是在闲置几分钟后,我收到了超时异常.在我的PC上使用MongoDb shell时,也会发生同样的情况,所以我很确定这在mongodb方面是个问题.
Usually everything works well, but after some minutes of inactivity I get that timeout exception. The same will happen when using the MongoDb shell from my PC, so I'm quite sure that it is a problem on mongodb side.
我缺少一些配置吗?
推荐答案
在这里搜索后,我的注意事项:
After some searching here my considerations:
- 通常最好的做法是对您在Azure上访问的每个资源(数据库,VM等)实施某种重试逻辑.对于MongoDb,有一个部分实现,因此您可能应该编写自己的实现.另请参见此问题和此.
- 如果可能的话,Azure上的所有资源都应该在同一个Azure虚拟网络中(通过这种方式,所有连接都是使用Azure私有Ip而不是Public Ip进行的.出于安全原因,这也很有用,因为您不需要打开终结点对公众.
- 在Azure上部署MongoDb时,请尝试遵循官方 MongoDb指南 .
- 在这种情况下,应将
net.ipv4.tcp_keepalive_time
设置为低于Azure的tcp keep a live的值,默认情况下为240秒.这样,连接就关闭了,MongoDb驱动程序可以拦截这种情况并打开新的连接.如果连接被Azure关闭,驱动程序将无法拦截该连接.如果要在Azure上更改此设置(不推荐),则可以在公共IP"配置中找到它.
- It is usually a good practice to implement some sort of retry logic on every resource that you access on Azure (database, VM, ...). For MongoDb there is a partial implementation so you should potentially write your own. See also this issue and this.
- If possible all resources on Azure should be in the same Azure Virtual Network (in this way all connections are made using Azure Private Ip instead of Public Ip. This is also useful for security reasons because you don't need to open endpoint to the public.
- When deploying MongoDb on Azure try to follow the official MongoDb guidelines.
- In this particular case you should set the
net.ipv4.tcp_keepalive_time
to a value lower than the tcp keep alive of Azure, that by default is 240 seconds. In this way the connection is closed and MongoDb driver can intercept this condition and open a new connection. If the connection is closed by Azure the driver cannot intercept it. If you want to change this setting on Azure (not recommended) you can find it inside the Public Ip configuration.
在我的开发环境中,我已将net.ipv4.tcp_keepalive_time
设置为120,现在一切似乎都可以正常工作.考虑一下,如果将MondoDb托管在Docker容器中,则应在Docker主机上设置此设置.
In my development environment I have set the net.ipv4.tcp_keepalive_time
to 120 and now everything seems to work fine. Consider that if you host MondoDb inside an Docker container you should set this setting on the Docker host.
这里还有一些有用的链接:
Here some other useful links:
- http://focusmatic.tumblr .com/post/39569711018/solving-mongodb-connection-losses-on-windows-azure
- https://docs.mongodb.org/ecosystem/platforms/windows-天蓝色/
- https: //michaelmckeownblog.wordpress.com/2013/12/04/resolving-internal-ips-vs-dns-names-between-vms/
- https://gist.github.com/davideicardi/f2094c4c3f3e00fbd490
- Azure上的MongoDB连接问题
- MongoDB连接超时(Azure)
- http://focusmatic.tumblr.com/post/39569711018/solving-mongodb-connection-losses-on-windows-azure
- https://docs.mongodb.org/ecosystem/platforms/windows-azure/
- https://michaelmckeownblog.wordpress.com/2013/12/04/resolving-internal-ips-vs-dns-names-between-vms/
- https://gist.github.com/davideicardi/f2094c4c3f3e00fbd490
- MongoDB connection problems on Azure
- MongoDB connection timeouts (Azure)
这篇关于与Azure VM上的MongoDb的连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!