安装 API 网关 APISIX

前言

Apache APISIX 是国人开源的微服务 API 网关,目前发展势头旺盛,性能强劲,不知道此为何物的可以去 GITHUB 官方仓库文档里看看详细介绍。

系统版本

1
2
[root@fjr-ofckv-73-94 supdev]# cat /etc/issue
CentOS release 6.10 (Final)

安装套件

注:这里源码安装的目录都在 /www/down 内

  1. OpenResty
  2. Etcd
  3. luarocks
  4. Apache APISIX
  5. nodejs
  6. yarn
  7. incubator-apisix-dashboard

OpenResty

因为 APISIX 是基于 OpenResty 开发,因此这个是必须安装的,而且最好安装二进制版本,否则会出现一些稀奇古怪的问题,比如 OpenSSL 版本低,但升级 OpenSSL 后,又出现相关 lua 模块少打开控制参数之类的费时费力的问题。

去官网下载二进制版本即可,不用满网络搜索。

1
2
3
cd /etc/yum.repos.d/
wget https://openresty.org/package/centos/openresty.repo
yum -y install openresty

安装成功后,会把相关的文件放入 /usr/local/openresty/ 目录内,这时看一看下安装版本。

1
2
[root@fjr-ofckv-73-94 openresty]# openresty -v
nginx version: openresty/1.15.8.3

Etcd

Etcd 在 APISIX 中用来存储相关配置,并且 Etcd 天然支持分布式,二者搭配可谓是强强联合。

这个直接去官方下,除非有梯子,否则那叫一个慢,这里推荐一个华为开源镜像站,搜索 etcd,选择最新版本,下载安装即可。

1
2
3
4
wget https://mirrors.huaweicloud.com/etcd/v3.4.7/etcd-v3.4.7-linux-amd64.tar.gz
tar xf etcd-v3.4.7-linux-amd64.tar.gz
cd etcd-v3.4.7-linux-amd64
cp -r etcd etcdctl /usr/bin

由于 APISIX 目前使用的是 V2 版本的信息,而新版本默认是 V3 的信息,因此需要在启动的时候知道 V2 版本。

1
nohup etcd --enable-v2=true &

注:Etcd v3 的客户端使用 gRPC 与 server 进行通信,通信的消息协议使用 protobuf 进行约定,代替了 v2 版本的 HTTP-json 格式,使用二进制替代文 本,更加节省空间。同时 gRPC 使用的是 HTTP/2 协议,同一个连接可以同时处理多个请求,不必像 HTTP1.1 协议中,多个请求需要建立多个连接 。同时,HTTP/2 会对请求的 Header 和请求数据进行压缩编码,常见的有 Header 帧,用于传输 Header 内容,另外就是 Data 帧,来传输正文实体 。客户端可以将多个请求放到不同的流中,然后将这些流拆分成帧的形式进行二进制传输,传输的帧也会有一个编号,因此在一个连接中客 户端可以发送多个请求,减少了连接数,降低了对服务器的压力,二进制的数据传输格式也会是传输速度更快。

luarocks

由于 APISIX 使用的是 Lua 语言开发的,这就势必用到 lua 相关库,因此要用到 luarocks 来安装三方库。一句话,luarocks 之于 Lua,就像 Composer 之于 PHP,Go Modules 之于 Go, Maven 之于 Java。

1
yum install -y luarocks lua-devel

Apache APISIX

1
2
3
wget https://downloads.apache.org/incubator/apisix/1.2/apache-apisix-1.2-incubating-src.tar.gz
tar xf apache-apisix-1.2-incubating-src.tar.gz
cd apache-apisix-1.2-incubating-src

然后依次执行以下命令

1
2
3
4
5
6
7
8
# 安装 Lua 模块依赖,这一步耗时最长
make deps

# 初始化运行环境
make init

# 启动
make run

也可以运行 make help 查看相关命令帮助。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@fjr-ofckv-73-94 apache-apisix-1.2-incubating]# make help
Makefile rules:

    help:             Show Makefile rules.
    deps:             Installation dependencies
    utils:            Installation tools
    lint:             Lint Lua source code
    init:             Initialize the runtime environment
    run:              Start the apisix server
    stop:             Stop the apisix server
    verify:           Verify the configuration of apisix server
    clean:            Remove generated files
    reload:           Reload the apisix server
    install:          Install the apisix
    test:             Run the test case
    license-check:    Check Lua source code for Apache License

至此 APISIX 已成功安装并启动,但官方同时为了易用,基于 vue 开发了个管理界面,需要安装 nodejs、yarn、dashboard。

安装 nodejs

这一步得注意了,别直接用系统提供的 node,版本太低,以致下一步安装 yarn 后,提示 nodejs 版本低。同时也别使用高版本,因为高版本使用的是 gcc 新版本编译的,一旦下载下来,运行 ./bin/node 会出现类似于这种的 Requires: libc.so.6(GLIBC_2.14) 提示。

当然了,升级 gcc 也是一种尝试。

1
2
3
4
5
6
7
8
9
10
11
cd /etc/yum.repos.d
wget http://people.centos.org/tru/devtools-2/devtools-2.repo
yum install devtoolset-2-gcc devtoolset-2-binutils devtoolset-2-gcc-c++

mv /usr/bin/gcc /usr/bin/gcc-4.4.7
mv /usr/bin/g++ /usr/bin/g++-4.4.7
mv /usr/bin/c++ /usr/bin/c++-4.4.7
ln -s /opt/rh/devtoolset-2/root/usr/bin/gcc /usr/bin/gcc
ln -s /opt/rh/devtoolset-2/root/usr/bin/c++ /usr/bin/c++
ln -s /opt/rh/devtoolset-2/root/usr/bin/g++ /usr/bin/g++
gcc --version

下面来安装 nodejs 二进制版本

1
2
3
wget https://nodejs.org/download/release/v11.0.0/node-v11.0.0-linux-arm64.tar.gz
tar xf node-v11.0.0-linux-arm64.tar.gz
ln -sf /www/down/node-v11.0.0-linux-arm64/bin/node /usr/bin/node

查看版本

1
2
[root@fjr-ofckv-73-94 node-v11.0.0-linux-x64]# node -v
v11.0.0

安装 yarn

1
2
3
wget https://github.com/yarnpkg/yarn/releases/download/v1.22.4/yarn-v1.22.4.tar.gz
tar xf yarn-v1.22.4.tar.gz
ln -sf /www/down/yarn-v1.22.4/bin/yarn /usr/bin/yarn

查看版本

1
2
[root@fjr-ofckv-73-94 node-v11.0.0-linux-x64]# yarn -v
1.22.4

安装 dashboard

1
2
3
4
5
6
git clone https://github.com/apache/incubator-apisix-dashboard.git
cd incubator-apisix-dashboard.git
git submodule update --init --recursive
yarn && yarn build:prod # 这一步较耗时

cp -r ./dist/* /www/down/apache-apisix-1.2-incubating/dashboard

至此就完成了安装,在浏览器上输入 http://IP:9080/apisix/dashboard,就能看到如下界面

在这里插入图片描述
无需输入密码,进入后展示

在这里插入图片描述
最后再唠叨下,若是登陆后,页面弹出 401 unauthorized,也别慌,就差临门一脚了,别放弃,重新删掉 dashboard,再安装下载个,再停掉 APISIX,再开启,再试试,应该就成了。