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

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

Kong doesn't load entities when using with PostgreSQL

问题

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

这是我的Compose文件:

  1. services:
  2. api_gateway:
  3. container_name: api_gateway
  4. image: kong
  5. volumes:
  6. - ./:/app
  7. environment:
  8. - KONG_DATABASE=postgres
  9. - KONG_PG_HOST=kong_postgresql
  10. - KONG_PG_PORT=5432
  11. - KONG_PG_USER=kong
  12. - KONG_PG_DB=kong
  13. - KONG_PG_PASSWORD=password
  14. - KONG_PROXY_ACCESS_LOG=/dev/stdout
  15. - KONG_ADMIN_ACCESS_LOG=/dev/stdout
  16. - KONG_PROXY_ERROR_LOG=/dev/stderr
  17. - KONG_ADMIN_ERROR_LOG=/dev/stderr
  18. - KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl
  19. - KONG_DECLARATIVE_CONFIG=/app/kong.yml
  20. ports:
  21. - 8000:8000
  22. - 8443:8443
  23. - 8001:8001
  24. - 8444:8444
  25. command: bash -c "while !</dev/tcp/kong_postgresql/5432; do sleep 10; done; kong migrations bootstrap && kong start"
  26. restart: always
  27. kong_postgresql:
  28. image: postgres
  29. container_name: kong_postgresql
  30. restart: always
  31. expose:
  32. - 5432
  33. environment:
  34. POSTGRES_USER: kong
  35. POSTGRES_PASSWORD: password
  36. POSTGRES_DB: kong
  37. ports:
  38. - 5432:5432

这是我的kong.yml:

  1. _format_version: "3.0"
  2. _transform: true
  3. services:
  4. - name: customer_service
  5. url: http://customer:81/
  6. - name: auth_service
  7. url: http://auth:84/
  8. routes:
  9. - name: basket-requests
  10. service: basket_service
  11. paths:
  12. - /basket
  13. - name: auth-requests
  14. service: auth_service
  15. paths:
  16. - /auth
  17. plugins:
  18. - name: jwt
  19. service: basket_service
  20. enabled: true
  21. config:
  22. uri_param_names:
  23. - jwt
  24. key_claim_name: kid
  25. maximum_expiration: 100
  26. claims_to_verify:
  27. - 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:

  1. services:
  2. api_gateway:
  3. container_name: api_gateway
  4. image: kong
  5. volumes:
  6. - ./:/app
  7. environment:
  8. - KONG_DATABASE=postgres
  9. - KONG_PG_HOST=kong_postgresql
  10. - KONG_PG_PORT=5432
  11. - KONG_PG_USER=kong
  12. - KONG_PG_DB=kong
  13. - KONG_PG_PASSWORD=password
  14. - KONG_PROXY_ACCESS_LOG=/dev/stdout
  15. - KONG_ADMIN_ACCESS_LOG=/dev/stdout
  16. - KONG_PROXY_ERROR_LOG=/dev/stderr
  17. - KONG_ADMIN_ERROR_LOG=/dev/stderr
  18. - KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl
  19. - KONG_DECLARATIVE_CONFIG=/app/kong.yml
  20. ports:
  21. - 8000:8000
  22. - 8443:8443
  23. - 8001:8001
  24. - 8444:8444
  25. command: bash -c &quot;while !&lt;/dev/tcp/kong_postgresql/5432; do sleep 10; done; kong migrations bootstrap &amp;&amp; kong start&quot;
  26. restart: always
  27. kong_postgresql:
  28. image: postgres
  29. container_name: kong_postgresql
  30. restart: always
  31. expose:
  32. - 5432
  33. environment:
  34. POSTGRES_USER: kong
  35. POSTGRES_PASSWORD: password
  36. POSTGRES_DB: kong
  37. ports:
  38. - 5432:5432

And here is my kong.yml:

  1. _format_version: &quot;3.0&quot;
  2. _transform: true
  3. services:
  4. - name: customer_service
  5. url: http://customer:81/
  6. - name: auth_service
  7. url: http://auth:84/
  8. routes:
  9. - name: basket-requests
  10. service: basket_service
  11. paths:
  12. - /basket
  13. - name: auth-requests
  14. service: auth_service
  15. paths:
  16. - /auth
  17. plugins:
  18. - name: jwt
  19. service: basket_service
  20. enabled: true
  21. config:
  22. uri_param_names:
  23. - jwt
  24. key_claim_name: kid
  25. maximum_expiration: 100
  26. claims_to_verify:
  27. - 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命令将文件中的配置导入数据库。

尝试这样做:

  1. docker exec -it api_gateway bash
  2. 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:

  1. docker exec -it api_gateway bash
  2. 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:

确定