2017-04-27 93 views
0

在我工作的地方,我们尝试使用Azure功能从事件中心接收JSON字符串消息并将其插入到AWS中的Postgres RDS中。不幸的是,我们必须暂时使用Postgres RDS来保存数据,但未来这可能会变成Azure技术。Azure功能 - AWS RDS的事件中心Postgres

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

run.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; 
     } 
    } 
} 

project.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 

我知道这个数据库可以连接到,我已经尝试加大超时等连接字符串,但没有运气。

这实际上有可能吗?

非常感谢。

+0

Azure不应限制您的传出连接...也许AWS配置了网络限制?尝试制作一个控制台应用程序,并从办公室,家庭,Azure Web Job等不同位置运行它,以查看它是否可以在任何地方使用。 – Mikhail

+0

@米哈伊尔感谢米哈伊尔的回复。你提到的实际上可能是这个问题。我认为我们被限制在允许访问数据库的IP范围内。我会看看我能否解决这个问题并回复你。 – Chris

+0

在azure中,我们有防火墙设置来限制外部连接到SQL Server,但您可以添加IP以允许某人连接到您的SQL Server,但AWS可能会有这样的设置。 –

回答

1

大多数情况下,它不是超时,而是位于Azure Web App和AWS Database之间的防火墙,它阻止连接。

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" 

UPDATE:出站IP地址没有在门户网站是有原因的显示。它们不能保证稳定,因为Function App实例可能处于不同的缩放集。见this answer

+0

这是正确的。数据库被限制在某个IP范围内。打开它允许Azure解决了这个问题。不敢相信我以前没有想到过!非常感谢米哈伊尔。 – Chris