- 由于mac系统的原因,Mac无法在本地直接调试apisix-runner。详细原因参考链接github.com/apache/apis…
一、搭建apisix本地环境
本地推荐使用docker-compose来搭建apisix环境,使用apisix-docker项目。详细内容可以参考官方文档。
二、本地插件开发和构建
本地通过apisix-go-plugin-runner项目来开发插件,具体操作可以参考文档
Apisix通过jwt-token + 自定义插件实现身份认证。
需要注意的是在构建go-runner的时候,根据最终的需要来构建对应的软件包。
例如:构建在linux运行的go-runner,在main.go目录下执行命令
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
三、apisix容器中启动插件
-
构建go-runner
将构建好的go-runner复制到apisix docker-compose apisix_conf目录
cd cmd/go-runner
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
cp ./go-runner ./apisix_conf/go-runner
#如果runner中用到了mysql等链接配置,也需要将配置文件复制到容器挂载目录
cp -rf ../../configs /Users/qujianfei/gitProject/git-sample/apisix/apisix-docker/example/apisix_conf
-
更新docker-compose apisix_config,挂载go-runner到容器
services:
apisix:
##image: apache/apisix:${APISIX_IMAGE_TAG:-3.8.0-debian}
image: reg.axt.com/library/apisix:3.8.0-debian
restart: always
volumes:
- ./apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml:ro
- ./apisix_conf/go-runner:/usr/local/apisix/go-runner
- ./apisix_conf/configs:/usr/local/apisix/configs
depends_on:
- etcd
##network_mode: host
ports:
- "9180:9180/tcp"
- "9080:9080/tcp"
- "9091:9091/tcp"
- "9443:9443/tcp"
- "9092:9092/tcp"
networks:
apisix:
-
更新apisix config.yaml添加插件调试配置
更新docker-compose apisix_config/config.yaml文件
apisix:
node_listen: 9080 # APISIX listening port
enable_ipv6: false
enable_control: true
control:
ip: "0.0.0.0"
port: 9092
deployment:
admin:
allow_admin: # https://nginx.org/en/docs/http/ngx_http_access_module.html#allow
- 0.0.0.0/0 # We need to restrict ip access rules for security. 0.0.0.0/0 is for test.
admin_key:
- name: "admin"
key: edd1c9f034335f136f87ad84b625c8f1
role: admin # admin: manage all configuration data
- name: "viewer"
key: 4054f7cf07e344346cd3f287985e76a2
role: viewer
etcd:
host: # it's possible to define multiple etcd hosts addresses of the same etcd cluster.
- "http://etcd:2379" # multiple etcd address
prefix: "/apisix" # apisix configurations prefix
timeout: 30 # 30 seconds
plugin_attr:
prometheus:
export_addr:
ip: "0.0.0.0"
port: 9091
ext-plugin:
path_for_test: /usr/local/apisix/runner.sock
-
apisix容器内启动go-runner
APISIX_LISTEN_ADDRESS=unix:/usr/local/apisix/runner.sock ./go-runner run
-
创建router和调用router使用自定义插件
在host机器上创建router,并且访问router path验证插件功能
➜ ~ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/ams/amcenter",
"plugins": {
"ext-plugin-pre-req": {
"conf": [
{ "name": "say", "value":"{"body":"hello"}"}
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"192.168.101.23:31102": 1
}
}
}'
{"key":"/apisix/routes/1","value":{"uri":"/ams/amcenter","update_time":1713927880,"upstream":{"type":"roundrobin","nodes":{"192.168.101.23:31102":1},"hash_on":"vars","scheme":"http","pass_host":"pass"},"priority":0,"create_time":1713927880,"id":"1","plugins":{"ext-plugin-pre-req":{"conf":[{"name":"say","value":"{"body":"hello"}"}],"allow_degradation":false}},"status":1}}
➜ ~ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X GET
{"key":"/apisix/routes/1","createdIndex":30,"value":{"create_time":1713927880,"priority":0,"upstream":{"hash_on":"vars","nodes":{"192.168.101.23:31102":1},"scheme":"http","type":"roundrobin","pass_host":"pass"},"id":"1","status":1,"update_time":1713927880,"plugins":{"ext-plugin-pre-req":{"conf":[{"value":"{"body":"hello"}","name":"say"}],"allow_degradation":false}},"uri":"/ams/amcenter"},"modifiedIndex":30}
➜ ~ curl http://127.0.0.1:9080/ams/amcenter
{"timestamp":"2024-04-24T11:16:30.894+08:00","status":200,"data":{"id":"ba05cd6a68","name":"troyqu","root":true,"accountId":1000089,"email":"troyqu@qq.com","companyInfo":{}},"path":"/ams/amcenter","method":"GET","ms":99,"message":"个人中心成功"}%
访问请求插件输出日志
apisix@dbf310ff9389:/usr/local/apisix$ APISIX_LISTEN_ADDRESS=unix:/usr/local/apisix/runner.sock ./go-runner run
2024-04-24T03:16:04.046Z INFO plugin/plugin.go:73 register plugin fault-injection
2024-04-24T03:16:04.046Z INFO plugin/plugin.go:73 register plugin limit-req
2024-04-24T03:16:04.046Z INFO plugin/plugin.go:73 register plugin request-body-rewrite
2024-04-24T03:16:04.047Z INFO plugin/plugin.go:73 register plugin response-rewrite
2024-04-24T03:16:04.047Z INFO plugin/plugin.go:73 register plugin say
2024-04-24T03:16:04.047Z WARN server/server.go:192 conf cache ttl is 1h12m0s
2024-04-24T03:16:04.048Z WARN server/server.go:200 listening to /usr/local/apisix/runner.sock
2024-04-24T03:16:30.589Z INFO server/server.go:115 Client connected (unix)
2024-04-24T03:16:30.589Z INFO server/server.go:115 Client connected (unix)
2024-04-24T03:16:30.589Z INFO server/server.go:131 receive rpc type: 1 data length: 120
2024-04-24T03:16:30.589Z INFO plugin/conf.go:98 prepare conf for plugin say
2024-04-24T03:16:30.592Z INFO server/server.go:131 receive rpc type: 2 data length: 228
2024-04-24T03:16:30.593Z INFO plugin/plugin.go:120 run plugin say
2024-04-24T03:16:30.593Z INFO plugins/say.go:70 request header%!(EXTRA *http.Header=&{map[Account-Id:[100000800000] User-Id:[ba7ee581842a4a378f8]] map[Accept:[*/*] Host:[127.0.0.1:9080] User-Agent:[curl/7.88.1]] map[]})
相关文档
apisix.apache.org/zh/docs/api…
apisix.apache.org/zh/docs/api…