前言
Apache APISIX 是国人开源的微服务 API 网关,目前发展势头旺盛,性能强劲,不知道此为何物的可以去 GITHUB 官方仓库文档里看看详细介绍。
系统版本
[root@fjr-ofckv-73-94 supdev]# cat /etc/issue
CentOS release 6.10 (Final)
安装套件
注:这里源码安装的目录都在 /www/down 内
- OpenResty
- Etcd
- luarocks
- Apache APISIX
- nodejs
- yarn
- incubator-apisix-dashboard
OpenResty
因为 APISIX 是基于 OpenResty 开发,因此这个是必须安装的,而且最好安装二进制版本,否则会出现一些稀奇古怪的问题,比如 OpenSSL 版本低,但升级 OpenSSL 后,又出现相关 lua 模块少打开控制参数之类的费时费力的问题。
去官网下载二进制版本即可,不用满网络搜索。
cd /etc/yum.repos.d/
wget https://openresty.org/package/centos/openresty.repo
yum -y install openresty
安装成功后,会把相关的文件放入 /usr/local/openresty/
目录内,这时看一看下安装版本。
[root@fjr-ofckv-73-94 openresty]# openresty -v
nginx version: openresty/1.15.8.3
Etcd
Etcd 在 APISIX 中用来存储相关配置,并且 Etcd 天然支持分布式,二者搭配可谓是强强联合。
这个直接去官方下,除非有梯子,否则那叫一个慢,这里推荐一个华为开源镜像站,搜索 etcd,选择最新版本,下载安装即可。
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 版本。
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。
yum install -y luarocks lua-devel
Apache APISIX
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
然后依次执行以下命令
# 安装 Lua 模块依赖,这一步耗时最长
make deps
# 初始化运行环境
make init
# 启动
make run
也可以运行 make help
查看相关命令帮助。
[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 也是一种尝试。
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 二进制版本
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
查看版本
[root@fjr-ofckv-73-94 node-v11.0.0-linux-x64]# node -v
v11.0.0
yarn
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
查看版本
[root@fjr-ofckv-73-94 node-v11.0.0-linux-x64]# yarn -v
1.22.4
dashboard
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,再开启,再试试,应该就成了。
牛刀小试
安装好后,来个 demo 体验把。
动态 Upstream
使用过 Nginx 配置过 upstream 都晓得,每在 upstream 里添加或摘取个服务地址,都需要重启下,很不方便,若是能动态添加就便利多了,也就是通过心跳检测 upstream 里那组服务,若有服务故障,摘取掉,若流量增加,那么就添加。这在 APISIX 都不叫事,依次点击左侧 Upstream 菜单,点击添加按钮,进入配置页面。
其中,Type 对应的 roundrobin 为轮询模式,待会儿设置好后,在浏览器上便会把请求依次转发到 80 和 8081 端口。
当然,前提是在服务器上启动相关配置。这里使用在 openresty 里添加,默认启动 openresty 服务,80 端口就可以访问,这里只需添加 8081 端口的配置。
server {
listen 8081;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("Hello World")
}
}
}
启动 openresty,访问 8081 端口,便会返回 Hello World
。
[root@fjr-ofckv-73-94 dashboard]# curl 127.0.0.1:8081
Hello World
添加路由
好了,动态路由配置好了,现在需要在路由那应用了。点击左侧的 Routes 菜单,再点击添加按钮进入配置页面。
其中 URIs 里的 index.html 为对外暴露的访问地址,Methods 默认为 get 访问。
好了,动态 Upstream 和 路由都配置好了,这时只需在浏览器地址栏里输入 http://ip:8090/index.html,每刷新一次,内容就会变一次,因为后台设置了轮询模式。