Skip to content

Commit

Permalink
docs: add tutorials docs and fix others docs (apache#7952)
Browse files Browse the repository at this point in the history
  • Loading branch information
guitu168 authored Sep 21, 2022
1 parent 5d1429c commit ec6ea8a
Show file tree
Hide file tree
Showing 9 changed files with 808 additions and 27 deletions.
2 changes: 2 additions & 0 deletions docs/en/latest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"type": "category",
"label": "Tutorials",
"items": [
"tutorials/expose-api",
"tutorials/protect-api",
"tutorials/observe-your-api"
]
},
Expand Down
123 changes: 123 additions & 0 deletions docs/en/latest/tutorials/expose-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
title: Expose API
keywords:
- API Gateway
- Apache APISIX
- Expose Service
description: This article describes how to publish services through the API Gateway Apache APISIX.
---

<!--
#
# 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
#
# https://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.
#
-->

This article will guide you through APISIX's upstream, routing, and service concepts and introduce how to publish your services through APISIX.

## Concept introduction

### Upstream

[Upstream](../terminology/upstream.md) is a virtual host abstraction that performs load balancing on a given set of service nodes according to the configured rules.

The role of the Upstream is to load balance the service nodes according to the configuration rules, and Upstream information can be directly configured to the Route or Service.

When multiple routes or services refer to the same upstream, you can create an upstream object and use the upstream ID in the Route or Service to reference the upstream to reduce maintenance pressure.

### Route

[Routes](../terminology/route.md) match the client's request based on defined rules, load and execute the corresponding plugins, and forwards the request to the specified Upstream.

### Service

A [Service](../terminology/service.md) is an abstraction of an API (which can also be understood as a set of Route abstractions). It usually corresponds to an upstream service abstraction.

## Prerequisites

Please make sure you have [installed Apache APISIX](../installation-guide.md) before doing the following.

## Expose your service

1. Create an Upstream.

Create an Upstream service containing `httpbin.org` that you can use for testing. This is a return service that will return the parameters we passed in the request.

```
curl "https://127.0.0.1:9180/apisix/admin/upstreams/1" \
-H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d '
{
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}'
```

In this command, we specify the Admin API Key of Apache APISIX as `edd1c9f034335f136f87ad84b625c8f1`, use `roundrobin` as the load balancing mechanism, and set `httpbin.org:80` as the upstream service. To bind this upstream to a route, `upstream_id` needs to be set to `1` here. Here you can specify multiple upstreams under `nodes` to achieve load balancing.

For more information, please refer to [Upstream](../terminology/upstream.md).

2. Create a Route.

```shell
curl "https://127.0.0.1:9180/apisix/admin/routes/1" \
-H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d '
{
"methods": ["GET"],
"host": "example.com",
"uri": "/anything/*",
"upstream_id": "1"
}'
```

:::note

Adding an `upstream` object to your route can achieve the above effect.

```shell
curl "https://127.0.0.1:9180/apisix/admin/routes/1" \
-H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d '
{
"methods": ["GET"],
"host": "example.com",
"uri": "/anything/*",
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
```

:::

3. Test

After creating the Route, you can test the Service with the following command:

```
curl -i -X GET "https://127.0.0.1:9080/get?foo1=bar1&foo2=bar2" -H "Host: httpbin.org"
```

APISIX will forward the request to `https://httpbin.org:80/anything/foo?arg=10`.

## More Tutorials

You can refer to [Protect API](./protect-api.md) to protect your API.

You can also use APISIX's [Plugin](../terminology/plugin.md) to achieve more functions.
29 changes: 18 additions & 11 deletions docs/en/latest/tutorials/observe-your-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ Before enabling our plugins we need to install Apache APISIX, create a route, an

