前言
前文介绍了网关apisix的调研,接下来给大家介绍一下实际使用
一、安装
APISIX 在以下操作系统中可顺利安装并做过运行测试,需要注意的是:OpenResty 的版本必须 >= 1.15.8.1:
CentOS 7, Ubuntu 16.04, Ubuntu 18.04, Debian 9, Debian 10, macOS, ARM64 Ubuntu 18.04
安装 APISIX 的步骤:
- 安装运行时依赖:OpenResty 和 etcd,参考依赖安装文档
- 有以下几种方式来安装 Apache APISIX:
- 通过源码包安装;
- 如果你在使用 CentOS 7,可以使用 RPM 包安装;
- 其它 Linux 操作系统,可以使用 Luarocks 安装方式;
- 你也可以使用 Docker 镜像 来安装。
对于我自己,采用的是Docker运行方式。
-
Docker 安装Apisix
1、下载apisix-docker项目
git clone https://github.com/apache/incubator-apisix-docker.git
2、install master branch version, which has latest code
#1、 cd incubator-apisix-docker/ #2、 docker build -t apisix:master-alpine -f alpine/Dockerfile alpine
3、Run etcd server
#3、Run etcd server docker run -it --name etcd-server \ -v `pwd`/example/etcd_conf/etcd.conf.yml:/opt/bitnami/etcd/conf/etcd.conf.yml \ -p 2379:2379 \ -p 2380:2380 \ --env ALLOW_NONE_AUTHENTICATION=yes \ -d bitnami/etcd:3.3.13-r80 ##测试 etcdctl put key-test "Hi Tinywan!" // 设置 OK I have no name!@1d05cea9f08c:/$ etcdctl get key-test // 获取 key-test Hi Tinywan! I have no name!@1d05cea9f08c:/$ etcdctl del key-test // 删除 1 I have no name!@1d05cea9f08c:/$ etcdctl get key-test // 删除 I have no name!@1d05cea9f08c:/$
进入容器查看是否已经开启v2协议
docker exec -it etcd-server bash $ etcd --help |grep enable-v2 usage: etcd [flags] start an etcd server etcd --version show the version of etcd etcd -h | --help show the help information about etcd etcd --config-file path to the server configuration file etcd gateway run the stateless pass-through etcd TCP connection forwarding proxy etcd grpc-proxy run the stateless etcd v3 gRPC L7 reverse proxy --enable-v2 'true' --experimental-enable-v2v3 ''
--enable-v2 'true'
表示已经开启v2协议。原因:APISIX 目前仅支持 etcd 的 v2 协议存储,但最新版的 etcd (3.4 开始)已经默认关闭 v2 协议。 需要在启动参数中添加 --enable-v2=true,才能启用 v2 协议。
4、Run APISIX server
#4、Run APISIX server docker run --name test-api-gateway \ -v `pwd`/example/apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml \ -v `pwd`/example/apisix_log:/usr/local/apisix/logs \ -p 8080:9080 \ -p 9443:9443 \ -d docker.oa.isuwang.com:5000/system/apisix:master-alpine ##注:在执行上述命令实例化容器之前,需要将`pwd`/example/apisix_conf/config.yaml配置文件中 ``` etcd: host: "http://127.0.0.1:2379" # etcd address prefix: "/apisix" # apisix configurations prefix timeout: 1 # 1 seconds ``` 127.0.0.1:2379 改为自己IP 实例化后的test-api-gateway 没有incubator-apisix-dashboard 模块
5、快速创建镜像
方便构建APISIX相关镜像,可拉小编的项目到根目录下执行
mvn clean install
命令即可(apisix镜像构建过程中可能因为网络原因构建失败,多试几回即可)- 构建·apisix
- 构建apisix-dashboard
二、APISIX配置
更详细的apisix api可查看官网,小编这里仅介绍基本的使用。apisix api也可通过apisix-dashboard进行可视化配置,更加方便。dashboard 缺陷:
目前dashboard还存在一些不完善的地方,一些插件的函数配置等不可直接编辑,还是得通过apisix api进行。不过基本的一些配置直接可视化配置、查看很直观,也够用。
#、常规配置 demo (注:IP、节点等都改为自己的)
## 1、配置upstreams
curl http://127.0.0.1/apisix/admin/upstreams/001 -X PUT -i -d '
{
"type":"roundrobin",
"nodes":{
"127.0.0.1:9090":1
},
"desc":"oss"
}
'
## 2、 配置service (可以将各类插件编辑为service,配合route使用。这只是小编写的其中一个插件的demo,可按需扩展)
curl http://127.0.0.1/apisix/admin/services/001 -X PUT -d '
{
"plugins":{
"serverless-pre-function":{
"phase":"rewrite",
"functions":["
return function()
local authorization = ngx.var.cookie_Authorization
ngx.req.set_header(\"Authorization\", authorization)
end"]
},
"proxy-rewrite":{
"enable_websocket":true
}
},
"upstream_id":"001",
"desc":"oss"
}
'
## 3、配置route
curl http://127.0.0.1/apisix/admin/routes/001 -X PUT -i -d '
{
"uris":["/*"],
"hosts":[
"local-sandbox.com"
],
"upstream_id":"001",
"service_id": "001",
"desc":"oss"
}'
## 4、删除route
curl http://127.0.0.1/apisix/admin/routes/001 -X DELETE -i
# 配置上游节点负载均衡+健康检查demo
curl http://127.0.0.1/apisix/admin/routes/015 -X PUT -d '
{
"uris": ["/*"],
"hosts": ["local-web.com"],
"plugins": {},
"upstream": {
"nodes": {
"127.0.0.1:8081": 1,
"127.0.0.1:8082": 1
},
"type": "roundrobin",
"retries": 2,
"checks": {
"active": {
"http_path": "/status",
"host": "local-web.com",
"healthy": {
"interval": 2,
"successes": 1
},
"unhealthy": {
"interval": 1,
"http_failures": 2
},
"req_headers": ["User-Agent: curl/7.29.0"]
},
"passive": {
"healthy": {
"http_statuses": [200, 201],
"successes": 3
},
"unhealthy": {
"http_statuses": [500],
"http_failures": 3,
"tcp_failures": 3
}
}
}
}
}'
三、灰度配置(A/B测试)
据官方介绍,如何通过APISIX支持A/B测试?
比如,根据入参arg_id
分组:
- A组:arg_id <= 1000
- B组:arg_id > 1000
可以这么做:
# vars 对应nginx 的变量
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d '
{
"uri": "/index.html",
"vars": [
["arg_id", "<=", "1000"]
],
"plugins": {
"redirect": {
"uri": "/test?group_id=1"
}
}
}'
curl -i http://127.0.0.1:9080/apisix/admin/routes/2 -X PUT -d '
{
"uri": "/index.html",
"vars": [
["arg_id", ">", "1000"]
],
"plugins": {
"redirect": {
"uri": "/test?group_id=2"
}
}
}'
这种操作使用与大批量的操作,缺点是vars 对应的值只能是常量。相信大部分人的灰度名单配置,都是存储在某个地方,如小编的是存储在redis中动态配置获取的。那么这种可以怎么配置呢?可借助filter_func
函数,在函数中可进行任意的自定义匹配操作。
# 灰度配置demo,通过filter_func 连接redis匹配灰度名单
curl http://192.168.28.86/apisix/admin/routes/0001 -X PUT -i -d '
{
"uris":["/*" ],
"hosts":[
"apisix.sandbox.com"
],
"filter_func": "function(vars)
local test = true
local redis = require \"resty.redis\"
local red = redis:new()
red:set_timeout(1000)
local ok, err = red:connect(\"127.0.0.1\", 6379)
if not ok then
ngx.say(\"failed to connect: \", err)
return
end
local key = \"gray.admin.staffName.\"..string.upper(tostring(ngx.var.COOKIE_StaffLoginName))
local res, err = red:get(key)
ngx.say(res)
return test
end",
"upstream":{
"nodes":{
"127.0.0.1:80":1
},
"type":"roundrobin",
"retries":2
},
"plugins":{},
"desc":"apisix admin"
}'
以上,相信已经满正常的使用需求了,如需更多功能,请查看api。
注:介绍的功能nginx本身也可以支持,介绍只是看中了网关的功能使用,替换nginx
网友评论