我正在研究一个微服务架构。我想在前面实现一个孔API网关。我不想进入kong admin API并手动添加所有公开的API。我可以借助像eureka这样的服务发现实现将我的API自动配置到Kong吗?
发布于 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
<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基于生成的kong.yml在无DB声明式配置模式下运行3.
你可以直接使用这个文件作为孔的配置!请以此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
发布于 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标签读取所有配置。
https://stackoverflow.com/questions/61754819
复制相似问题