- [google-cloud-logging](https://apisix.apache.org/docs/apisix/plugins/google-cloud-logging)

And you can see the [full list](https://apisix.apache.org/docs/apisix/plugins/zipkin) on the official website of Apache APISIX. Now for demo purposes, let's choose a simple but mostly used _http-logger_ plugin that is capable of sending API Log data requests to HTTP/HTTPS servers or sends as JSON objects to Monitoring tools. We can assume that a route and an upstream are created. You can learn how to set up them in the **[Getting started with Apache APISIX](https://youtu.be/dUOjJkb61so)** video tutorial. Also, you can find all command-line examples on the GitHub page [apisix-observability-plugins](https://boburmirzo.github.io/apisix-observability-plugins/)
And you can see the [full list](../plugins/http-logger.md) on the official website of Apache APISIX. Now for demo purposes, let's choose a simple but mostly used _http-logger_ plugin that is capable of sending API Log data requests to HTTP/HTTPS servers or sends as JSON objects to Monitoring tools. We can assume that a route and an upstream are created. You can learn how to set up them in the **[Getting started with Apache APISIX](https://youtu.be/dUOjJkb61so)** video tutorial. Also, you can find all command-line examples on the GitHub page [apisix-observability-plugins](https://boburmirzo.github.io/apisix-observability-plugins/)

You can generate a mock HTTP server at [mockbin.com](https://mockbin.org/) to record and view the logs. Note that we also bind the route to an upstream (You can refer to this documentation to learn about more [core concepts of Apache APISIX](https://apisix.apache.org/docs/apisix/architecture-design/apisix)).

The following is an example of how to enable the http-logger for a specific route.

```shell

curl https://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
curl https://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"http-logger": {
Expand All @@ -101,14 +102,18 @@ curl https://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f13

```

> To http-logger plugin settings, your can just put your mock server URI address like below:
:::note

To `http-logger` plugin settings, your can just put your mock server URI address like below:

```json
{
"uri": "https://mockbin.org/bin/5451b7cd-af27-41b8-8df1-282ffea13a61"
}
```

:::

Once we get a successful response from APISIX server, we can send a request to this _get_ endpoint to generate logs.

```shell
Expand All @@ -130,7 +135,8 @@ Apache APISIX API Gateway also offers [prometheus-plugin](https://apisix.apache.
Let’s enable prometheus-plugin for our route:

```shell
curl https://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
curl https://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/get",
"plugins": {
Expand All @@ -140,7 +146,7 @@ curl https://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f1
}'
```

We fetch the metric data from the specified URL `/apisix/prometheus/`metrics.
We fetch the metric data from the specified URL `/apisix/prometheus/metrics`.

```shell
curl -i https://127.0.0.1:9091/apisix/prometheus/metrics
Expand Down Expand Up @@ -185,20 +191,21 @@ In addition to this, you can view the Grafana dashboard running in your local in

You can also check two other plugins for metrics:

- [Node status Plugin](https://apisix.apache.org/docs/apisix/plugins/node-status/)
- [Node status Plugin](../plugins/node-status.md)

- [Datadog Plugin](https://apisix.apache.org/docs/apisix/plugins/opentelemetry)
- [Datadog Plugin](../plugins/datadog.md)

## Tracing

The third is **tracing** or distributed tracing allows you to understand the life of a request as it traverses your service network and allows you to answer questions like what service has this request touched and how much latency was introduced. Traces enable you to further explore which logs to look at for a particular session or related set of API calls.

[Zipkin](https://zipkin.io/) an open-source distributed tracing system. [APISIX plugin](https://apisix.apache.org/docs/apisix/plugins/zipkin) is supported to collect tracing and report to Zipkin Collector based on [Zipkin API specification](https://zipkin.io/pages/instrumenting.html).

Here’s an example to enable the _zipkin plugin_ on the specified route:
Here’s an example to enable the `zipkin` plugin on the specified route:

```shell
curl https://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
curl https://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"methods": [
"GET"
Expand Down Expand Up @@ -242,9 +249,9 @@ As you noticed, the recent traces were exposed in the above pictures.

You can also check two other plugins for tracing:

- [Skywalking-plugin](https://apisix.apache.org/docs/apisix/plugins/skywalking)
- [Skywalking-plugin](../plugins/skywalking.md)

- [Opentelemetry-plugin](https://apisix.apache.org/docs/apisix/plugins/opentelemetry)
- [Opentelemetry-plugin](../plugins/opentelemetry.md)

## Summary

Expand Down
124 changes: 124 additions & 0 deletions docs/en/latest/tutorials/protect-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
title: Protect API
keywords:
- API Gateway
- Apache APISIX
- Rate Limit
- Protect API
description: This article describes how to secure your API with the rate limiting plugin for API Gateway Apache APISIX.
---

<!--
#
# 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
#
# https://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.
#
-->

This article describes secure your API with the rate limiting plugin for API Gateway Apache APISIX.

## Concept introduction

### Plugin

This represents the configuration of the plugins that are executed during the HTTP request/response lifecycle. A [Plugin](./terminology/plugin.md) configuration can be bound directly to a Route, a Service, a Consumer or a Plugin Config.

:::note

If [Route](./terminology/route.md), [Service](./terminology/service.md), [Plugin Config](./terminology/plugin-config.md) or Consumer are all bound to the same for plugins, only one plugin configuration will take effect. The priority of plugin configurations is: Consumer > Route > Plugin Config > Service. At the same time, there are 6 stages involved in the plugin execution process, namely `rewrite`, `access`, `before_proxy`, `header_filter`, `body_filter` and `log`.

:::

## Preconditions

Before following this tutorial, ensure you have [exposed the service](./expose-api.md).

## Protect your API

We can use rate limits to limit our API services to ensure the stable operation of API services and avoid system crashes caused by some sudden traffic. We can restrict as follows:

1. Limit the request rate;
2. Limit the number of requests per unit time;
3. Delay request;
4. Reject client requests;
5. Limit the rate of response data.

APISIX provides several plugins for limiting current and speed, including [limit-conn](./plugins/limit-conn.md), [limit-count](./plugins/limit-count.md), [limit- req](./plugins/limit-req.md) and other plugins.

- The `limit-conn` Plugin limits the number of concurrent requests to your services.
- The `limit-req` Plugin limits the number of requests to your service using the leaky bucket algorithm.
- The `limit-count` Plugin limits the number of requests to your service by a given count per time.

Next, we will use the `limit-count` plugin as an example to show you how to protect your API with a rate limit plugin:

1. Create a Route.

```shell
curl -i https://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/index.html",
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key_type": "var",
"key": "remote_addr"
}
},
"upstream_id": "1"
}
}'
```

In the above configuration, a Route with ID `1` is created using the upstream made in [Expose Service](./expose-api.md), and the `limit-count` plugin is enabled. The plugin only allows the client to access the upstream service `2` times within `60` seconds. If more than two times, the `503` error code will be returned.

2. Test

```shell
curl https://127.0.0.1:9080/index.html
```

After using the above command to access three times in a row, the following error will appear:

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

If the above result is returned, the `limit-count` plugin has taken effect and protected your API.

## More Traffic plugins

In addition to providing plugins for limiting current and speed, APISIX also offers many other plugins to meet the needs of actual scenarios:

- [proxy-cache](./plugins/proxy-cache.md): This plugin provides the ability to cache backend response data. It can be used with other plugins. The plugin supports both disk and memory-based caching. Currently, the data to be cached can be specified according to the response code and request mode, and more complex caching strategies can also be configured through the no_cache and cache_bypass attributes.
- [request-validation](./plugins/request-validation.md): This plugin is used to validate requests forwarded to upstream services in advance.
- [proxy-mirror](./plugins/proxy-mirror.md): This plugin provides the ability to mirror client requests. Traffic mirroring is copying the real online traffic to the mirroring service, so that the online traffic or request content can be analyzed in detail without affecting the online service.
- [api-breaker](./plugins/api-breaker.md): This plugin implements an API circuit breaker to help us protect upstream business services.
- [traffic-split](./plugins/traffic-split.md): You can use this plugin to gradually guide the percentage of traffic between upstreams to achieve blue-green release and grayscale release.
- [request-id](./plugins/request-id.md): The plugin adds a `unique` ID to each request proxy through APISIX for tracking API requests.
- [proxy-control](./plugins/proxy-control.md): This plugin can dynamically control the behavior of NGINX proxy.
- [client-control](./plugins/client-control.md): This plugin can dynamically control how NGINX handles client requests by setting an upper limit on the client request body size.

## More Tutorials

You can refer to the [Observe API](./observe-your-api.md) document to monitor APISIX, collect logs, and track.
9 changes: 9 additions & 0 deletions docs/zh/latest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
"type": "doc",
"id": "architecture-design/apisix"
},
{
"type": "category",
"label": "Tutorials",
"items": [
"tutorials/expose-api",
"tutorials/protect-api",
"tutorials/observe-your-api"
]
},
{
"type": "category",
"label": "Terminology",
Expand Down
Loading

0 comments on commit ec6ea8a

Please sign in to comment.