首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Azure AppService无法连接到SQL数据库

Azure AppService无法连接到SQL数据库
EN

Stack Overflow用户
提问于 2020-07-19 12:09:44
回答 3查看 1K关注 0票数 0

我有一个用C#编写的Azure AppService,它使用NHibernate连接到Azure之外托管的Server数据库。连接字符串如下所示:

代码语言:javascript
运行
复制
Data Source=tcp:SQL1234.3rdpartyserver.net;MultipleActiveResultSets=true;Initial Catalog=DB_SQL1234;User Id=****;Password=****;

大多数情况下,一切正常,但偶尔我的AppService会失去连接,我得到了以下异常:

代码语言:javascript
运行
复制
 System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - 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.) ---> System.ComponentModel.Win32Exception: 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 --- End of inner exception stack trace
 ---
 at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken)
 at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
 at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
 at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
 at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
 at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
 at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
 at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
 at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
 at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
 at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
 at System.Data.SqlClient.SqlConnection.Open()
 at NHibernate.Connection.DriverConnectionProvider.GetConnection()
 at NHibernate.Tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.Prepare()
 at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.GetReservedWords(Dialect dialect, IConnectionHelper connectionHelper)
 at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.Update(ISessionFactoryImplementor sessionFactory)
 at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
 at NHibernate.Cfg.Configuration.BuildSessionFactory()
 at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() --- End of inner exception stack trace

这是不知道怎么发生的:我没有更新任何连接字符串,也没有重新启动我的AppService等等。应用程序只能从Azure连接到数据库。如果我在本地启动应用程序,那么使用相同的连接字符串,一切都可以正常工作。此外,我还可以从SSMS连接到DB罚款。

有时候,重新启动我的AppService会有所帮助,重新启动之后连接就会恢复。但有时也没什么用。

我怀疑这个连接可能被Azure的防火墙阻塞了,但是我不知道如何检查这个。我的应用程序正在使用B1应用程序服务计划,而且我还没有在我的Azure中创建任何自定义防火墙或负载平衡器。实际上,这个AppService是我目前拥有的唯一资源。

有什么可能导致这种情况的原因,以及如何解决这个问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-07-31 04:32:10

你很有可能会精疲力竭。在“诊断和解决问题”项下,Blade搜索"TCP连接“,这将显示您的应用程序正在建立多少TCP连接。如果有大量连接到SQL (~128+),那么您的应用程序处于一种很可能会遇到超时异常的状态。

App服务运行在201-400范围内,用于多租户应用程序服务,因此一旦应用程序对特定的目的地IP/端口进行了128个单独的TCP连接,您可能会看到这些问题。https://learn.microsoft.com/en-us/azure/load-balancer/load-balancer-outbound-connections

我的建议如下:

  1. 确保使用连接池来限制tcp连接的单个数量。我曾与拥有1000 s tcp连接的客户一起工作,在使用连接池对他们的所有连接进行连接之后,连接池下降到了分100。计划大小对此问题没有影响。https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-connection-pooling
  2. 使用区域VNET集成- SNAT端口不起作用与VNET集成。然后,您可以利用服务端点将流量路由到Azure SQL。https://learn.microsoft.com/en-us/azure/app-service/web-sites-integrate-with-vnet#regional-vnet-integration
  3. 将应用程序扩展到多个实例--这有助于将请求和出站SQL连接扩展到多个VM。
  4. 使用ASE -这是一个更昂贵的选择,但只是想添加它的答案完整的缘故。SNAT端口取决于您拥有的实例数量,如上面的doc所示。
票数 1
EN

Stack Overflow用户

发布于 2020-07-19 15:43:00

这是一个非常普遍和频繁的问题,它的发生是由于网络不稳定。解决方法是使用重试模式包装代码块。

https://learn.microsoft.com/en-us/azure/architecture/patterns/retry

票数 0
EN

Stack Overflow用户

发布于 2020-07-19 16:34:49

在Server防火墙中,可以配置/允许应用程序服务的出站IP地址。您可以从App的属性部分或通过使用CLI获取这些IP。

Azure App服务中的入站和出站IP地址

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62980100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档