趣谈 | 什么是API货币化
作者:刘维,API7.ai 网关开发工程师
摘要:本文介绍 API 货币化的概念,应用场景和具体实践。
# create consumer group
curl http://127.0.0.1:9180/apisix/admin/consumer_groups/company_a -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"limit-count": {
"count": 200,
"time_window": 60,
"rejected_code": 503,
"group": "$consumer_group_id"
}
}
}'
# create consumer 1
curl http://127.0.0.1:9180/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"username": "jack",
"plugins": {
"key-auth": {
"key": "auth-one"
}
},
"group_id": "company_a"
}'
# create consumer 2
curl http://127.0.0.1:9180/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"username": "johnson",
"plugins": {
"key-auth": {
"key": "auth-two"
}
},
"group_id": "company_a"
}'
# create route
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/get",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org": 1
}
}
}'
# hit the route
curl -i http://127.0.0.1:9180/get -H 'apikey: auth-one'
...
X-RateLimit-Limit: 200
X-RateLimit-Remaining: 199
...
curl -i http://127.0.0.1:9180/get -H 'apikey: auth-two'
...
X-RateLimit-Limit: 200
X-RateLimit-Remaining: 198
...
# change count value to 2 requests per minute
curl http://127.0.0.1:9180/apisix/admin/consumer_groups/company_a -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"group": "$consumer_group_id"
}
}
}'
# hit the route
curl -i http://127.0.0.1:9180/get -H 'apikey: auth-two'
...
X-RateLimit-Limit: 2
X-RateLimit-Remaining: 1
...
curl -i http://127.0.0.1:9180/get -H 'apikey: auth-one'
...
X-RateLimit-Limit: 2
X-RateLimit-Remaining: 0
...
# no count, HTTP 503
curl -i http://127.0.0.1:9180/get -H 'apikey: auth-one'
HTTP/1.1 503 Service Temporarily Unavailable
# after a minute, count recover
curl -i http://127.0.0.1:9180/get -H 'apikey: auth-one'
...
X-RateLimit-Limit: 2
X-RateLimit-Remaining: 1
...
# create another route
curl http://127.0.0.1:9180/apisix/admin/routes/2 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/anything",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org": 1
}
}
}'
# you could see both routes share the same count
curl -i http://127.0.0.1:9180/get -H 'apikey: auth-one'
...
X-RateLimit-Limit: 2
X-RateLimit-Remaining: 1
...
curl -i http://127.0.0.1:9180/anything -H 'apikey: auth-one'
HTTP/1.1 503 Service Temporarily Unavailable
...
OSCHINA 2022中国开源开发者问卷启动
你的反馈很重要!
点击下方小程序立即参与
END
点这里 ↓↓↓ 记得 关注✔ 标星⭐ 哦~
微信扫码关注该文公众号作者
戳这里提交新闻线索和高质量文章给我们。
来源: qq
点击查看作者最近其他文章