Skip to content

zhangbincheng1997/kong-gateway

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kong-gateway

参考链接:https://github.com/qianyugang/kong-docs-cn

Windows10安装Docker

菜鸟教程

打包镜像

docker build -f Dockerfile -t kong-gateway .
 
build参数:
-f docker配置文件
-t 镜像名称
. 上下文环境的目录

网关安装

  1. 创建环境
docker network create kong-net
  1. 启动数据库
docker run -d --name kong-database \
     --network=kong-net \
     -p 5432:5432 \
     -e "POSTGRES_DB=kong" \
     -e "POSTGRES_USER=kong" \
     -e "POSTGRES_PASSWORD=kong" \
     postgres:9.6

// 换行
docker run -d --name kong-database --network=kong-net -p 5432:5432 -e "POSTGRES_DB=kong" -e "POSTGRES_USER=kong" -e "POSTGRES_PASSWORD=kong" postgres:9.6
  1. 初始化数据库
docker run --rm \
     --network=kong-net \
     -e "KONG_DATABASE=postgres" \
     -e "KONG_PG_HOST=kong-database" \
     -e "KONG_PG_USER=kong" \
     -e "KONG_PG_PASSWORD=kong" \
     kong:latest kong migrations bootstrap

// 换行
docker run --rm --network=kong-net -e "KONG_DATABASE=postgres" -e "KONG_PG_HOST=kong-database" -e "KONG_PG_USER=kong" -e "KONG_PG_PASSWORD=kong" kong:latest kong migrations bootstrap
  1. 启动kong
docker run -it -d --name kong \
     --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_ACCESS_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, 0.0.0.0:8444 ssl" \
     --env "KONG_PLUGINS=bundled,api-access-gateway,my-global-plugin" \
     --env "KONG_LUA_PACKAGE_PATH=./?.lua;./?/init.lua;/data/kong/?.lua;" \
     -v D:\\kong-gateway:/data/kong \
     -p 8000:8000 \
     -p 8443:8443 \
     -p 127.0.0.1:8001:8001 \
     -p 127.0.0.1:8444:8444 \
     kong-gateway

// 换行
docker run -it -d --name kong --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_ACCESS_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, 0.0.0.0:8444 ssl" --env "KONG_PLUGINS=bundled,api-access-gateway,my-global-plugin" --env "KONG_LUA_PACKAGE_PATH=./?.lua;./?/init.lua;/data/kong/?.lua;" -v D:\\kong-gateway:/data/kong -p 8000:8000 -p 8443:8443 -p 127.0.0.1:8001:8001 -p 127.0.0.1:8444:8444 kong-gateway

// -it参数表示伪终端,-d参数表示后台运行
// D:\\kong-gateway 加载的路径
// api-access-gateway,my-global-plugin 加载的插件

访问:http://localhost:8001/

  1. 启动konga
docker run -d --name konga \
     --network=kong-net \
     -e "DB_ADAPTER=postgres" \
     -e "DB_URI=postgresql://kong:kong@kong-database:5432/konga" \
     -p 1337:1337 \
     pantsel/konga

// 换行
docker run -d --name konga --network kong-net -e "DB_ADAPTER=postgres" -e "DB_URI=postgresql://kong:kong@kong-database:5432/konga" -p 1337:1337 pantsel/konga

访问:http://localhost:1337/

  1. docker界面

网关配置

  1. 登录/注册

  1. 创建连接Connection

  1. 创建服务Services
  • 命令方式:
curl -i -X POST \
--url http://localhost:8001/services \
--data 'name=baidu-service' \
--data 'url=https://www.baidu.com/'
  • 图形方式:

  1. 创建路由Routes
  • 命令方式:
curl -i -X POST \
--url http://localhost:8001/services/baidu-service/routes \
--data 'paths[]=/api/baidu'
  • 图形方式:

  1. 创建插件Plugins
  • 命令方式:
curl -i -X POST \
--url http://localhost:8001/routes/e72acd4e-3f72-4075-afe3-60e77a09a932/plugins \
--data '{"name":"api-access-gateway","config":{"skip_auth":true}}' \
  • 图形方式:

  1. 全局插件Global-Plugins

网关测试

TestToken

{
  "iss": "roro",
  "uid": 2021314
}

Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJyb3JvIiwidWlkIjoyMDIxMzE0fQ.SGv-R0BWv6rYzYW9i-ZvfxNZbOygNGirRxHUoncIEMQ
  • skip_auth = true

  • skip_auth = false

  • rate_limit
local function lock(uid)
    -- 连接池
    local red = redis:new(global_config.redis)
    -- NX SET if Not eXists
    -- EX 过期时间,seconds
    -- 成功:ok,失败:nil
    local ok, err = red:set(uid, 1, "NX", "EX", global_config.redis.expire_time or 10)
    kong.log(ok, err)
    if err then
        return response:err(error.redis_error, err)
    end
    if not ok then
        return response:err(error.rate_limit)
    end
end

插件开发

Plugin Development - Implementing Custom Logic

实现自定义逻辑

Plugin Development - Plugin Configuration

插件配置

About

docker + kong + konga + postgresql

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published