2017-04-27 47 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