kong学习笔记(1)-插件开发-Hello-World

日期:2019-8-14 9:13:52
作者:ack
访问:2681
环境
1. kong 1.21
2. luarock 3.1.3
3. idea
插件目录
kong插件目录规则:
kong.plugins.<plugin_name>.<module_name>(要求对lua模块熟悉)
plugin_name :这个是插件的名称
module_name :是插件中模块名称
我们要开发的Hello-World例子目录规则如下:(具体目录规则参考官网https://docs.konghq.com/1.2.x/plugin-development/file-structure/)

假设我们插件存放的位置为:/root/test
创建我们的hello-world
mkdir -p /root/test/kong/plugins/hello-world
创建好文件夹后,下载hello-world例子(https://github.com/brndmg/kong-plugin-hello-world/tree/master/src)
将下载后,src下的所有.lua文件复制到/root/test/kong/plugins/hello-world
到这里就完成了。

插件安装
这里不用luarocks安装直接手动安装
1、配置插件所在目录
找到启动kong的配置文件kong.conf
修改lua_package_path,将插件文件添加到这里
#lua_package_path = ./?.lua;./?/init.lua;
修改为:
lua_package_path = ./?.lua;./?/init.lua;/root/test?.lua;;
2、告诉kong加载插件
#plugins = bundled
修改为:
plugins = bundled,hello-world

配置日志
日志的配置在kong.conf
log_level = debug
#log_level = notice # Log level of the Nginx server. Logs are
# found at `<prefix>/logs/error.log`.
# See http://nginx.org/en/docs/ngx_core_module.html#error_log for a list
# of accepted values.
proxy_access_log = /data/logs/kong/proxy_access.log
#proxy_access_log = logs/access.log # Path for proxy port request access
# logs. Set this value to `off` to
# disable logging proxy requests.
# If this value is a relative path,
# it will be placed under the
# `prefix` location.
proxy_error_log = /data/logs/kong/error.log
#proxy_error_log = logs/error.log # Path for proxy port request error
# logs. The granularity of these logs
# is adjusted by the `log_level`
# property.
admin_access_log = /data/logs/kong/admin_access.log
#admin_access_log = logs/admin_access.log # Path for Admin API request access
# logs. Set this value to `off` to
# disable logging Admin API requests.
# If this value is a relative path,
# it will be placed under the
# `prefix` location.
admin_error_log = /data/logs/kong/admin_error.log
#admin_error_log = logs/error.log # Path for Admin API request error
# logs. The granularity of these logs
# is adjusted by the `log_level`
# property.

请对照修改相应部分
验证插件
创建一个service
curl -i -X POST --url http://127.0.0.1:8001/services/ --data 'name=tdt-service' --data 'url=http://www.tianditu.gov.cn/'
为服务指定路由
curl -i -X POST --url http://127.0.0.1:8001/services/tdt-service/routes  --data 'paths[]=/tdt'
添加我们的插件
curl -i -X POST --url http://127.0.0.1:8001/services/tdt-service/plugins/ --data 'name=hello-world'
访问路由
curl -i -X GET  --url http://127.0.0.1:8000/tdt
查看日志
查看日志是有 Helloworld输出
日志的配置在kong.conf