花椒直播 Kong 应用实践
- - DockOne.ioKong 是面向现代架构(混合云,混合组织)的下一代 API 网关平台,具有云原生、高性能,易用、可扩展等特性. 适用于 API Gateway,Kubernetes Ingress,Service Mesh Sidecar 等场景. 云原生:与平台无关,Kong 可以从裸机运行到 Kubernetes.
# 创建一个名称 hello 的 upstream
curl -X POST http://localhost:8001/upstreams --data "name=hello"
# 为 hello 添加两个负载均衡节点
curl -X POST http://localhost:8001/upstreams/hello/targets --data "target=localhost:8080" --data "weight=100"
curl -X POST http://localhost:8001/upstreams/hello/targets --data "target=localhost:8081" --data "weight=100"
# 配置一个service
curl -X POST http://localhost:8001/services --data "name=hello" --data "host=hello"
# 为service 配置路由
curl -X POST http://localhost:8001/routes --data "paths[]=/hello" --data "service.id={$service.id 配置上面service返回的}"
# /etc/kong/kong.conf
proxy_listen = 0.0.0.0:8000, 0.0.0.0:8443 ssl, 0.0.0.0:9080 http2
simple-plugin
├── handler.lua
└── schema.lua
complete-plugin
├── api.lua
├── daos.lua
├── handler.lua
├── migrations
│ ├── cassandra.lua
│ └── postgres.lua
└── schema.lua
local BasePlugin = require "kong.plugins.base_plugin"
-- The actual logic is implemented in those modules
local access = require "kong.plugins.my-custom-plugin.access"
local body_filter = require "kong.plugins.my-custom-plugin.body_filter"
local CustomHandler = BasePlugin:extend()
function CustomHandler:new()
CustomHandler.super.new(self, "my-custom-plugin")
end
function CustomHandler:access(config)
CustomHandler.super.access(self)
-- Execute any function from the module loaded in `access`,
-- for example, `execute()` and passing it the plugin's configuration.
access.execute(config)
end
function CustomHandler:body_filter(config)
CustomHandler.super.body_filter(self)
-- Execute any function from the module loaded in `body_filter`,
-- for example, `execute()` and passing it the plugin's configuration.
body_filter.execute(config)
end
return CustomHandler
return {
no_consumer = true, -- this plugin will only be applied to Services or Routes,
fields = {
-- Describe your plugin's configuration's schema here.
},
self_check = function(schema, plugin_t, dao, is_updating)
-- perform any custom verification
return true
end
}
/data/kong/plugins/simple-plugin/
lua_package_path = /data/?.lua;($default);
plugins = bundled,simple-plugin
nginx_proxy_proxy_buffer_size=128k
nginx_proxy_proxy_buffers=4 256k
nginx_proxy_proxy_busy_buffers_size=256k
如果设置为 true, paths 设置有值,那么请求将会被替换掉
{ "paths": ["/service"], "strip_path": true, "service": { "id": "..." }}
请求:GET /service/path/to/resource HTTP/1.1Host: …
Proxy: GET /path/to/resource HTTP/1.1
{ "paths": ["/version/\d+/service"], "strip_path": true, "service": { "id": "..." }}
请求:GET /version/1/service/path/to/resource HTTP/1.1
Proxy: GET /path/to/resource HTTP/1.1
如果设置为 true,代理后仍然保留 header 请求 host
请求:GET / HTTP/1.1Host: service.com
Proxy: GET / HTTP/1.1Host: service.com
设置为false,将不保留header host
GET / HTTP/1.1Host: service.com
GET / HTTP/1.1Host: