APISIX快速入门

1.安装

1.1 前置条件


1.2 安装依赖

1.2.1 CentOS 7

# install etcd
wget https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz
tar -xvf etcd-v3.4.13-linux-amd64.tar.gz && \
    cd etcd-v3.4.13-linux-amd64 && \
    sudo cp -a etcd etcdctl /usr/bin/

# add OpenResty source
yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

# install OpenResty and some compilation tools
yum install -y openresty curl git gcc openresty-openssl111-devel unzip pcre pcre-devel

# install LuaRocks
curl https://raw.githubusercontent.com/apache/apisix/master/utils/linux-install-luarocks.sh -sL | bash -

# start etcd server
nohup etcd &

1.2.2 Mac OSX

# install OpenResty, etcd and some compilation tools
brew install openresty/brew/openresty luarocks lua@5.1 etcd curl git pcre

# start etcd server
brew services start etcd


1.3 安装APISIX

1.3.1 rpm安装

yum install -y https://github.com/apache/apisix-dashboard/releases/download/v2.6/apisix-dashboard-2.6-0.x86_64.rpm

1.3.2 源码安装

mkdir apisix-2.6
wget https://downloads.apache.org/apisix/2.6/apache-apisix-2.6-src.tgz
tar zxvf apache-apisix-2.6-src.tgz -C apisix-2.6

# Switch to the apisix-2.6 directory
cd apisix-2.6
# Create dependencies
make deps


1.4 服务管理

apisix help

1.4.1 初始化nginx 和etcd

# initialize NGINX config file and etcd
apisix init

1.4.2 启动服务

# start Apache APISIX server
apisix start

1.4.3 停止服务

# stop Apache APISIX server gracefully
apisix quit

# stop Apache APISIX server immediately
apisix stop

1.4.4 Centos7启动

systemctl start apisix
systemctl stop apisix
systemctl status apisix

如出现 Unit apisix.servic could not be found,就添加systemd服务。

/usr/lib/systemd/system/apisix.servic

[Unit]
Description=apisix
Conflicts=apisix.service
After=network-online.target

[Service]
Type=forking
WorkingDirectory=/usr/local/apisix
ExecStart=/usr/bin/apisix start
ExecStop=/usr/bin/apisix stop
ExecReload=/usr/bin/apisix reload


1.5 api-dashboard安装

yum install -y https://github.com/apache/apisix-dashboard/releases/download/v2.6/apisix-dashboard-2.6-0.x86_64.rpm
cd /usr/local/apisix/dashboard/conf
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

conf:
 listen:
   host: 127.0.0.1     # `manager api` listening ip or host name
   port: 9000          # `manager api` listening port
 allow_list:           # If we don't set any IP list, then any IP access is allowed by 
   default.
   - 127.0.0.1/24

修改host为0.0.0.0并注释掉allow_list

conf:
  listen:
    host: 0.0.0.0     # `manager api` listening ip or host name
    port: 9000          # `manager api` listening port  
#allow_list:           # If we don't set any IP list, then any IP access is allowed by 
  #  default.
  #  - 127.0.0.1/24

启动

nohup manager-api -p /usr/local/apisix/dashboard/ &

直接访问:http://106.14.134.210:9000/ admin/admin

图片.png


2. 动态负载均衡

2.1 创建二个后端服务

http_server01.php

<?php

$http = new Swoole\Http\Server('0.0.0.0', 9501);

$http->on('Request', function ($request, $response) {
    $response->header('Content-Type', 'text/html; charset=utf-8');
    $response->end('<h1>Hello 9501 server01 . #' . rand(1000, 9999) . '</h1>');
});

$http->start();

http_server02.php

<?php

$http = new Swoole\Http\Server('0.0.0.0', 9502);

$http->on('Request', function ($request, $response) {
    $response->header('Content-Type', 'text/html; charset=utf-8');
    $response->end('<h1>Hello 9502 server02 . #' . rand(1000, 9999) . '</h1>');
});

$http->start();

2.2 测试是否单独正常访问

[root@iZuf64x4zihal2tc46i6d2Z apisix]# curl 127.0.0.1:9501
<h1>Hello 9501 server01 . #5659</h1>

[root@iZuf64x4zihal2tc46i6d2Z apisix]# curl 127.0.0.1:9502
<h1>Hello 9502 server02 . #9030</h1>

2.3 概念

一个微服务可以通过 APISIX 的路由、服务、上游和插件等多个实体之间的关系进行配置。 Route(路由)与客户端请求匹配,并指定它们到达 APISIX 后如何发送到 Upstream(上游,后端 API 服务)。 Service(服务)为上游服务提供了抽象。因此,您可以创建单个 Service 并在多个 Route 中引用它。

2.4 创建 APISIX Upstream(上游,后端 API 服务)

APISIX Upstream,是虚拟主机抽象,对给定的多个服务节点按照配置规则进行负载均衡。它根据配置规则在给定的一组服务节点上执行负载平衡。 因此,单个上游配置可以由提供相同服务的多个服务器组成。每个节点将包括一个 key(地址/ip:port)和一个 value (节点的权重)。 服务可以通过轮询或一致哈希(cHash)机制进行负载平衡。

配置路由时,可以直接设置 Upstream 信息,也可以使用服务抽象来引用 Upstream 信息。

