配置开发环境
# wget https://golang.org/dl/go1.14.11.linux-amd64.tar.gz# tar -C /usr/local/ -xzf go1.14.11.linux-amd64.tar.gzecho 'export PATH=$PATH:/usr/local/go/bin' >>.profilesource .profilego vesion
设置go代理
go env -w GO111MODULE=ongo env -w GOPROXY=https://goproxy.io,direct
源码地址 https://github.com/Kong/go-pluginserver
下载安装go-pluginserver
git clone https://github.com/Kong/go-pluginserver.gitcd go-pluginservergo mod tidygo buildsudo cp go-pluginserver /usr/local/bin/
安装c相关编译环境
contos
yum install make automake gcc gcc-c++ kernel-devel
ubuntu
apt-get install build-essential
下载官方插件示例
# git clone https://github.com/Kong/go-plugins.gitcd go-pluginsgo build -buildmode=plugin go-hello.go
apt-get install build-essential
创建插件目录
sudo mkdir -p /usr/local/share/go/1.14/kong/pluginsexport KONG_GO_PLUGIN_DIR=/usr/local/share/go/1.14/kong/pluginscp go-hello.so $KONG_GO_PLUGIN_DIR
拉取编译镜像
docker pull kong/go-plugin-tool:latest-alpine-latest
编译
docker run --rm -v $(PWD):/plugins kong/go-plugin-tool:latest-alpine-latest build go-hello.go
测试
curl -i -X POST http://localhost:8001/services \--data name=example_service \--data url='http://mockbin.org'
创建路由
curl -i -X POST http://localhost:8001/services/example_service/routes \--data 'paths[]=/mock' \--data name=mocking
测试路由是否成功
curl -i -X GET http://localhost:8000/mock/request
开启插件
curl -i -X POST http://localhost:8001/services/example_service/plugins --data name=go-hello --data config.message=go
利用httpie
$ http 127.0.0.1:8000/mock/requestHTTP/1.1 200 OKAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Headers: host,connection,x-forwarded-for,x-forwarded-proto,x-forwarded-host,x-forwarded-port,x-real-ip,x-forwarded-path,x-forwarded-prefix,user-agent,accept,accept-encoding,x-request-id,via,connect-time,x-request-start,total-route-timeAccess-Control-Allow-Methods: GETAccess-Control-Allow-Origin: *Connection: keep-aliveContent-Length: 983Content-Type: application/json; charset=utf-8Date: Tue, 10 Nov 2020 10:17:46 GMTEtag: W/"3d7-KRV5NiPRV4+gPGajVkNTrRX02Hc"Keep-Alive: timeout=4Kong-Cloud-Request-Id: d43c26055e92b8626e860080b7db5d05Proxy-Connection: keep-aliveServer: CowboyVary: Accept, Accept-EncodingVia: kong/2.2.0X-Kong-Proxy-Latency: 16X-Kong-Request: baxiangX-Kong-Upstream-Latency: 520X-Kong-Upstream-Status: 200X-Powered-By: mockbin
插件问题
go-pluginserver terminated when using custom Go plugin #22
nginx 的用户需要设置 https://github.com/Kong/go-pluginserver/issues/22
编译错误
Error: /usr/local/share/lua/5.1/kong/cmd/start.lua:64: 2020/11/10 18:41:10 failed to open plugin go-hello: plugin.Open("/usr/local/share/go/1.14/kong/plugins/go-hello"): plugin was built with a different version of package internal/cpunginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/db/dao/plugins/go.lua:480: bad argument #1 to 'ipairs' (table expected, got nil)stack traceback:[C]: in function 'ipairs'/usr/local/share/lua/5.1/kong/db/dao/plugins/go.lua:480: in function 'get_plugin'/usr/local/share/lua/5.1/kong/db/dao/plugins/go.lua:519: in function 'load_plugin'/usr/local/share/lua/5.1/kong/db/dao/plugins.lua:159: in function 'load_plugin_handler'/usr/local/share/lua/5.1/kong/db/dao/plugins.lua:243: in function 'load_plugin'/usr/local/share/lua/5.1/kong/db/dao/plugins.lua:295: in function 'load_plugin_schemas'/usr/local/share/lua/5.1/kong/init.lua:467: in function 'init'init_by_lua:3: in main chunkRun with --v (verbose) or --vv (debug) for more details
kong 配置
database = postgres # Determines which of PostgreSQL or Cassandra# this node will use as its datastore.# Accepted values are `postgres`,# `cassandra`, and `off`.pg_host = 127.0.0.1 # Host of the Postgres server.pg_port = 5438 # Port of the Postgres server.pg_timeout = 5000 # Defines the timeout (in ms), for connecting,# reading and writing.pg_user = kong # Postgres user.pg_password = baxiang2020 # Postgres user's password.pg_database = kong # The database name to connect to.admin_listen = 0.0.0.0:8001nginx_user= rootplugins = bundled,go-hellogo_plugins_dir = /usr/local/share/go/1.14/kong/plugins
Docker file
## Compile Go filesFROM golang:1.13.5 as compilerRUN git clone https://github.com/Kong/go-plugins /usr/src/go-pluginsRUN cd /usr/src/go-plugins && makeRUN git clone https://github.com/Kong/go-pluginserver /usr/src/go-pluginserverRUN cd /usr/src/go-pluginserver && make## Build custom Kong imageFROM kong:2.0-ubuntuLABEL maintainer="HP GSB Solutions"# Copy Kong configurationCOPY conf/kong.conf /etc/kong/kong.conf# Copy Go filesCOPY --from=compiler /usr/src/go-plugins/*.so /usr/local/go-plugins/RUN chmod -R 755 /usr/local/go-plugins/*.so# Copy Go Plugin Server executableCOPY --from=compiler /usr/src/go-pluginserver/go-pluginserver /usr/local/go-pluginserverRUN chmod -R 755 /usr/local/go-pluginserver
配置文件
conf/kong.conf:
plugins=bundled,go-hellogo_plugins_dir=/usr/local/go-pluginsgo_pluginserver_exe=/usr/local/go-pluginserver
编译
docker build —rm=true —force-rm=true -t kong2experiment .
参考
https://github.com/Kong/go-plugins
https://github.com/lampnick/kong-rate-limiting-golang
https://github.com/domudall/kong-go-demo
https://dev.to/_mertsimsek/go-plugin-development-on-kong-50pb
https://github.com/agarwal-nitesh/kong-auth-plugin-go
https://github.com/Kong/docker-kong/issues/326
https://github.com/agarwal-nitesh/kong-auth-plugin-go
https://github.com/gszr/kong-demos/tree/master/02-go-plugins
