亚马逊,应用网关 AWS API GATEWAY

 

 

API stands for “Application Programming Interface”. Amazon API gateway is the front door for your APIs on AWS cloud. An API Gateway provides a set of API endpoints and encapsulates the internal structure without revealing how it is being constructed.

API代表“应用程序编程接口” 。 Amazon API网关是AWS云上API的前门。 API网关提供了一组API端点并封装了内部结构,而没有透露其构造方式。

Amazon API gateway process and handles the request and response from thousands and millions of concurrent API calls. Similar to other services on AWS cloud, it is also build to massively scale.

Amazon API网关处理并处理来自成千上万并发API调用的请求和响应。 与AWS云上的其他服务类似,它也可以大规模构建。

 
 

AWS offers Amazon API Gateway service on a very reasonable cost and also available in AWS free tier. It allows you 1 million API calls in a month for free in the free tier and for 12 months in a row. If your application use exceeds the tiers, you simply pay standard, pay-as-you-go service rates.

AWS以非常合理的价格提供了Amazon API Gateway服务,并且还提供AWS免费套餐。 它允许您每月免费获得一次免费的API调用,并且可以连续12个月进行一百万次API调用。 如果您的应用程序使用超出了这些级别,则只需支付标准的即付即用服务费。

什么是API网关? (What is API Gateway?)

To understand and use Amazon API Gateway, you need to know what is an API Gateway and its applications.

要了解和使用Amazon API Gateway,您需要知道什么是API Gateway及其应用程序。

In a large application, you may have more than one microservice running and all of them exposes some of the API endpoints. You can connect to these API endpoints from the microservice url. But think about when you consume these API endpoints in your UI or give them to some third party. You will end up having many URLs. And for each of the service, you have to manage cache, throttling, etc separately.

在大型应用程序中,您可能正在运行多个微服务,并且所有微服务都公开了一些API端点。 您可以从微服务网址连接到这些API端点。 但是请考虑一下何时在用户界面中使用这些API终结点或将它们提供给第三方。 您最终将拥有许多URL。 对于每项服务,您都必须分别管理缓存,限制等。

API gateway removes having several URLs for REST services and provides you a front door for your APIs. It encapsulates the internal structure of your services. And restricts the direct access to your APIs which makes the APIs secure.

API网关删除了用于REST服务的多个URL,并为您的API提供了前门。 它封装了服务的内部结构。 并限制对API的直接访问,这使API安全。

 

Mostly, API Gateways are a proxy based application which routes the request to the right resources. And take care of all the front door activities like throttling, security, load balancing, logging, authentication, and authorization, etc.

通常,API网关是基于代理的应用程序,可将请求路由到正确的资源。 并注意所有前门活动,例如节流,安全性,负载平衡,日志记录,身份验证和授权等。

Amazon API网关 (Amazon API Gateway)

Amazon API Gateway is an AWS service that is the front door for all your APIs running of AWS cloud. In addition, it offers service for creating, publishing, maintaining, monitoring, and securing REST and WebSocket APIs at any scale.

Amazon API Gateway是一项AWS服务,是运行AWS云的所有API的前门。 此外,它还提供用于创建,发布,维护,监视和保护任意规模的REST和WebSocket API的服务。

You can create a REST API with the help of Amazon API Gateway and deploy it instantly. The API you can use in your application or can also make it available for the third party.

您可以在Amazon API Gateway的帮助下创建REST API并立即进行部署。 您可以在应用程序中使用的API或也可以将其提供给第三方。

Let’s take an example of Amazon API Gateway application to understand it (Source: Amazon Web services):

让我们以Amazon API Gateway应用程序为例来理解它(来源:Amazon Web服务):

Amazon Api Gateway Application

Amazon Api Gateway Application

Amazon Api网关应用程序

The example above explains the Amazon API Gateway and how it fits well with other services. So, in this application, the APIs are built on API Gateway and connected to invoke the lambda functions.

上面的示例说明了Amazon API Gateway以及它如何与其他服务很好地融合在一起。 因此,在此应用程序中,API建立在API网关上,并已连接以调用lambda函数。

If you don’t know about Amazon S3 and how to host public site then please read our post on:

如果您不了解Amazon S3以及如何托管公共站点,请阅读以下文章:

  1. Amazon S3 – AWS Simple Storage Service

     

    Amazon S3 – AWS简单存储服务
  2. Host a host a public static website on Amazon S3?

     

    在Amazon S3上托管主机公共静态网站?

Note: We will build this application in our next post to give you an end-to-end application overview of Amazon API Gateway, AWS Lambda, and Amazon S3.

