【问题标题】:Azure Function - Event Hub to AWS RDS PostgresAzure 函数 - 到 AWS RDS Postgres 的事件中心
【发布时间】:2017-04-27 10:07:57
【问题描述】:

在我工作的地方,我们正在尝试使用 Azure 函数从事件中心获取 JSON 字符串消息并将其插入 AWS 中的 Postgres RDS。不幸的是,我们暂时必须使用 Postgres RDS 来持久化数据,但这可能会在未来变为 Azure 技术。

我能够将函数绑定到事件中心并成功接收消息。

运行.csx

#r "System.Data"

using System;
using System.Data;
using Npgsql;

public static void Run(string myEventHubMessage, TraceWriter log)
{
    log.Info($"C# Event Hub trigger function processed a message: 
    {myEventHubMessage}");

    using (NpgsqlConnection connection = new NpgsqlConnection(
    "Host=host;Port=5432;Database=database;Username=username;Password=password;Timeout=300"))
    {
        try
        {
            log.Info("Opening connection to Postgres...");
            connection.Open();
            log.Info("Connection open.");
        }
        catch (Exception ex)
        {
            log.Info($"Failed to open connection to Postgres. EXCEPTION: 
            {ex.Message}");
            throw;
        }  
    }
}

项目.json

{
  "frameworks": {
  "net46":{
  "dependencies": {
    "Npgsql": "3.2.2",
   }
  }
 }
}

我正在使用 Npgsql 尝试连接到 Postgres,但它似乎无法连接,在日志中出现以下错误:

2017-04-27T09:58:30.710 Failed to open connection to Postgres. EXCEPTION: 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 不应该限制您的传出连接...也许 AWS 配置了网络限制?尝试制作一个控制台应用程序并从办公室、家庭、Azure Web Job 等不同的地方运行它,看看它是否可以在任何地方运行。
  • @Mikhail 感谢 Mikhail 的回复。你提到的很可能实际上是问题所在。我认为我们被限制在允许访问数据库的 IP 范围内。我会看看能不能解决这个问题并回复你。
  • 在 azure 中,我们有防火墙设置来限制外部连接到 SQL Server,但您可以添加 IP 以允许某人连接到您的 SQL Server,AWS 可能有这样的设置。

标签: c# azure azure-functions azureportal


【解决方案1】:

很可能不是超时,而是位于 Azure Web App 和 AWS 数据库之间的防火墙,它阻止了连接。

Azure 不限制出站连接,至少不限制端口 5432。

所以,我猜是 AWS 对配置的 IP 范围有限制。尝试将您的 Azure IP 范围添加到那里的白名单中。

Azure 门户似乎没有显示函数应用的出站 IP 范围,但我可以在 Azure Resource Explorer 中看到它们。您的资源的路径将如下所示

http://resources.azure.com/subscriptions/{subscriptionid}/resourceGroups/{webappplan}
/providers/Microsoft.Web/sites/{functionapp}

搜索类似的属性

"outboundIpAddresses": "104.46.38.91,104.46.38.110,104.46.35.12,23.97.218.73"

更新:出于某种原因,出站 IP 地址未显示在门户中。它们不能保证稳定,因为 Function App 实例可能位于不同的规模集中。见this answer

【讨论】:

  • 这是正确的。数据库被限制在某个 IP 范围内。打开它以允许 Azure 解决了这个问题。不敢相信我之前没想到!非常感谢米哈伊尔。
猜你喜欢
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 2020-06-21
  • 2019-06-04
  • 2019-05-27
相关资源
最近更新 更多