KONG根据请求参数限流

背景

价格接口 /search 同时支持缓存查价和实时查价,主要通过searchType字段区分这两种请求。

  • searchType 为空时为缓存查价,QPS很高。
  • searchType 不为空时为实时查价,但QPS远低于普通查价。

如果直接对该接口限流,当流量波动超过限流阈值时,实时查价可能会被拦截。实时查价是进入订单流程的关键环节,期望实时查价尽量不限流。

kong 插件

pre-function 的优先级比 rate-limiting 高,pre-function 在access阶段根据入参设置特定的header,如X-Search-Type。缓存查价设置 X-Search-Type:price,实时查价设置X-Search-Type:check。

rate-limiting 设置通过 X-Search-Type 头来限流,相当于缓存查价和实时查价设置了相同的限流,但由于实时查价的qps远低于缓存查价,所以满足了要求。

  • pre-function access 阶段的脚本

入参为json格式

复制代码
local kong = kong
local cjson = require("cjson.safe")

local req_body = kong.request.get_raw_body()
if req_body then
    local decoded_body = cjson.decode(req_body)

    if decoded_body and decoded_body.searchType and decoded_body.searchType ~= "" then
        kong.service.request.set_header("X-Search-Type", "check")
    else
        kong.service.request.set_header("X-Search-Type", "price")
    end
end

konga-kong-postgres 三件套

docker-compose.yml

复制代码
version: "3"

networks:
 kong-net:
  driver: bridge

services:
  kong-database:
    image: postgres:9.6
    restart: always
    networks:
      - kong-net
    environment:
      POSTGRES_PASSWORD: kong
      POSTGRES_USER: kong
      POSTGRES_DB: kong
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "kong"]
      interval: 5s
      timeout: 5s
      retries: 5

  kong-migration:
    image: kong:2.2.1-ubuntu
    command: "kong migrations bootstrap"
    networks:
      - kong-net
    restart: on-failure
    environment:
      KONG_PG_HOST: kong-database
      KONG_DATABASE: postgres
      KONG_PG_PASSWORD: kong
    links:
      - kong-database
    depends_on:
      - kong-database


  kong:
    image: kong:2.2.1-ubuntu
    restart: always
    networks:
      - kong-net
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: kong-database
      KONG_PG_USER: kong
      KONG_PG_PASSWORD: kong
      KONG_PROXY_LISTEN: 0.0.0.0:8000
      KONG_PROXY_LISTEN_SSL: 0.0.0.0:8443
      KONG_ADMIN_LISTEN: 0.0.0.0:8001
      KONG_PROXY_ACCESS_LOG: /dev/stdout
      KONG_ADMIN_ACCESS_LOG: /dev/stdout
      KONG_PROXY_ERROR_LOG: /dev/stderr
      KONG_ADMIN_ERROR_LOG: /dev/stderr
    depends_on:
      - kong-migration
      - kong-database
    healthcheck:
      test: ["CMD", "curl", "-f", "http://kong:8001"]
      interval: 5s
      timeout: 2s
      retries: 15
    ports:
      - "8001:8001"
      - "8000:8000"


  konga-prepare:
    image: pantsel/konga:0.14.9
    command: "-c prepare -a postgres -u postgresql://kong:kong@kong-database:5432/postgres"
    environment:
      DB_ADAPTER: postgres
      DB_HOST: kong-database
      DB_USER: kong
      DB_PASSWORD: kong
    networks:
      - kong-net
    restart: on-failure
    links:
      - kong-database
    depends_on:
      - kong-database

 
  konga:
    image: pantsel/konga:0.14.9
    restart: always
    networks:
        - kong-net
    environment:
      DB_ADAPTER: postgres
      DB_HOST: kong-database
      DB_USER: kong
      DB_PASSWORD: kong
      DB_DATABASE: postgres
      NODE_ENV: production
    depends_on:
      - kong-database
    ports:
      - "1337:1337"

总结

这里只是根据入参限流的简单实现,不支持根据入参设置不同的限流阈值。要实现更复杂的限流,可以自定义插件,或者下降到服务层处理。

相关推荐
tnan25222 天前
记录docker使用kong consul postgresql配置dns异常解决
docker·kong·consul
William一直在路上14 天前
KONG API Gateway中的核心概念
网络·gateway·kong
freesharer14 天前
kong网关集成Safeline WAF 插件
kong
鼠鼠我捏,要死了捏21 天前
高并发微服务限流算法方案对比与实践指南
微服务·高并发·限流
悟能不能悟1 个月前
kong是什么
kong
freesharer1 个月前
kong网关基于header分流灰度发布
kong
TracyCoder1232 个月前
接口限频算法:漏桶算法、令牌桶算法、滑动窗口算法
spring boot·spring·限流
曼彻斯特的海边2 个月前
RequestRateLimiterGatewayFilterFactory
spring cloud·gateway·限流
星释2 个月前
使用API网关Kong配置反向代理和负载均衡
运维·负载均衡·kong