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)最新教程

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,928评论 6 509
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,748评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,282评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,065评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,101评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,855评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,521评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,414评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,931评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,053评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,191评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,873评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,529评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,074评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,188评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,491评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,173评论 2 357

推荐阅读更多精彩内容