【问题标题】:How to configure kong api gateway with service discovery?如何使用服务发现配置 kong api 网关?
【发布时间】:2020-05-12 14:49:45
【问题描述】:

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

【问题讨论】:

    标签: spring-boot netflix-eureka service-discovery kong


    【解决方案1】:

    您可以结合使用OpenAPI spec 生成springdoc-openapi 和使用Insomnia's Inso CLI 转换为Kong's Declarative Configuration。这种方法使您无需手动使用 Kong 的管理 API 来添加 Spring Boot 服务(我不喜欢这样做):

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

    我创建了a fully comprehensible blog post for this scenario,但以下是简短的步骤:

    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>
    

    现在mvn verify 已经在您的target 目录中生成了openapi.json

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

    通过npm安装Insomnia Inso CLI

    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作为described in my post)。

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

    【讨论】:

      【解决方案2】:

      我对任何自动操作一无所知,但您可以自己构建一些东西并使用管理 API 添加 API:https://docs.konghq.com/getting-started-guide/latest/expose-services/

      如果您想要使用服务发现的真正自动网关,请查看使用 Eureka 和 Zuul 的 https://spring.io/projects/spring-cloud-netflix

      或者,您可以查看 Traefik,它有许多加载配置的选项(包括从您放置在容器上的 Docker 标签中读取所有配置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-28
        • 2020-04-16
        • 1970-01-01
        • 2019-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-27
        相关资源
        最近更新 更多