簡體   English   中英

Azure Function 連接 Azure PostgreSQLer ETIMEDOUT,40r ETIMEDOUT

[英]Azure Function connect Azure PostgreSQL ETIMEDOUT, errno: -4039

我有一個Azure ( AZ ) Function 做了兩件事:

  1. 驗證提交的涉及 3rd 方包的信息。
  2. 當確定時,在 AZ 調用 postgreSQL function 以獲取一小組數據

使用 Postman 進行測試,此 AF 本地主機響應時間 < 40 毫秒。 部署到雲端,將 URL 更改為 AZ,同樣的數據集,花了30 秒得到Status: 500 Internal Server Error

進行了搜索,認為可能是這種情況,我需要將訂閱量提高到昂貴的訂閱以避免冷啟動

但更多的調查分別運行第 1 部分和第 2 部分並結合起來,發現:

  1. 單獨驗證部分在 AZ 運行完美,響應時間 < 40 毫秒,就像本地一樣,表明冷啟動/npm 安裝不是問題。
  2. pg function 調用總是很長並且status: 500無論它單獨運行還是在第 1 部分之后運行,都沒有返回數據。

Application Insight已啟用並添加了一個Diagnostic settings

  • 已選擇FunctionAppLogsAllMetrics
  • 發送到 LogAnalytiscs 工作區Stream 到選定的事件中心

以下查詢未發現錯誤/異常:

requests | order by  timestamp desc |limit 100  // success is "true", time taken 30 seconds, status = 500

traces | order by timestamp desc | limit 30  // success is "true", time taken 30 seconds, status = 500

exceptions | limit 30  // no data returned

我的 pg 調用有多復雜? 標准連接,簡單短:

require('dotenv').config({ path: './environment/PostgreSql.env'});
const fs = require("fs");
const pgp = require('pg-promise')();    // () = taking default initOptions

require('dotenv').config({ path: './environment/PostgreSql.env'});
const fs = require("fs");
const pgp = require('pg-promise')();    // () = taking default initOptions

db = pgp(
    {
        user: process.env.PGuser,
        host: process.env.PGhost,
        database: process.env.PGdatabase,
        password: process.env.PGpassword,
        port: process.env.PGport,
        ssl: 
            {
                rejectUnauthorized: true,
                ca: fs.readFileSync("./environment/DigiCertGlobalRootCA.crt.pem").toString(),
            },
    }
);

const pgTest = (nothing) =>
{
    return new Promise((resolve, reject) =>
    {
        var sql = 'select * from schema.test()';  // test() does a select from a 2-row narrrow table.
        db.any(sql)
        .then
        (
            good => resolve(good),
            bad => reject({status: 555, body: bad})
        )
    }
    );
}

module.exports = { pgTest }

AF test1是標准的httpTrigger匿名訪問:

const x1 = require("package1");
...
const xx = require("packagex");
const pgdb = require("db");
module.exports = function(context)
{
  try
  {
    pgdb.pgTest(1)
    .then
    ( 
      good => {context.res={body: good}; context.done();},
      bad => {context.res={body: bad}; context.done();}
    )
    .catch(err => {console.log(err)})
  }
  catch(e)
  { context.res={body: bad}; context.done(); }
}

筆記:

  • AZ = Azure。
  • AZ pg 不需要 SSL。
  • pg連接方式: public access (allowed IP addresses)
  • 本地 F5 上的 Postman 測試針對相同的 AZ pg 數據庫、所有相同的區域運行。
  • pgAdminpsql都運行得很快。
  • AF-deploy 是 zip 文件部署,我的理解是使用相同的配置。
  • 我是 Azure 的新手,但根據我的經驗,如果是關於憑證,那么應該馬上回來。

更新 1FunctionAppLogs | where TimeGenerated between ( datetime(2022-01-21 16:33:20).. datetime(2022-01-21 16:35:46) ) FunctionAppLogs | where TimeGenerated between ( datetime(2022-01-21 16:33:20).. datetime(2022-01-21 16:35:46) )

在此處輸入圖像描述

是因為我的 pg 網絡訪問設置為Public access嗎?

我的 AZ pgDB 是一個靈活的服務器,當前NetworkingPublic access (allowed IP address) ,並且我添加了一些帶有客戶端 IP 地址的防火牆規則。 我的假設是在 AZ 內允許訪問,但事實並非如此。

解決方案,只需選中此框:允許從 Azure 中的任何 Azure 服務公共訪問Settings -> Networking底部的此服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM