kong网关的使用

复制代码
关于docker的kong详情https://cloud.tencent.com/developer/article/2339527
和自定义插件https://blog.csdn.net/whos2016/article/details/103034883
docker run -d --name kong-gateway \
 --network=kong-net \
 -e "KONG_DATABASE=postgres" \
 -e "KONG_PG_HOST=kong-database" \
 -e "KONG_PG_USER=kong" \
 -e "KONG_PG_PASSWORD=kong" \
 -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
 -e "KONG_ADMIN_ACCE_LOG=/dev/stdout" \
 -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
 -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
 -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
 -e "KONG_ADMIN_GUI_URL=http://localhost:8002" \
 -e "KONG_PLUGIN_PATH=/kong/plugins/?.lua;;" \
 -e "KONG_LUA_PACKAGE_PATH=/kong/plugins/my-first-plugin/?.lua;;" \
 -e "KONG_PLUGINS=bundled,my-first-plugin" \
 -e "KONG_CUSTOM_PLUGINS=my-first-plugin" \
 -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
 -e "KONG_LOG_LEVEL=debug" \
 -p 8000:8000 \
 -p 8443:8443 \
 -p 8001:8001 \
 -p 8444:8444 \
 -p 8002:8002 \
 -p 8445:8445 \
 -p 8003:8003 \
 -p 8004:8004 \
 kong/kong-gateway:latest


#带有映射目录的容器
docker run -d --name kong-gateway \
 --network=kong-net \
 -e "KONG_DATABASE=postgres" \
 -e "KONG_PG_HOST=kong-database" \
 -e "KONG_PG_USER=kong" \
 -e "KONG_PG_PASSWORD=kong" \
 -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
 -e "KONG_ADMIN_ACCE_LOG=/dev/stdout" \
 -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
 -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
 -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
 -e "KONG_ADMIN_GUI_URL=http://localhost:8002" \
 -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
 -e "KONG_LOG_LEVEL=debug" \
 -v "/root/workspace/my-plugins:/kong/plugins" \
 -v "/root/workspace/etc/kong:/etc/kong" \
 -v "/root/workspace/lua_kong/kong:/usr/local/share/lua/5.1/kong" \
 -p 8000:8000 \
 -p 8443:8443 \
 -p 8001:8001 \
 -p 8444:8444 \
 -p 8002:8002 \
 -p 8445:8445 \
 -p 8003:8003 \
 -p 8004:8004 \
 kong/kong-gateway:latest

## 指定一个目录存放kong容器的内容,这个有三个部分 自定插件,kong配置 和lua脚本
mkdir etc lua_kong
docker cp ${container_id}:/etc/kong etc/kong # ${container_id} 换成容器ID,下同
docker cp ${container_id}:/usr/local/share/lua/5.1/kong lua_kong/kong  # 

关于自定义插件内容可以参考文章

手把手教你实现一个Kong网关插件-腾讯云开发者社区-腾讯云

有关问题,进入kong容器内部,修改的配置文件无效被覆盖:因为kong会生成自己定义的配置文件去启动nginx,所以你在/usr/local/kong中的nginx-kong.conf修改的内容是生成式的,每次启动就没了,使用kong启动一个nginx的server方式很简单,在宿主机新建文件,具体内容参考

Kong配置项向导 - 简书 注意添加一个服务器指定的端口是在容器内部,如果kong没有暴露该接口对外是访问不了,你可以该8001然后定义一个路由返回内容

复制代码
server {
    listen 8001; ## 指定端口kong容器一定暴露在宿主机
    location /sayHello {
      return 200 "服务正在升级,请稍后再试8001 ,你想使用kong开启8009服务器提供服务担是该端口是容器内部没有暴露>出来";
    }
}

这些就能使你的kong像nginx和openresty一样工作和设置了。

相关推荐
DJ斯特拉1 天前
Redis使用lua脚本
junit·单元测试·lua
2301_771717211 天前
idea中springboot中使用junit测试报错的解决方案
spring boot·junit·intellij-idea
Java成神之路-2 天前
Spring 整合 MyBatis 全流程详解(附 Junit 单元测试实战)(Spring系列6)
spring·junit·mybatis
難釋懷3 天前
OpenResty封装Redis工具
redis·junit·openresty
希望永不加班6 天前
SpringBoot 接口测试:Postman 与 JUnit 5 实战
java·spring boot·后端·junit·postman
難釋懷6 天前
OpenResty基于ID负载均衡
junit·负载均衡·openresty
難釋懷6 天前
OpenResty-CJSON工具类
junit·openresty
難釋懷6 天前
OpenResty封装http工具
http·junit·openresty
難釋懷9 天前
OpenResty监听请求
junit·openresty
難釋懷9 天前
OpenResty获取参数的API
junit·openresty