注意:我们将在下一篇文章中构建此应用程序,以向您提供Amazon API Gateway,AWS Lambda和Amazon S3的端到端应用程序概述。

建筑 (Architecture)

The architecture of Amazon API gateway is quite simple. API Gateway is the internet facing service which connects the request to the specific API calls. Below diagram demonstrate how API Gateway works:

Amazon API网关的架构非常简单。 API网关是面向互联网的服务,可将请求连接到特定的API调用。 下图展示了API Gateway的工作方式:

Amazon Aws Architecture

Amazon Aws Architecture (Source: Amazon Web Services)

Amazon Aws架构(来源:Amazon Web服务)

为什么选择Amazon API Gateway? (Why Amazon API Gateway?)

Amazon API Gateway offers a number of benefits which makes it one of the highly used services on AWS Cloud platform. Let’s discuss some of many reasons of why use Amazon API Gateway?

Amazon API Gateway具有许多优势,使其成为AWS Cloud平台上使用率很高的服务之一。 让我们讨论为什么使用Amazon API Gateway的许多原因中的一些原因。

REST端点 (REST Endpoints)

Instead of creating an application which exposes some RESTFul endpoints and deploying it, Amazon API Gateway offers to create REST endpoints without worrying about the application. Create resource-based APIs and use API Gateway’s data transformation capabilities to generate the requests in the language target services expect.

Amazon API Gateway可以创建REST端点,而无需担心该应用程序,而不是创建公开一些RESTFul端点的应用程序并进行部署。 创建基于资源的API,并使用API​​ Gateway的数据转换功能以目标服务期望的语言生成请求。

You can just create the API endpoint and connect them to the Lambda functions.

您可以只创建API端点并将其连接到Lambda函数。

安全控制 (Security Controls)

You can make your API highly secure with Amazon Cognito, Identity and Identity Access Management (IAM). You can execute authorization flow from API Gateway which internally executes the Lambda.

您可以使用Amazon Cognito,身份和身份访问管理(IAM)使您的API高度安全。 您可以从内部执行Lambda的API网关执行授权流程。

API版本控制 (API Versioning)

You can deploy multiple versions of the same API and can run them simultaneously with the help of Amazon API Gateway. The best thing is that you only pay for the usage and not for the multiple deployments.

您可以部署同一API的多个版本,并可以在Amazon API Gateway的帮助下同时运行它们。 最好的事情是,您只需为使用付费,而无需为多个部署付费。

性能规模,流量限制和缓存 (Performance Scale, Traffic Throttle and Cache)

Similar to other services Amazon API Gateway offers high scalability with very low latency. Like AWS CloudFront, it takes advantage of the global network of edge location. Read about Amazon CloudFront & Edge Locations.

与其他服务类似,Amazon API Gateway提供了很高的可扩展性和非常低的延迟。 像AWS CloudFront一样,它利用了边缘位置的全球网络。 了解有关Amazon CloudFront和Edge位置的信息 。

 

Traffic Throttle and Cache makes sure that backend APIs are not called unnecessarily. Which makes it highly reliable and available.

流量限制和缓存可确保不会不必要地调用后端API。 这使其高度可靠且可用。

监控方式 (Monitoring)

Monitoring the API is an integral part of the Amazon API Gateway. A number of metrics available on Amazon CloudWatch to monitor your APIs. You can create dashboards and set alarms and notification in CloudWatch.

监视API是Amazon API Gateway不可或缺的一部分。 Amazon CloudWatch上有许多指标可用于监视您的API。 您可以创建仪表板,并在CloudWatch中设置警报和通知。

Amazon API Gateway的功能 (Features of Amazon API Gateway)

Amazon API Gateway offers a number of features. Some of them we have already discussed in Why Amazon API Gateway” section.

Amazon API Gateway提供了许多功能。 我们在“ 为什么使用Amazon API Gateway”一节中已经讨论了其中的一些。

Amazon API gateway Offers features such as the following:

Amazon API网关提供以下功能:

  • CloudWatch Access logging and monitoring which cinludes creation of Alarms and notifications.

     

    CloudWatch Access日志记录和监视可阻止创建警报和通知。
  • Supports both stateless and statefull APIs. For example – Stateful WebSocket and Stateless RESTFul APIs

     

    同时支持无状态和全状态API。 例如–有状态WebSocket和无状态RESTFul API
  • Supports custom domain names for your Amazon API gateway – Instead of using the default API gateway URL, you can set and use your domain.

     

    支持您的Amazon API网关的自定义域名–您可以设置和使用您的域,而不是使用默认的API网关URL。
  • Allow multiple version of the same API to run simultaneously

     

    允许同一API的多个版本同时运行
  • Ability to use CloudFormation templates to create and do the whole deployent without doing a single manual configuration

     

    使用CloudFormation模板来创建和执行整个部署程序的能力,而无需执行单个手动配置
  • Provides Authorization and Authentication mechanism with Conginito and Lambda funcions

     

    提供Conginito和Lambda函数的授权和身份验证机制
  • Caching and request throtteling

     

    缓存并请求限制
  • Single point access to all of your APIs

     

    单点访问所有API

