首页 > 解决方案 > 如何使用服务发现配置 kong api 网关?

问题描述

我正在研究微服务架构。我想在前面实现一个kong API网关。我不想进入 kong admin API 并手动添加所有公开的 API。我可以借助像 eureka 这样的任何服务发现实现来将我的 API 自动配置到 Kong 吗?

标签: spring-bootnetflix-eurekaservice-discoverykong

解决方案


您可以结合使用springdoc-openapi生成OpenAPI 规范和使用Insomnia 的 Inso CLI转换到Kong 的声明式配置。这种方法使您无需手动使用 Kong 的管理 API 来添加 Spring Boot 服务(我不喜欢这样做):

在此处输入图像描述

这个过程甚至可以在您的(例如 Maven)构建中自动化 - 并且在每次构建或推送后自动更新(如果在您的 CI/CD 管道中配置)。

为这个场景创建了一篇完全可以理解的博客文章,但这里有一些简短的步骤:

1. 使用 springdoc-openapi-maven-plugin 生成 OpenAPI 规范

添加springdoc-openapi-ui到您的pom.xml

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.4.8</version>
</dependency>

然后添加springdoc-openapi-maven-plugin并配置你spring-boot-maven-plugin在阶段内运行它pre-integration-test

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>pre-integration-test</id>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>post-integration-test</id>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
        <execution>
            <phase>integration-test</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

现在 amvn verify已经openapi.json在您的target目录中生成了一个。

2. 从 OpenAPI 规范生成 Kong 声明式配置

通过以下方式安装Insomnia Inso CLInpm

npm i -g insomnia-inso

现在使用 Insomnica Inso CLI 从 OpenAPI json 生成 Kong 声明式配置:

inso generate config weatherbackend/target/openapi.json --output kong/kong.yml --type declarative --verbose

这将生成一个kong/kong.yml这样的:

_format_version: "1.1"
services:
  - name: weatherbackend
    url: http://weatherbackend:8080
    plugins: []
    routes:
      - tags:
          - OAS3_import
        name: weatherbackend-path-get
        methods:
          - GET
        paths:
          - /weather/general/outlook
        strip_path: false
      - tags:
          - OAS3_import
        name: weatherbackend-path_1-post
        methods:
          - POST
        paths:
          - /weather/general/outlook
        strip_path: false
      - tags:
          - OAS3_import
        name: weatherbackend-path_2-get
        methods:
          - GET
        paths:
          - /weather/(?<name>\S+)$
        strip_path: false
    tags:
      - OAS3_import
upstreams:
  - name: weatherbackend
    targets:
      - target: weatherbackend:8080
    tags:
      - OAS3_import

3. 基于生成的 kong.yml 以无 DB 声明式配置模式运行 Kong

可以直接使用这个文件作为Kong的配置!请参阅此 Docker Compose 部署作为示例,了解如何执行此操作:

version: '3.7'

services:
  kong:
    image: kong:2.2.0
    environment:
      KONG_ADMIN_ACCESS_LOG: /dev/stdout
      KONG_ADMIN_ERROR_LOG: /dev/stderr
      KONG_ADMIN_LISTEN: '0.0.0.0:8001'
      KONG_DATABASE: "off"
      KONG_DECLARATIVE_CONFIG: /usr/local/kong/declarative/kong.yml
      KONG_PROXY_ACCESS_LOG: /dev/stdout
      KONG_PROXY_ERROR_LOG: /dev/stderr
    volumes:
      - ./kong/:/usr/local/kong/declarative
    networks:
      - kong-net
    ports:
      - "8000:8000/tcp"
      - "127.0.0.1:8001:8001/tcp"
      - "8443:8443/tcp"
      - "127.0.0.1:8444:8444/tcp"
    healthcheck:
      test: ["CMD", "kong", "health"]
      interval: 10s
      timeout: 10s
      retries: 10
    restart: on-failure
    deploy:
      restart_policy:
        condition: on-failure
 
  # no portbinding here - the actual services should be accessible through Kong
  weatherbackend:
    build: ./weatherbackend
    ports:
      - "8080"
    networks:
      - kong-net
    tty:
      true
    restart:
      unless-stopped

networks:
  kong-net:
    external: false

整个过程是可自动化的(例如,使用我的帖子中描述的exec-maven-plugin)。

如果您对这一切如何协同工作有疑问,您还可以查看这个示例项目,该项目利用了这里提到的每一步(包括每一步的 CI/CD 自动化):https ://github.com/jonashackt/spring-引导-openapi-kong


推荐阅读