在 APISIX 控制台的「Upstream」菜单中,创建一个 APISIX Upstream。如下图所示:

图片.png
图片.png

2.5 创建 APISIX Route

APISIX Route,字面意思就是路由,通过定义一些规则来匹配客户端的请求,然后根据匹配结果加载并执行相应的 插件,并把请求转发给到指定 Upstream。

图片.png
图片.png

2.6 浏览器访问

三次访问结果,按照轮训规则1:2来的。


图片.png
图片.png
图片.png

3. 限流限速

APISIX 内置了常用的三个限流限速插件:

  • limit-count:基于“固定窗口”的限速实现。
  • limit-req:基于漏桶原理的请求限速实现。
  • limit-conn:限制并发请求(或并发连接)。
图片.png

3.1 配置 limit-req 插件

我们来演示使用 limit-req 插件,毕竟基于漏桶的限流算法,是目前较为常用的限流方式。

漏桶算法(Leaky Bucket)是网络世界中流量整形(Traffic Shaping)或速率限制(Rate Limiting)时经常使用的一种算法,它的主要目的是控制数据注入到网络的速率,平滑网络上的突发流量。
漏桶算法提供了一种机制,通过它,突发流量可以被整形以便为网络提供一个稳定的流量。

图片.png
图片.png

图片.png

3.1.1 属性说明

  • rate:指定的请求速率(以秒为单位),请求速率超过 rate 但没有超过 (rate + brust)的请求会被加上延时

  • burst:请求速率超过 (rate + brust)的请求会被直接拒绝

  • rejected_code:当请求超过阈值被拒绝时,返回的 HTTP 状态码

  • key:是用来做请求计数的依据,当前接受的 key 有:"remote_addr"(客户端 IP 地址), "server_addr"(服务端 IP 地址), 请求头中的"X-Forwarded-For" 或 "X-Real-IP"。

上述截图配置含义:限制了每秒请求速率为 10大于 10 小于 20的会被加上延时,速率超过 30就会被拒绝。

快速访问返回包含 503 返回码的响应头:

HTTP/1.1 503 Service Temporarily Unavailable
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Server: APISIX web server

<html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body>
<center><h1>503 Service Temporarily Unavailable</h1></center>
<hr><center>openresty</center>
</body>
</html>

3.2 配置 limit-conn插件

Apisix 的限制并发请求(或并发连接)插件。

图片.png
图片.png

3.2.1 属性说明

  • conn: 允许的最大并发请求数。 超过这个比率的请求(低于“ conn” + “ burst”)将被延迟以符合这个阈值。

  • burst: 允许延迟的过多并发请求(或连接)的数量。

  • default_conn_delay: 默认的典型连接(或请求)的处理延迟时间。

  • key: 用户指定的限制并发级别的关键字,可以是客户端IP或服务端IP。
    例如:可以使用主机名(或服务器区域)作为关键字,以便限制每个主机名的并发性。 另外,我们也可以使用客户端地址作为关键字,这样我们就可以避免单个客户端用太多的并行连接或请求淹没我们的服务。
    现在接受以下关键字: “remote_addr”(客户端的 IP),“server_addr”(服务器的 IP),请* 求头中的“ X-Forwarded-For/X-Real-IP”。

  • rejected_code: 当请求超过阈值时返回的 HTTP状态码, 默认值是503。

4. 身份验证

APISIX 内置了常用的四个身份验证插件:

  • key-auth:基于 Key Authentication 的用户认证。
  • JWT-auth:基于 JWT (JSON Web Tokens) Authentication 的用户认证。
  • basic-auth:基于 basic auth 的用户认证。
图片.png

4.1 配置 JWT-auth 插件

在 APISIX 控制台的「Consumer」菜单中,创建一个 APISIX Consumer,Consumer 是某类服务的消费者,需与用户认证体系配合才能使用。添加 JWT Authentication 到一个 service 或 route。 然后 consumer 将其密钥添加到查询字符串参数、请求头或 cookie 中以验证其请求。

4.1.1 服务,消费者配置

图片.png
图片.png

图片.png

关键的地方

图片.png

设置key,secret参数,可自定义查看官方文档:https://apisix.apache.org/docs/apisix/plugins/jwt-auth/

图片.png

获取访问的密钥token

curl http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=gangan


[root@iZuf64x4zihal2tc46i6d2Z apisix]# curl http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=gangan -i
HTTP/1.1 200 OK
Date: Tue, 28 Sep 2021 02:53:29 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: APISIX/2.5

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MzI4ODQwMDksImtleSI6ImdhbmdhbiJ9.qYAv87wdgRJ56EMtUpOSNYEfW2wXvO2qCshIi8DSBE8

4.1.2 缺少token

{"message":"Missing JWT token in request"}

4.1.3 token过期

{"message":"'exp' claim expired at Mon, 27 Jul 2020 09:20:37 GMT"}

4.1.4 正常访问

图片.png

参考资料

https://github.com/apache/apisix/blob/master/docs/en/latest/install-dependencies.md

https://github.com/apache/apisix/blob/master/docs/en/latest/how-to-build.md

https://apisix.apache.org/docs/apisix/plugins/jwt-auth/

网关APISIX实战

APISIX 入门(国产微服务网关)

apisix安装过程和坑

centos安装api-six以及可视化工具(apisix-dashboard)最新教程

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容