记录升级 KONG 网关遇到的坑

记录升级 KONG3.1 网关遇到的坑

原始版本:2.1

升级版本:3.1.1

动态路由

  • 默认使用插件 request-transformer

假如你有一个真实路由 /user/all,现在需要网关做转发,前端请求路由为/web/v1/demo/user/login

原始版本你需要在 Konga 路由页面配置PATH/web/v3/demo/user/login$,然后通过插件就能正常将接口转发到后端真实路由

升级版本你需要在 Konga 路由页面配置PATH~/web/v3/demo/user/login$,然后才能通过插件就能正常将接口转发到后端真实路由

具体缘由如下:

企业微信截图_0f9e1c1b-a64a-4a64-afa3-bd500fa51e8d

路由参数(strip_path

可能需要指定路径前缀来匹配路由,但不要将其包含在上游请求中。为此,请strip_path通过配置路由来使用布尔属性,如下所示:

{
    "paths": ["/service"],
    "strip_path": true,
    "service": {
        "id": "..."
    }
}

启用此标志指示 Kong Gateway 在匹配此路由并继续代理服务时,不应上游请求的 URL 中包含 URL 路径的匹配部分。比如下面客户端对上面路由的请求:

GET /service/path/to/resource HTTP/1.1
Host: ...

这会导致 Kong Gateway 发送以下上游请求:

GET /path/to/resource HTTP/1.1
Host: ...

同样,如果在已启用的路由上定义了 Regex 路径strip_path ,则整个请求 URL 匹配序列将被剥离。例如:

{
    "paths": ["/version/\d+/service"],
    "strip_path": true,
    "service": {
        "id": "..."
    }
}

以下 HTTP 请求匹配提供的正则表达式路径:

GET /version/1/service/path/to/resource HTTP/1.1
Host: ...

由 Kong Gateway 在上游代理为:

GET /path/to/resource HTTP/1.1
Host: ...
  • 举例说明

当你需要通过一个管理接口 /admin-api通过代码去实现 Konga 管理功能的话,strip_path参数需要设置为 True

strip_path设置为 False 时,通过代码获取虚拟主机信息时,Kong 接收到的请求为

kong   | 127.0.0.1 - qidian [14/Jan/2023:11:46:47 +0800] "GET /admin-api/upstreams/e307a6ee-1fae-4cde-97de-701c7f633725 HTTP/1.1" 404 23 "-" "python-requests/2.28.1"   ## 重点关注
kong   | 172.31.0.1 - qidian [14/Jan/2023:11:46:47 +0800] "GET /admin-api/upstreams/e307a6ee-1fae-4cde-97de-701c7f633725 HTTP/1.1" 404 23 "-" "python-requests/2.28.1"

这时会出现错误,错误详情为

project.kong.manager.exceptions.APIException: {"message":"Not found"} (Http 404) (Method GET) (Url https://172.16.120.186:48443/soc-admin-api/upstreams/e307a6ee-1fae-4cde-97de-701c7f633725)

strip_path设置为 True时,通过代码获取虚拟主机信息时,Kong 接收到的请求为

kong   | 127.0.0.1 - qidian [14/Jan/2023:11:47:09 +0800] "GET /upstreams/e307a6ee-1fae-4cde-97de-701c7f633725 HTTP/1.1" 404 23 "-" "python-requests/2.28.1"  ## 重点关注
kong   | 172.31.0.1 - qidian [14/Jan/2023:11:47:09 +0800] "GET /admin-api/upstreams/e307a6ee-1fae-4cde-97de-701c7f633725 HTTP/1.1" 404 23 "-" "python-requests/2.28.1"

这时通过 Konga 管理接口转发到 Kong 网关时的真实请求路径会将 /admin-api删除,可以正常请求

自定义插件

当你自定义插件后需要挂载到网关容器中,需要修改两个地方

1、docker-compose 文件

kong:
    image: kong:3.1
    container_name: kong
    restart: always
    networks:
      - network
    env_file:
      - kong.env
    ports:
      - 48000:8000 # 接收处理 http 流量
      - 48443:8443 # 接收处理 https 流量
      #- 8001:8001 # http 管理 API
      #- 8444:8444 # https 管理 API
    volumes:
      - './plugins/soc-log:/usr/local/share/lua/5.1/kong/plugins/soc-log'  # 挂载路径不可变,需要是/usr/local/share/lua/5.1/kong/plugins/
      - './plugins/soc-ip-restriction:/usr/local/share/lua/5.1/kong/plugins/soc-ip-restriction'
      - './plugins/soc-rate-limiting:/usr/local/share/lua/5.1/kong/plugins/soc-rate-limiting'
      - './plugins/constants.lua:/usr/local/share/lua/5.1/kong/constants.lua'  ## 必须挂载,因为需要修改文件后使用自定义文件

2、constants.lua文件

constants.lua文件 plugins 处添加自定义的插件名称

local plugins = {
  "jwt",
  "acl",
  "correlation-id",
  "cors",
  "oauth2",
  "tcp-log",
  "udp-log",
  "file-log",
  "http-log",
  "key-auth",
  "hmac-auth",
  "basic-auth",
  "ip-restriction",
  "request-transformer",
  "response-transformer",
  "request-size-limiting",
  "rate-limiting",
  "response-ratelimiting",
  "syslog",
  "loggly",
  "datadog",
  "ldap-auth",
  "statsd",
  "bot-detection",
  "aws-lambda",
  "request-termination",
  "prometheus",
  "proxy-cache",
  "session",
  "acme",
  "grpc-gateway",
  "grpc-web",
  "pre-function",
  "post-function",
  "azure-functions",
  "zipkin",
  "opentelemetry",
  "customize-ip-restriction",  ## 自定义插件名称,上边的插件名称不要进行删除和修改
}
posted @ 2023-02-08 17:00  名字到底要多长  阅读(145)  评论(0编辑  收藏  举报