概述

Apache APISIX is a dynamic, real-time, high-performance API gateway.
APISIX provides rich traffic management features such as load balancing, dynamic upstream, canary release, circuit breaking, authentication, observability, and more.

APISIX 是一个云原生、高性能、可扩展的微服务 API 开源网关,基于OpenResty(Nginx+Lua)和etcd来实现,对比传统的API网关,具有动态路由和热插件加载的特点。系统本身自带前端,可以手动配置路由、负载均衡、限速限流、身份验证等插件,操作方便。APISIX是用Lua语言开发,语言相对简单,容易上手,同时可以按自己的需求进行系统的二次开发以及开发自己的插件。

APISIX架构

功能

APISIX的功能有很多,包括动态路由、url重写、动态上游、IP黑白名单、A/B测试、灰度发布、限速限流、监控报警、健康检查等等,本文只介绍几个比较常用的功能,其他功能具体可以查看APISIX的官方文档说明。

服务热启动功能

使用过Nginx的同学都会遇到过这种情况,在修改nginx.conf后需要重启nginx服务修改才会生效,重启时间虽然短暂,但难免会有一段服务不可用时间。当然,可以通过其他方式进行规避服务不可用。APISIX的服务热启动通过在路由、Service、Upstreams等插件中动态的修改配置即可,并不需要再重启APISIX服务来使修改生效。

热插件功能

不再需要在nginx.conf文件中编写复杂的规则代码来实现需求,通过使用自带的路由、Upstreams等插件,在前端页面中添加所需要的规则即可实现。下文应用会详细介绍。目前通过插件可以实现uri重写、根据请求信息中的内容实现路由跳转等等。也可以根据自己的需求开发符合自己需求的插件。

动态负载均衡

通过Upstreams插件,可以实现基于权重的roundrobin和chash负载均衡。

数据集群

APISIX支持etcd集群,通过etcd集群增强了系统的可用性,大大减小了故障损失。

监控

APISIX外接第三方prometheus监控系统,提供符合prometheus数据格式的监控指标数据。Prometheus 是由 SoundCloud 开源监控告警解决方案,已经比较成熟完善。

安装

官网的How to build Apache APISIX中说明了可以使用 RPM Repository, RPM package, Docker, Helm Chart, and source release package.等方式来构建安装APISIX。本次我使用RPM和YUM方式来构建部署APISIX。

ETCD

[root@LWS-NODE-JP software]# yum -y install golang
[root@LWS-NODE-JP software]#  wget https://github.com/etcd-io/etcd/releases/download/v3.5.1/etcd-v3.5.1-linux-amd64.tar.gz
[root@LWS-NODE-JP software]# unzip v3.5.0.zip
[root@LWS-NODE-JP software]# mv etcd-3.5.0/ /usr/local/etcd
[root@LWS-NODE-JP software]# vim /usr/lib/systemd/system/etcd.service 
[Unit]
Description=Etcd Server
After=network.target network-online.target
Wants=network-online.target

[Service]
Type=notify
EnvironmentFile=-/usr/local/etcd/etcd.conf
WorkingDirectory=/usr/local/etcd/
ExecStart=/usr/local/bin/etcd
Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

[root@LWS-NODE-JP software]# systemctl daemon-reload
[root@LWS-NODE-JP software]# systemctl restart etcd

Openresty

Openresty可以使用yum也可以使用source code安装,我本地已经编译安装好Openresty了。
(注:不要diy nginx的版本名,否则apisix会报错找不到openresty。)

编译安装

wget https://openresty.org/download/openresty-1.19.9.1.tar.gz
tar -zxvf openresty-1.19.9.1.tar.gz
cd openresty-1.19.9.1/ && ./configure --user=www --group=www --prefix=/usr/local --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-http_geoip_module --with-openssl=/data/software/openssl-1.1.1l  --with-openssl-opt='enable-weak-ssl-ciphers' --add-module=/data/software/ngx_brotli --add-module=/data/software/ngx_cache_purge && gmake && gmake install

APISIX

yum-config-manager --add-repo https://repos.apiseven.com/packages/centos/apache-apisix.repo
yum info -y apisix
yum --showduplicates list apisix
yum install apisix
apisix init
apisix start
````

如果尚未安装 OpenResty 的官方 RPM 仓库,以下命令可以帮助您自动安装 OpenResty 和 Apache APISIX 的 RPM 仓库。

yum install -y https://repos.apiseven.com/packages/centos/apache-apisix-repo-1.0-1.noarch.rpm



### Dashboard

[root@LWS-NODE-JP software]# yum install -y https://github.com/apache/apisix-dashboard/releases/download/v2.10.0/apisix-dashboard-2.10.0-0.el7.x86_64.rpm
[root@LWS-NODE-JP software]# vim /usr/local/apisix/dashboard/conf/conf.yaml

yamllint disable rule:comments-indentation

conf:
listen:

# host: 127.0.0.1     # the address on which the `Manager API` should listen.
                      # The default value is 0.0.0.0, if want to specify, please enable it.
host: 0.0.0.0  # 设置成0.0.0.0允许所有,生产环境慎用。  # This value accepts IPv4, IPv6, and hostname.
port: 8888   # 自定义端口        # The port on which the `Manager API` should listen.

# ssl:
# host: 127.0.0.1 # the address on which the Manager API should listen for HTTPS.

                      # The default value is 0.0.0.0, if want to specify, please enable it.

# port: 9001 # The port on which the Manager API should listen for HTTPS.
# cert: "/tmp/cert/example.crt" # Path of your SSL cert.
# key: "/tmp/cert/example.key" # Path of your SSL key.

#allow_list: # If we don't set any IP list, then any IP access is allowed by default.

#- 127.0.0.1           # The rules are checked in sequence until the first match is found.
#- ::1               # In this example, access is allowed only for IPv4 network 127.0.0.1, and for IPv6 network ::1.
#- all                # It also support CIDR like 192.168.1.0/24 and 2001:0db8::/32

[root@LWS-NODE-JP software]# nohup manager-api -p /usr/local/apisix/dashboard/ & # 生产环境建议注册到systemctl管理

使用http://YOU-IP-ADDRESS:PORT,账密:admin/admin就可以进入到APISIX的Dashboard了。

![APISIX Dashboard][2]


# 参考资料

[ETCD](https://etcd.io/docs/v3.5/install/)
[ETCD-github](https://github.com/etcd-io/etcd/releases/)
[APISIX-github](https://github.com/apache/apisix-dashboard/releases/tag/v2.9.0)
[openresty-download](http://openresty.org/en/download.html)
[APISIX](https://apisix.apache.org/zh/docs/apisix/getting-started)


  [1]: https://static.lianjiale.com//wp-content/uploads/2021/12/14/apixis-jiagou.png
文章目录