mirror of
https://gitee.com/iresty/apisix.git
synced 2024-12-15 17:31:45 +08:00
89 lines
2.9 KiB
Markdown
89 lines
2.9 KiB
Markdown
|
---
|
|||
|
title: Service
|
|||
|
---
|
|||
|
|
|||
|
<!--
|
|||
|
#
|
|||
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|||
|
# contributor license agreements. See the NOTICE file distributed with
|
|||
|
# this work for additional information regarding copyright ownership.
|
|||
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|||
|
# (the "License"); you may not use this file except in compliance with
|
|||
|
# the License. You may obtain a copy of the License at
|
|||
|
#
|
|||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|||
|
#
|
|||
|
# 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.
|
|||
|
#
|
|||
|
-->
|
|||
|
|
|||
|
`Service` 是某类 API 的抽象(也可以理解为一组 Route 的抽象)。它通常与上游服务抽象是一一对应的,`Route`
|
|||
|
与 `Service` 之间,通常是 N:1 的关系,参看下图。
|
|||
|
|
|||
|
![服务示例](../../../assets/images/service-example.png)
|
|||
|
|
|||
|
不同 Route 规则同时绑定到一个 Service 上,这些 Route 将具有相同的上游和插件配置,减少冗余配置。
|
|||
|
|
|||
|
比如下面的例子,创建了一个启用限流插件的 Service,然后把 id 为 `100`、`101` 的 Route 都绑定在这个 Service 上。
|
|||
|
|
|||
|
```shell
|
|||
|
# create new Service
|
|||
|
$ curl http://127.0.0.1:9080/apisix/admin/services/200 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
|||
|
{
|
|||
|
"plugins": {
|
|||
|
"limit-count": {
|
|||
|
"count": 2,
|
|||
|
"time_window": 60,
|
|||
|
"rejected_code": 503,
|
|||
|
"key": "remote_addr"
|
|||
|
}
|
|||
|
},
|
|||
|
"upstream": {
|
|||
|
"type": "roundrobin",
|
|||
|
"nodes": {
|
|||
|
"39.97.63.215:80": 1
|
|||
|
}
|
|||
|
}
|
|||
|
}'
|
|||
|
|
|||
|
# create new Route and reference the service by id `200`
|
|||
|
curl http://127.0.0.1:9080/apisix/admin/routes/100 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
|||
|
{
|
|||
|
"methods": ["GET"],
|
|||
|
"uri": "/index.html",
|
|||
|
"service_id": "200"
|
|||
|
}'
|
|||
|
|
|||
|
curl http://127.0.0.1:9080/apisix/admin/routes/101 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
|||
|
{
|
|||
|
"methods": ["GET"],
|
|||
|
"uri": "/foo/index.html",
|
|||
|
"service_id": "200"
|
|||
|
}'
|
|||
|
```
|
|||
|
|
|||
|
当然我们也可以为 Route 指定不同的插件参数或上游,比如下面这个 Route 设置了不同的限流参数,其他部分(比如上游)则继续使用 Service 中的配置参数。
|
|||
|
|
|||
|
```shell
|
|||
|
curl http://127.0.0.1:9080/apisix/admin/routes/102 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
|||
|
{
|
|||
|
"uri": "/bar/index.html",
|
|||
|
"id": "102",
|
|||
|
"service_id": "200",
|
|||
|
"plugins": {
|
|||
|
"limit-count": {
|
|||
|
"count": 2000,
|
|||
|
"time_window": 60,
|
|||
|
"rejected_code": 503,
|
|||
|
"key": "remote_addr"
|
|||
|
}
|
|||
|
}
|
|||
|
}'
|
|||
|
```
|
|||
|
|
|||
|
注意:当 Route 和 Service 都开启同一个插件时,Route 参数的优先级是高于 Service 的。
|