Kong在使用PostgreSQL时不加载实体。

huangapple go评论136阅读模式
英文:

Kong doesn't load entities when using with PostgreSQL

问题

我想使用Kong作为API网关。我正在使用Kong镜像。

这是我的Compose文件:

services:
  api_gateway:
    container_name: api_gateway
    image: kong
    volumes:
      - ./:/app
    environment:
      - KONG_DATABASE=postgres
      - KONG_PG_HOST=kong_postgresql
      - KONG_PG_PORT=5432
      - KONG_PG_USER=kong
      - KONG_PG_DB=kong
      - KONG_PG_PASSWORD=password
      - KONG_PROXY_ACCESS_LOG=/dev/stdout
      - KONG_ADMIN_ACCESS_LOG=/dev/stdout
      - KONG_PROXY_ERROR_LOG=/dev/stderr
      - KONG_ADMIN_ERROR_LOG=/dev/stderr
      - KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl
      - KONG_DECLARATIVE_CONFIG=/app/kong.yml
    ports:
      - 8000:8000
      - 8443:8443
      - 8001:8001
      - 8444:8444
    command: bash -c "while !</dev/tcp/kong_postgresql/5432; do sleep 10; done; kong migrations bootstrap && kong start"
    restart: always

  kong_postgresql:
    image: postgres
    container_name: kong_postgresql
    restart: always
    expose:
      - 5432
    environment:
      POSTGRES_USER: kong
      POSTGRES_PASSWORD: password
      POSTGRES_DB: kong
    ports:
      - 5432:5432

这是我的kong.yml:

_format_version: "3.0"

_transform: true

services:
  - name: customer_service
    url: http://customer:81/

  - name: auth_service
    url: http://auth:84/

routes:
  - name: basket-requests
    service: basket_service
    paths:
      - /basket

  - name: auth-requests
    service: auth_service
    paths:
      - /auth

plugins:
  - name: jwt
    service: basket_service
    enabled: true
    config:
      uri_param_names:
        - jwt
      key_claim_name: kid
      maximum_expiration: 100
      claims_to_verify:
        - exp

数据库和Kong成功启动。但是,当我在http://localhost:8001/services上发出请求时,它返回一个空数组。为什么会发生这种情况?Kong是否不支持使用数据库进行声明性配置?我想从我的kong.yml文件加载实体到PostgreSQL中。

我尝试将KONG_DATABASE设置为"off",然后一切正常。如果在http://localhost:8001/services上发出请求,它将返回kong.yml文件中的服务。

英文:

I want to use Kong as an API Gateway. I am using Kong image.

Here is my compose file:

services:
  api_gateway:
    container_name: api_gateway
    image: kong
    volumes:
      - ./:/app
    environment:
      - KONG_DATABASE=postgres
      - KONG_PG_HOST=kong_postgresql
      - KONG_PG_PORT=5432
      - KONG_PG_USER=kong
      - KONG_PG_DB=kong
      - KONG_PG_PASSWORD=password
      - KONG_PROXY_ACCESS_LOG=/dev/stdout
      - KONG_ADMIN_ACCESS_LOG=/dev/stdout
      - KONG_PROXY_ERROR_LOG=/dev/stderr
      - KONG_ADMIN_ERROR_LOG=/dev/stderr
      - KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl
      - KONG_DECLARATIVE_CONFIG=/app/kong.yml
    ports:
      - 8000:8000
      - 8443:8443
      - 8001:8001
      - 8444:8444
    command: bash -c &quot;while !&lt;/dev/tcp/kong_postgresql/5432; do sleep 10; done; kong migrations bootstrap &amp;&amp; kong start&quot;
    restart: always

  kong_postgresql:
    image: postgres
    container_name: kong_postgresql
    restart: always
    expose:
      - 5432
    environment:
      POSTGRES_USER: kong
      POSTGRES_PASSWORD: password
      POSTGRES_DB: kong
    ports:
      - 5432:5432




And here is my kong.yml:

_format_version: &quot;3.0&quot;

_transform: true

services:
  - name: customer_service
    url: http://customer:81/

  - name: auth_service
    url: http://auth:84/

routes:
  - name: basket-requests
    service: basket_service
    paths:
      - /basket

  - name: auth-requests
    service: auth_service
    paths:
      - /auth

plugins:
  - name: jwt
    service: basket_service
    enabled: true
    config:
      uri_param_names:
        - jwt
      key_claim_name: kid
      maximum_expiration: 100
      claims_to_verify:
        - exp


The database and Kong starts successfully. But when I make request at http://localhost:8001/services it returns an empty array. Why is this happening? Does not Kong support declarative configuration with using a database? I want to load my entities to postgresql from my kong.yml file.

I tried setting KONG_DATABASE=off and then it is OK. If a make a request at http://localhost:8001/services it returns the services in kong.yml file.

答案1

得分: 0

Kong 不支持在使用数据库时从声明性配置加载。它是为 无数据库模式 设计的,在这种模式下,您不需要一个 PostgreSQL 数据库。

要以声明性方式管理配置,我建议使用 deck,它是一个使用 Kong 的管理 API 将声明性配置文件应用于数据库的命令行工具。

英文:

Kong does not support loading from a declarative config when using a database. It is designed for DB-less mode where you don't need a Postgres database.

To manage configuration declaratively, I recommend using deck which is a CLI that applies a declarative config file to the database using Kong's admin API.

答案2

得分: 0

如果您想同时使用声明性配置文件和数据库,您可以使用kong config db_import命令将文件中的配置导入数据库。

尝试这样做:

docker exec -it api_gateway bash
kong config db_import /app/kong.yml

您可以在这里找到有关db_import命令的一些重要注意事项:https://docs.konghq.com/deck/latest/faqs/

英文:

If you want to use both the declarative configuration file and the database, you can use the kong config db_import command to import the configuration from the file into the database.

try this:

docker exec -it api_gateway bash
kong config db_import /app/kong.yml

Here you can find some important notes about db_import command :https://docs.konghq.com/deck/latest/faqs/

huangapple
  • 本文由 发表于 2023年5月7日 04:26:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定