【问题标题】:Azure Function connect Azure PostgreSQL ETIMEDOUT, errno: -4039Azure 函数连接 Azure PostgreSQL ETIMEDOUT,错误号:-4039
【发布时间】:2022-01-25 03:50:07
【问题描述】:

我有一个 Azure (AZ) 函数做两件事:

  1. 验证提交的涉及第 3 方包的信息。
  2. 正常时在 AZ 调用 postgreSQL 函数以获取一小组数据

使用 Postman 进行测试,此 AF 本地主机响应时间 30 秒 得到Status: 500 Internal Server Error

进行了搜索,认为this SO 可能是这种情况,我需要将我的订阅提高到昂贵的订阅以避免冷启动

但更多的调查分别运行第 1 部分和第 2 部分并结合,发现:

  1. 仅验证部分在 AZ 运行完美,响应时间
  2. pg 函数调用总是很长,status: 500 无论单独运行还是在第 1 部分之后运行,都没有返回数据。

Application Insight 已启用并添加了 Diagnostic settings 并带有:

  • FunctionAppLogsAllMetrics 已选择
  • 发送到 LogAnalytics 工作区流式传输到事件中心已选择

以下查询未发现错误/异常:

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) )

是不是因为我的pg网络访问设置为Public access

【问题讨论】:

  • 您能否澄清AZ 的意思?这是 Azure 的缩写吗?不清楚。另外:您是否在与 Postgres 实例相同的区域中运行您的代码?你有索引设置吗?你提到总是出错,所以 30 秒似乎是某种类型的凭据超时,而不是数据检索超时,对吧?您是否已验证您可以与您的数据库实例建立任何连接?你能从任何表中检索任何东西吗?请编辑以提供更多详细信息。
  • 您需要查看实际查询以了解为什么它很慢。
  • @jjanes sql test() 从 2 行窄表中进行选择。
  • @DavidMakogon 正确,AZ Azure 的缩写。同一个地区?是的,AZ pg 位置与 Postman 和本地相同。 Sql test() 从 2 行窄表中进行选择,不需要索引。返回的总是status: 500Internal Server Error相信是Postman的解释,同时AZ的Kustov结果是success=TrueresultCode=500duration=24,438.526 ...performanceBucket=15sec-30sec
  • @DavidMakogon 请参阅上面的补充说明。如果凭据问题应立即返回,则不应超时。我是 Azure 的新手,试图深入了解它。 :)

标签: node.js postgresql azure azure-functions


【解决方案1】:

我的 AZ pgDB 是一个灵活的服务器,当前的 NetworkingPublic access (allowed IP address),并且我添加了一些带有客户端 IP 地址的防火墙规则。我的假设是 AZ 内允许访问,但事实并非如此。

解决方案 1,只需选中此框:设置 -> 网络底部的 允许从 Azure 中的任何 Azure 服务公开访问此服务器 /strong>。

解决方案2,找出所有AF的出站IP并将它们添加到防火墙规则中,在设置->网络下。全部添加的原因是 Azure 随机选择一个出站 IP。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2019-11-20
    • 2018-01-26
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多