配置开发环境

  1. # wget https://golang.org/dl/go1.14.11.linux-amd64.tar.gz
  2. # tar -C /usr/local/ -xzf go1.14.11.linux-amd64.tar.gz
  3. echo 'export PATH=$PATH:/usr/local/go/bin' >>.profile
  4. source .profile
  5. go vesion

设置go代理

  1. go env -w GO111MODULE=on
  2. go env -w GOPROXY=https://goproxy.io,direct

源码地址 https://github.com/Kong/go-pluginserver
下载安装go-pluginserver

  1. git clone https://github.com/Kong/go-pluginserver.git
  2. cd go-pluginserver
  3. go mod tidy
  4. go build
  5. sudo cp go-pluginserver /usr/local/bin/

安装c相关编译环境
contos

  1. yum install make automake gcc gcc-c++ kernel-devel

ubuntu

  1. apt-get install build-essential

下载官方插件示例

  1. # git clone https://github.com/Kong/go-plugins.git
  2. cd go-plugins
  3. go build -buildmode=plugin go-hello.go
  1. apt-get install build-essential

创建插件目录

  1. sudo mkdir -p /usr/local/share/go/1.14/kong/plugins
  2. export KONG_GO_PLUGIN_DIR=/usr/local/share/go/1.14/kong/plugins
  3. cp go-hello.so $KONG_GO_PLUGIN_DIR

拉取编译镜像

  1. docker pull kong/go-plugin-tool:latest-alpine-latest

编译

  1. docker run --rm -v $(PWD):/plugins kong/go-plugin-tool:latest-alpine-latest build go-hello.go

测试

  1. curl -i -X POST http://localhost:8001/services \
  2. --data name=example_service \
  3. --data url='http://mockbin.org'

创建路由

  1. curl -i -X POST http://localhost:8001/services/example_service/routes \
  2. --data 'paths[]=/mock' \
  3. --data name=mocking

测试路由是否成功

  1. curl -i -X GET http://localhost:8000/mock/request

开启插件

  1. curl -i -X POST http://localhost:8001/services/example_service/plugins --data name=go-hello --data config.message=go

利用httpie

  1. $ http 127.0.0.1:8000/mock/request
  2. HTTP/1.1 200 OK
  3. Access-Control-Allow-Credentials: true
  4. 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
  5. Access-Control-Allow-Methods: GET
  6. Access-Control-Allow-Origin: *
  7. Connection: keep-alive
  8. Content-Length: 983
  9. Content-Type: application/json; charset=utf-8
  10. Date: Tue, 10 Nov 2020 10:17:46 GMT
  11. Etag: W/"3d7-KRV5NiPRV4+gPGajVkNTrRX02Hc"
  12. Keep-Alive: timeout=4
  13. Kong-Cloud-Request-Id: d43c26055e92b8626e860080b7db5d05
  14. Proxy-Connection: keep-alive
  15. Server: Cowboy
  16. Vary: Accept, Accept-Encoding
  17. Via: kong/2.2.0
  18. X-Kong-Proxy-Latency: 16
  19. X-Kong-Request: baxiang
  20. X-Kong-Upstream-Latency: 520
  21. X-Kong-Upstream-Status: 200
  22. X-Powered-By: mockbin

插件问题
go-pluginserver terminated when using custom Go plugin #22
nginx 的用户需要设置 https://github.com/Kong/go-pluginserver/issues/22

编译错误

  1. 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
  2. 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)
  3. stack traceback:
  4. [C]: in function 'ipairs'
  5. /usr/local/share/lua/5.1/kong/db/dao/plugins/go.lua:480: in function 'get_plugin'
  6. /usr/local/share/lua/5.1/kong/db/dao/plugins/go.lua:519: in function 'load_plugin'
  7. /usr/local/share/lua/5.1/kong/db/dao/plugins.lua:159: in function 'load_plugin_handler'
  8. /usr/local/share/lua/5.1/kong/db/dao/plugins.lua:243: in function 'load_plugin'
  9. /usr/local/share/lua/5.1/kong/db/dao/plugins.lua:295: in function 'load_plugin_schemas'
  10. /usr/local/share/lua/5.1/kong/init.lua:467: in function 'init'
  11. init_by_lua:3: in main chunk
  12. Run with --v (verbose) or --vv (debug) for more details

kong 配置

  1. database = postgres # Determines which of PostgreSQL or Cassandra
  2. # this node will use as its datastore.
  3. # Accepted values are `postgres`,
  4. # `cassandra`, and `off`.
  5. pg_host = 127.0.0.1 # Host of the Postgres server.
  6. pg_port = 5438 # Port of the Postgres server.
  7. pg_timeout = 5000 # Defines the timeout (in ms), for connecting,
  8. # reading and writing.
  9. pg_user = kong # Postgres user.
  10. pg_password = baxiang2020 # Postgres user's password.
  11. pg_database = kong # The database name to connect to.
  12. admin_listen = 0.0.0.0:8001
  13. nginx_user= root
  14. plugins = bundled,go-hello
  15. go_plugins_dir = /usr/local/share/go/1.14/kong/plugins

Docker file

  1. ## Compile Go files
  2. FROM golang:1.13.5 as compiler
  3. RUN git clone https://github.com/Kong/go-plugins /usr/src/go-plugins
  4. RUN cd /usr/src/go-plugins && make
  5. RUN git clone https://github.com/Kong/go-pluginserver /usr/src/go-pluginserver
  6. RUN cd /usr/src/go-pluginserver && make
  7. ## Build custom Kong image
  8. FROM kong:2.0-ubuntu
  9. LABEL maintainer="HP GSB Solutions"
  10. # Copy Kong configuration
  11. COPY conf/kong.conf /etc/kong/kong.conf
  12. # Copy Go files
  13. COPY --from=compiler /usr/src/go-plugins/*.so /usr/local/go-plugins/
  14. RUN chmod -R 755 /usr/local/go-plugins/*.so
  15. # Copy Go Plugin Server executable
  16. COPY --from=compiler /usr/src/go-pluginserver/go-pluginserver /usr/local/go-pluginserver
  17. RUN chmod -R 755 /usr/local/go-pluginserver

配置文件

conf/kong.conf:

  1. plugins=bundled,go-hello
  2. go_plugins_dir=/usr/local/go-plugins
  3. 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