使用示例REST API设置API网关 (Setup API Gateway with sample REST API)

Let’s create an API gateway and a REST API.

让我们创建一个API网关和一个REST API。

先决条件 (Prerequisite)

  • Login to the AWS management console (https://console.aws.amazon.com/)

     

    登录到AWS管理控制台( https://console.aws.amazon.com/ )
  • Search for “API Gateway” in services

     

    在服务中搜索“ API网关”
  • Click on the “API Gateway” service and you will see the below screen to get started with Amazon API gateway
    Get Started With Amazon Api Gateway

    Get Started With Amazon Api Gateway

     

    单击“ API网关”服务,您将看到以下屏幕以开始使用Amazon API网关

    Amazon Api Gateway入门

开始使用 (Get Started)

Click on “Get Started” button and you will see the below screen with a sample API specs in Swagger 2.0

单击“入门”按钮,您将看到以下屏幕,其中包含Swagger 2.0中的API样本示例

Create Example Api

Create Example Api

创建示例Api

Click on “OK” to import the selected API.

单击“确定”以导入所选的API。

选择创建 (Choose to create)

  1. Choose the protocol – Select whether you would like to create a REST API or a WebSocket API. We will choose “REST” option for the example.

     

    选择协议 –选择是要创建REST API还是WebSocket API。 我们将为示例选择“ REST”选项。
  2. Create new API – In Amazon API Gateway, a REST API refers to a collection of resources and methods that can be invoked through HTTPS endpoints.
    We will choose the “Example API”. You can also create a New API or import from the Swagger Json for existing API.

     

    创建新的API –在Amazon API Gateway中,REST API是指可以通过HTTPS端点调用的资源和方法的集合。
    我们将选择“ Example API” 。 您还可以创建新API或从Swagger Json导入现有API。
  3. Example API – The sample API Json that will be used to crate your first API.

     

    示例API –示例API Json将用于创建您的第一个API。

You may ignore rest of the options for now and click on “Import” button. See the screen below:

您现在可以忽略其余选项,然后单击“导入”按钮。 请参阅以下屏幕:

Import Example Api

Import Example Api

导入示例Api

部署API ( Deploy the API)

AWS will take few seconds to create API and Gateway. Once done, you will see the below screen:

AWS将花费几秒钟来创建API和网关。 完成后,您将看到以下屏幕:

First Restful Api

First Restful Api

第一个宁静的Api

If you want to add more API end points then you can do so. Click on the “Action”
button and you will see the below options:

如果要添加更多的API端点,则可以这样做。 点击“动作”
按钮,您将看到以下选项:

Api Actions

Api Actions

Api动作

In the API Sections, click on “Deploy API” to deploy the API. The below window will prompt to enter the stage that you want to deploy.

在“ API”部分中,单击“部署API”以部署API。 下面的窗口将提示您输入要部署的阶段。

Deploy Api

Deploy Api

部署Api

查看已部署的API (View Deployed API)

Once you click on the “Deploy” button, AWS will take few seconds/minutes based on the number of API endpoints to deploy, please wait while deployment completes. Once done, you will see the below screen:

一旦单击“部署”按钮,AWS将根据要部署的API端点数花费几秒钟/几分钟,请等待部署完成。 完成后,您将看到以下屏幕:

Api Gateway And Api

Api Gateway And Api

Api网关和Api

You can see the URL for your API: https://2ikmujgy4g.execute-api.us-east-1.amazonaws.com/stage.
Click on the in the new browser window and you will see the below documentation:

您可以看到您的API的URL: https://2ikmujgy4g.execute-api.us-east-1.amazonaws.com/stage : https://2ikmujgy4g.execute-api.us-east-1.amazonaws.com/stage 。
在新的浏览器窗口中单击,您将看到以下文档:

Your First Api

Your First Api

你的第一个Api

连接到您的API (Connect to Your API)

You can use postman to connect to your API OR just connect through the curl command.

您可以使用邮递员连接到您的API,也可以只通过curl命令连接。

Connect To Api

Connect To Api

连接到Api

posted @ 2021-11-19 14:31  Slashout  阅读(765)  评论(0编辑  收藏  举报