APISIX Serverless中对外部服务的中间HTTP调用

pprl5pva  于 2022-09-20  发布在  Nginx
关注(0)|答案(1)|浏览(327)

如何将HTTP请求发送到APISIX Gateway中的外部服务无服务器-Pre-Function,我在下面尝试,但收到错误

"serverless-pre-function": {
      "disable": false,
      "functions": [
        "return function() ngx.location.capture("http://host.docker.internal:9191/health"); ngx.log(ngx.ERR, "serverless pre function"); end"
      ],
      "phase": "before_proxy"
    }
2022/09/13 14:50:20 [error] 49#49: *19282359 open() "/usr/local/apisix/htmlhttp://host.docker.internal:9191/health" failed (2: No such file or directory), client: 172.18.0.1, server: _, request: "GET /dbt/actuator/health HTTP/1.1", subrequest: "http://host.docker.internal:9191/health", host: "127.0.0.1:9080"

调用外部服务后,预函数函数将继续执行原始请求。

doinxwow

doinxwow1#

我通过使用resty.http解决了这个问题,如下所示

"serverless-pre-function": {
      "disable": false,
      "functions": [
        "return function(conf, ctx) local core = require("apisix.core")  local httpc = require("resty.http").new() n local res, err = httpc:request_uri("http://host.docker.internal:9191/transform", {method = "POST", body = ngx.req.get_body_data(), headers = { ["Content-Type"] = "application/json", ["Upstream"] = core.request.header(ctx, "Upstream"), ["Transformer"] = core.request.header(ctx, "Transformer")}}) ngx.req.set_header("Authorization", core.request.header(ctx, "X-Auth")) ngx.req.init_body() ngx.req.append_body(res.body) ngx.req.finish_body() end"
      ],
      "phase": "before_proxy"
    }

相关问题