配置开发环境
# wget https://golang.org/dl/go1.14.11.linux-amd64.tar.gz
# tar -C /usr/local/ -xzf go1.14.11.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >>.profile
source .profile
go vesion
设置go代理
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct
源码地址 https://github.com/Kong/go-pluginserver
下载安装go-pluginserver
git clone https://github.com/Kong/go-pluginserver.git
cd go-pluginserver
go mod tidy
go build
sudo 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.git
cd go-plugins
go build -buildmode=plugin go-hello.go
apt-get install build-essential
创建插件目录
sudo mkdir -p /usr/local/share/go/1.14/kong/plugins
export KONG_GO_PLUGIN_DIR=/usr/local/share/go/1.14/kong/plugins
cp 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/request
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-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-time
Access-Control-Allow-Methods: GET
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 983
Content-Type: application/json; charset=utf-8
Date: Tue, 10 Nov 2020 10:17:46 GMT
Etag: W/"3d7-KRV5NiPRV4+gPGajVkNTrRX02Hc"
Keep-Alive: timeout=4
Kong-Cloud-Request-Id: d43c26055e92b8626e860080b7db5d05
Proxy-Connection: keep-alive
Server: Cowboy
Vary: Accept, Accept-Encoding
Via: kong/2.2.0
X-Kong-Proxy-Latency: 16
X-Kong-Request: baxiang
X-Kong-Upstream-Latency: 520
X-Kong-Upstream-Status: 200
X-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/cpu
nginx: [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 chunk
Run 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:8001
nginx_user= root
plugins = bundled,go-hello
go_plugins_dir = /usr/local/share/go/1.14/kong/plugins
Docker file
## Compile Go files
FROM golang:1.13.5 as compiler
RUN git clone https://github.com/Kong/go-plugins /usr/src/go-plugins
RUN cd /usr/src/go-plugins && make
RUN git clone https://github.com/Kong/go-pluginserver /usr/src/go-pluginserver
RUN cd /usr/src/go-pluginserver && make
## Build custom Kong image
FROM kong:2.0-ubuntu
LABEL maintainer="HP GSB Solutions"
# Copy Kong configuration
COPY conf/kong.conf /etc/kong/kong.conf
# Copy Go files
COPY --from=compiler /usr/src/go-plugins/*.so /usr/local/go-plugins/
RUN chmod -R 755 /usr/local/go-plugins/*.so
# Copy Go Plugin Server executable
COPY --from=compiler /usr/src/go-pluginserver/go-pluginserver /usr/local/go-pluginserver
RUN chmod -R 755 /usr/local/go-pluginserver
配置文件
conf/kong.conf
:
plugins=bundled,go-hello
go_plugins_dir=/usr/local/go-plugins
go_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