前言
在现代的 Web 开发中,后端服务的解耦和部署灵活性成为了一个重要问题。Serverless 架构的出现使得我们可以轻松地部署无状态的、轻量级的后端服务。AWS Lambda 和 API Gateway 组成的 Serverless 应用架构,成为了构建 Serverless 应用的标配。
Serverless 应用中的 API Gateway 起到了重要的角色,在使用 AWS API Gateway 时,开发者需要通过手动编写 CloudFormation 模板或通过 AWS 控制台创建 REST API,来为 Lambda 服务生成相应的入口。对于 API Gateway 的配置,早期的服务开发者,需要亲自理解 RESTful API 的设计,查阅 AWS 的 API Gateway 文档,繁琐费时,且容易出错。
本文就介绍了一个 NPM 包 Serverless Endpoint Configuration,即通过 YAML 配置自动化生成前端 API Gateway,并完成丰富的规则设置,例如速率限制、授权相关、缓存策略等常见服务配置,从而简化前期 API Gateway 的部署步骤。
功能介绍
此 NPM 依赖可以从配置自动生成 AWS API Gateway,包括生成 REST & WebSocket 的端点,同时还有以下核心功能:
支持对 不同端点的速率 限制的管理,并通过 控制域名访问策略 屏蔽非法访问。
支持 前端授权 调用 API,使用 AWS 的各项授权服务例如 API Key、 Amazon Cognito User Pools 以及 AWS Identity and Access Management(IAM)。
支持 在 API Gateway 中运用缓存策略 以提升服务响应速度。
安装
在使用 Serverless Endpoint Configuration 之前,需要确保已经安装了 Node.js,然后在命令行输入以下命令安装:
npm install serverless-endpoint-configuration
安装后,直接通过 require 引入:
const YAML = require('yaml'); const fs = require('fs'); const SEC = require('serverless-endpoint-configuration');
使用步骤
下面我们就介绍一下如何使用 Serverless Endpoint Configuration。
第一步:创建简单YAML文件
在项目中,创建一个 config.yaml 的配置文件,这个文件包含了生成 API Gateway 的信息和 API Gateway 的方法以及参数,具体格式如下:
-- -------------------- ---- ------- ----- - ----- ---------- ---- ------ ------------ ---- ---------- --------- ----------- - ----- -------- ---- ---------- ------- ----- --------- -------- --------- - ----- ------------------ ----------- ----------------- ---------- ------ ----- ------- ------------ ---------- ---- --- ------ ------- - ----- --------- ---- ---------- - ----- ------ ---- -------展开代码
其中:
- rest 是 REST API 的配置;
- name 是 REST API 名称;
- uri 是 REST API URI,即资源路径;
- description 是 REST API 描述;
- dataType 是 REST API 接收的数据类型;
- response 是 REST API 返回结果的格式和数据,schemaFile 是以对应格式的 JSON 文件路径;
- method 是 HTTP 请求方法,必须大写。
websocket 是 WebSocket 的配置;
- group 是 WebSocket 的群组;
- routes 是 WebSocket 的路由。
注意在 Serverless Endpoint Configuration 中,YAML 的接口名称与 HTTP 请求方法名是一一对应的。
例如,上述配置文件中,getpet
对应 GET
请求方式。
第二步:生成 API Gateway
在构建 Serverless 应用时,导入 YAML 配置文件即可使用生成 API Gateway。具体配置及生成 API Gateway 的相关代码如下:
-- -------------------- ---- ------- ----- -------- ------- - --- ---------- - -------------------------------- -------- --- ---------- - ----------------------- ------------------------ ----- --------------- - - -------- --------------------------- -- ----- -------- - ----- --------------------------------------- ---------------------- - --------------- -- - -------------------- ---展开代码
上述代码使用 fs
模块读取 YAML 配置文件,并使用 YAML
模块解析 YAML 文件,解析后获得 yamlObject
,此时console.log
可以查看配置信息,输出示例如下:
-- -------------------- ---- ------- - ----- - - ----- ----------- ---- ------- ------------ ---- ---------- ---------- ----------- - -------- - - -- ---------- - ------ - ----- -------- ------------ ---------- ---- --- ------ -- ------- - --------- -------- - - -展开代码
接着,使用 serverless-endpoint-configuration
的 generateEndpoints
方法生成 API Gateway。 此方法接受一个对象作为配置对象,调用时传递一个 JSON 序列化、自动转义的 YAML 对象即可,具体如下:
const endpointOptions = { produce: YAML.stringify(yamlObject), }; const endpoint = await SEC.generateEndpoints(endpointOptions);
API Gateway 配置修改
Serverless-Endpoint-Configuration 包含了基础的 API Gateway 属性,但是也可以自定义需要的 API Gateway 属性。在 AWS 的控制台或者仪表盘上,可以直接修改并保存您的 API Gateway。
建议是修改以前需要备份相关说明,因为 REST API 和 WebSocket 群组和路由在 AWS 的控制台和仪表板上进行了修改,导致自动化更改响应方法无效。
总结
在开发 Serverless 应用时,自动化生成 API Gateway 的工具极大地降低了客户端的集成难度。Serverless-Endpoint-Configuration 能够自动配置许多 AWS API Gateway 属性,极大地减轻了 API Gateway 的开发负担。在实际的开发项目中,结合 AWS Lambda 就可实现极佳的性价比,免费或少量免费的使用,还能免去过多的转化时间。
至此,我们已经介绍了 Serverless Endpoint Configuration 的使用方法,同时也阐释了 Serverless 应用的优点和配置方法。希望本文内容能够帮助到 Serverless 开发爱好者们。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/6005670181e8991b448e3440