首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何配置具有服务发现功能的kong api网关?

如何配置具有服务发现功能的kong api网关?
EN

Stack Overflow用户
提问于 2020-05-12 22:49:46
回答 2查看 2.5K关注 0票数 2

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

EN

回答 2

Stack Overflow用户

发布于 2020-12-04 19:43:46

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

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

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

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

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

代码语言:javascript
复制
<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阶段运行它:

代码语言:javascript
复制
<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

代码语言:javascript
复制
npm i -g insomnia-inso

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

代码语言:javascript
复制
inso generate config weatherbackend/target/openapi.json --output kong/kong.yml --type declarative --verbose

这将生成如下所示的kong/kong.yml

代码语言:javascript
复制
_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

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

你可以直接使用这个文件作为孔的配置!请以此Docker Compose部署为例来了解如何做到这一点:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2020-05-13 01:39:31

我不知道什么是自动的,但您可以自己构建一些东西,并使用Admin API添加API:https://docs.konghq.com/getting-started-guide/latest/expose-services/

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61754819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档