最近电脑更新,换了M1的,所有工作从新开始,意味着APISIX插件开发之路中的绊脚石——单测环境筹备,也要从新筹备。

因为降级了芯片和最新的macOS零碎(12.2.1),导致过程并没有设想中的顺利,之前没遇到过的坑,一个个的裸露进去了,这里做一个分享和BackUp。

还有想吐槽一下APISIX官网的文档,在对于单元测试和插件编写方面的内容,输入不是特地的多,而且散落各处,着实给开发人员带来了麻烦。

以下的操作都是基于APISIX源码下操作(https://github.com/apache/apisix),写该文时版本为V2.12.1。

失常装置流程

  1. 装置各种环境依赖
    # install OpenResty, etcd and some compilation tools    brew install openresty/brew/openresty luarocks lua@5.1 etcd curl git pcre openldap cpanminus    # start etcd server    brew services start etcd

官网最新源码中提供了脚本,位于utils/install-dependencies.sh。间接执行这个脚本就能够对上方的依赖进行装置和启动。

官网文档

  1. 批改环境变量

结合实际装置状况批改.bash_profile文件,并别忘了 source ~/.bash_profile

OPENRESTY_HOME=/usr/local/openrestyPATH=$OPENRESTY_HOME/nginx/sbin:$OPENRESTY_HOME/bin:$PATHexport OPENRESTY_HOME
  1. 下载APISIX源码
git clone https://github.com/apache/apisix.git
  1. 源码环境下解决
# 源码根目录下git clone https://github.com/iresty/test-nginx.gitrm -rf test-nginx/.gitsudo cpanm --notest Test::Nginx IPC::Run > build.log 2>&1 || (cat build.log && exit 1)export PERL5LIB=.:$PERL5LIBmake deps

我看官网举荐应用 make deps ENV_LUAROCKS_SERVER=https://luarocks.cn,但我试了,并没有起效,仍旧应用 https://luarocks.org,起初我通过批改luarocks配置文件 ~/.luarocks/config-5.1.lua ,失效了,大家也能够本人试试,在文章下方做个反馈。

APISIX文档

rocks_servers = {    "https://luarocks.cn"}

因为github的网络起因,make deps过程可能不是一帆风顺,有点急躁多试几次就能够了。

  1. 下载 toolkit 文件

t/目录下,执行

git clone https://github.com/api7/test-toolkit toolkitrm -rf toolkit/.git

这里须要重命名为toolkit,否则援用找不到。

这里如果不下载这个源码会导致,toolkit.json 找不到的问题呈现。

https://github.com/apache/api...

  1. 启动APISIX服务(非必须,能够依据理论状况)

自己是通过Docker形式启动

git clone https://github.com/apache/apisix-docker.gitcd apisix-docker/compose/docker-compose -f docker-compose.yaml -p docker-apisix up -d

这里须要留神,在下面咱们曾经启动了etcd,这里也会启动etcd,所以只有抉择其中一个就能够了。

  1. 测试一下

在源码根目录下,执行

prove -Itest-nginx/lib -r t/plugin/limit-conn.t

失常状况下这样子就能够启动了。

但事实没有这么顺利,而且下面的流程曾经是我采坑的后果了。

问题与解决方案

最新版本下无奈应用 brew 装置 openresty

% brew install openresty/brew/openrestyRunning `brew update --preinstall`...==> Auto-updated Homebrew!Updated 1 tap (homebrew/core).==> Updated FormulaeUpdated 10 formulae.==> Tapping openresty/brewCloning into '/opt/homebrew/Library/Taps/openresty/homebrew-brew'...remote: Enumerating objects: 2866, done.remote: Counting objects: 100% (34/34), done.remote: Compressing objects: 100% (28/28), done.remote: Total 2866 (delta 22), reused 10 (delta 6), pack-reused 2832Receiving objects: 100% (2866/2866), 627.61 KiB | 638.00 KiB/s, done.Resolving deltas: 100% (1610/1610), done.Error: Invalid formula: /opt/homebrew/Library/Taps/openresty/homebrew-brew/Formula/php-session-nginx-module.rbphp-session-nginx-module: Calling bottle :unneeded is disabled! There is no replacement.Please report this issue to the openresty/brew tap (not Homebrew/brew or Homebrew/core):  /opt/homebrew/Library/Taps/openresty/homebrew-brew/Formula/php-session-nginx-module.rb:8

官网的解决方案是将brew版本。https://github.com/openresty/...

而我抉择手动装置openresty

tar -xzvf openresty-VERSION.tar.gz// openresty-VERSION/ 目录./configure --prefix=/usr/local/openresty --with-http_addition_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module --with-http_v2_module --with-pcre=/Users/demo/installapps/pcre-8.45 --with-openssl=/Users/demo/installapps/opensslmakemake install

装置 openresty 须要依赖pcre和openssl,所以这个咱们也得手动装置

  • openssl:https://blog.csdn.net/qyee16/...
  • pcre:https://www.cnblogs.com/Jorda...

这里须要留神的是pcre要应用1,不要应用2,否则会有如下问题

src/core/ngx_regex.h:15:10: fatal error: 'pcre.h' file not found#include <pcre.h>^~~~~~~~1 error generated.make[1]: *** [objs/src/core/nginx.o] Error 1make: *** [install] Error 2

参考文献

不要遗记批改环境变量哦。

make deps 时找不到门路问题

能够通过应用ln -s 软链解决。

nginx报错

跑单测的时候,报如下谬误

nginx: [emerg] "listen" directive is not allowed here in /Users/gjason/project/apisix-all/apisix/t/servroot/conf/nginx.conf:188

可能是数据格式谬误导致 nginx.conf配置谬误。

也可能是Nginx没有装置好,比方短少下面提到的pcre和openssl导致的。