官方网站建设项目询价,口碑营销平台,广告设计怎么接单,泉州网站推广排名AWS API Gateway 集成使得使用符合 OpenAPI 标准的 Lambda Function 轻松实现 REST API。
关于开放API 它是一个 允许以标准方式描述 REST API 的规范。 OpenAPI规范 (OAS) 为 REST API 定义了与编程语言无关的标准接口描述。这使得人类和计算机都可以发现和理解服务的功能而无需访问源代码、附加文档或检查网络流量。
关于Swagger Swagger是一组围绕 OpenAPI 规范构建的开源工具可以帮助软件专业人员设计、构建、记录和使用 REST API。OpenAPI 处理规范Swagger 实现规范来描述 REST API 。
OpenAPI规范简介
目的
API是使用OpenAPI标准定义的可以被人类和计算机发现无需访问源代码、文档或通过网络流量检查即可了解服务的功能。 如果定义正确消费者可以使用最少量的实现逻辑来理解远程服务并与之交互。然后文档生成工具可以使用 OpenAPI 定义来显示 API代码生成工具可以使用各种编程语言、测试工具和许多其他用例来生成服务器和客户端。
开放API的主要 概念
OpenAPI文档
定义/描述 API 的文档或一组文档。OpenAPI 定义使用并符合 OpenAPI 规范。
路径模板
路径模板是指使用由大括号 ({}) 分隔的模板表达式将 URL 路径的一部分标记为可使用路径参数进行替换。
路径中的每个模板表达式必须对应于路径项本身和/或路径项的每个操作中包含的路径参数。
媒体类型
媒体类型定义分布在多个资源中。媒体类型定义应符合RFC6838。
可能的媒体类型定义的一些示例
应用程序/json测试/普通chartset-uff-8
HTTP 状态代码
HTTP 状态代码用于指示已执行操作的状态。可用的状态代码由RFC7231定义注册的状态代码列在IANA 状态代码注册表中。
AWS Lambda 函数的开放 API 参考实现
参考架构 参考实现由以下组件组成
AWS Lambda函数实现业务需求/逻辑 API 网关将功能公开为 APIAmazon API 网关集成器用于集成开放 API 规范并将 Lambda 函数操作公开为 API
有关每个架构组件的更多详细信息请参阅后续部分。
关于 AWS Lambda 函数
Lambda 是一种计算服务允许在不配置或管理服务器的情况下运行代码。Lambda 在高可用性计算基础设施上运行代码并执行计算资源的所有管理包括服务器和操作系统维护、容量配置和自动扩展以及日志记录。使用 Lambda几乎可以为任何类型的应用程序或后端服务运行代码。它所需要的只是以Lambda 支持的语言之一提供代码。 Lambda 仅在需要时运行函数并自动扩展从每天几个请求到每秒数千个请求。它节省了计算成本因为只需支付函数在运行时消耗的计算时间——代码不运行时不收费。可以使用 Lambda API 或来自其他 AWS 服务的事件调用 lambda 函数例如在 S3 存储桶中创建对象时。
关于 AWS API 网关
Amazon API Gateway 是一项完全托管的服务使开发人员可以轻松创建、发布、维护、监控和保护任何规模的 API。API 充当应用程序从后端服务访问数据、业务逻辑或功能的“前门”。
关于AWS API集成功能
这种类型的集成允许 API 公开 AWS 服务操作。在AWS集成中需要配置集成请求和集成响应并设置从方法请求到集成请求、从集成响应到方法响应的必要数据映射。
它允许指定用于此方法的后端集成的详细信息。此扩展是OpenAPI 操作对象的扩展属性。结果是一个API 网关集成对象。
重要财产
与指定后端的集成类型。有效值为
http 或 http_proxy用于与 HTTP 后端集成。aws_proxy用于与 AWS Lambda 函数的代理集成 aws用于与 AWS Lambda 函数自定义集成或与其他 AWS 服务集成例如 Amazon DynamoDB、Amazon Simple notification Service 或 Amazon Simple Queue Service。mock用于与 API Gateway 集成无需调用任何后端。这种类型的集成允许 API Gateway 返回响应而无需将请求进一步发送到后端。
Lambda 代理集成支持单个 Lambda 函数的简化集成设置。设置很简单可以随后端一起发展而无需拆除已经创建的设置。由于这些原因强烈建议与 Lambda 函数集成。
相比之下Lambda 自定义集成允许为具有类似输入和输出数据格式要求的各种集成端点重用已配置的映射模板。该设置涉及较多推荐用于更高级的应用场景。
x-amazon-apigateway-integrations例子
以下示例使用 OpenAPI 标准定义 HTTP API一个 GET 和一个 POST并为每个 API 使用一个 API 网关集成来与 Lambda Function 集成通过引用函数的 ARN。它使用 AWS Identity and Access Management (IAM) 角色作为集成凭证。以下格式采用 YAML。
openapi: 3.0.1
info:description: This is a definition of Proxy Pattern Service. The service has 2 APIs.version: v1title: Proxy Pattern Service
paths:/v1/proxypattern/employee:options:summary: CORS supportdescription: |Enable CORS by returning correct headersconsumes:- application/jsonproduces:- application/jsontags:- CORSsecurity:- NONE: []x-amazon-apigateway-integration:type: mockrequestTemplates:application/json: |{statusCode : 200}responses:default:statusCode: 200responseParameters:method.response.header.Access-Control-Allow-Headers : Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-apigw-api-id,X-Amz-Security-Token,Cache-Controlmethod.response.header.Access-Control-Allow-Methods : *method.response.header.Access-Control-Allow-Origin : *responseTemplates:application/json: |{}responses:200:description: Default response for CORS methodheaders:Access-Control-Allow-Headers:type: stringAccess-Control-Allow-Methods:type: stringAccess-Control-Allow-Origin:type: stringpost:summary: Save EmployeeoperationId: saveemployeestags:- saveemployeesrequestBody:required: truecontent:application/json:schema:$ref: #/components/schemas/employeeresponses:200:description: Saved Employee Successfullyheaders:Access-Control-Allow-Origin:schema:type: stringcontent:application/json:schema:$ref: #/components/schemas/employee400:description: Application Errorsheaders:Access-Control-Allow-Origin:schema:type: stringcontent:application/json:schema:$ref: #/components/schemas/ErrorResponse500:description: Other unspecified Errorsheaders:Access-Control-Allow-Origin:schema:type: stringcontent:application/json:schema:$ref: #/components/schemas/ErrorResponsex-amazon-apigateway-integration:credentials:Fn::Sub: arn:aws:iam::${AWS::AccountId}:role/delegate-admin-lambda-proxy-pattern-rolehttpMethod: POSTuri:Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ProxyPatternService.Arn}:live/invocationsresponses:default:statusCode: 200responseParameters:method.response.header.Access-Control-Allow-Origin : *BAD.*:statusCode: 400responseParameters:method.response.header.Access-Control-Allow-Origin : *INT.*:statusCode: 500responseParameters:method.response.header.Access-Control-Allow-Origin : *type: aws_proxyget:summary: Get All the EmployeesoperationId: getemployeestags:- getemployeesresponses:200:description: All The Employees retrieved successfullyheaders:Access-Control-Allow-Origin:schema:type: stringcontent:application/json:schema:type: arrayitems:$ref: #/components/schemas/employee204:description: Employees not foundheaders:Access-Control-Allow-Origin:schema:type: string500:description: Other unspecified Errorsheaders:Access-Control-Allow-Origin:schema:type: stringcontent:application/json:schema:$ref: #/components/schemas/ErrorResponsex-amazon-apigateway-integration:credentials:Fn::Sub: arn:aws:iam::${AWS::AccountId}:role/delegate-admin-lambda-proxy-pattern-rolehttpMethod: POSTuri:Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ProxyPatternService.Arn}:live/invocationsresponses:default:statusCode: 200responseParameters:method.response.header.Access-Control-Allow-Origin : *BAD.*:statusCode: 204responseParameters:method.response.header.Access-Control-Allow-Origin : *INT.*:statusCode: 500responseParameters:method.response.header.Access-Control-Allow-Origin : *type: aws_proxycomponents:schemas:Employee:type: objectdescription: EMPLOYEEproperties:employeeId:type: stringdescription: Id of the EmployeeemployeeName:type: stringdescription: Name of the Employeerequired:- employeeIdErrorResponse:type: objectproperties:errorCode:type: stringdescription: |indicates an error in processing.XXXX - Error in saving messageerrorMessage:type: stringdescription: message description of error.以下是 AWS Lambda 函数的云形成模板该模板将通过 AWS API Gateway 公开。格式为 YAML。此处已引用OpenAPI定义/openapi-apigateway-PxyPtrnSvc.yaml文件 ( )以便API Gateway了解通过 OpenAPI 定义文件定义的集成点。
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: AWS Serverless Quarkus HTTP - Proxy Pattern Poc Service
Globals:Api:EndpointConfiguration:Type: PRIVATEAuth:ResourcePolicy:CustomStatements: {Effect: Allow,Action: execute-api:Invoke,Resource: [execute-api:/*/*/*],Principal: *}
Resources:ProxyPatternServiceAWSApiGateway:Type: AWS::Serverless::ApiProperties:TracingEnabled: trueName: ProxyPatternServiceSvcApiGatewayStageName: devDefinitionBody:Fn::Transform:Name: AWS::IncludeParameters:Location: ./openapi-apigateway-PxyPtrnSvc.yamlProxyPatternServiceLogGroup:Type: AWS::Logs::LogGroupProperties:LogGroupName: /aws/lambda/ProxyPatternServiceRetentionInDays: 30ProxyPatternService:Type: AWS::Serverless::FunctionProperties:Handler: not.used.in.provided.runtimeRuntime: providedCodeUri: component1/function.zipMemorySize: 512Timeout: 900FunctionName: ProxyPatternServiceEnvironment:Variables:LOG_LEVEL: INFODISABLE_SIGNAL_HANDLERS: trueRole: !Sub arn:aws:iam::${AWS::AccountId}:role/delegate-admin-lambda-proxy-pattern-roleTracing: Active
Outputs:ProxyPatternServiceAWSApiGateway:Description: API Gateway endpoint URL for dev stage for Proxy Pattern Service TemplateValue: !Sub https://${ProxyPatternServiceAWSApiGateway}.execute-api.${AWS::Region}.amazonaws.com/dev/ProxyPatternServiceAWSApiGatewayRestApiId:Description: API Gateway ARN for Basic AWS API GatewayValue: !Ref ProxyPatternServiceAWSApiGatewayExport:Name: ProxyPatternServiceAWSApiGateway-RestApiIdProxyPatternServiceAWSApiGatewayRootResourceId:Value: !GetAtt ProxyPatternServiceAWSApiGateway.RootResourceIdExport:Name: ProxyPatternServiceAWSApiGateway-RootResourceId示例 API 定义可以与以下使用 Java 的 Controller 接口相关并且可以在 Java Quarkus 中实现
package com.example.proxypattern.controller;import com.example.proxypattern.exception.model.ErrorResponse;
import com.example.proxypattern.model.Employee;import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;Path(v1/proxypattern/)
public interface IProxyPatternController {GETPath(/employee)Produces(MediaType.APPLICATION_JSON)Operation(summary Get All the Employees, description getEmployees)APIResponses(value {APIResponse(responseCode 204, description Employees not found),APIResponse(responseCode 200, description All The Employees retrieved successfully, content Content(schema Schema(implementation Employee.class))),APIResponse(responseCode 500, description Other unspecified Errors, content Content(schema Schema(implementation ErrorResponse.class))) })public Response getAllEmployee();POSTPath(/employee)Consumes(MediaType.APPLICATION_JSON)Produces(MediaType.APPLICATION_JSON)Operation(summary Save Employee, description saveEmployees)APIResponses(value {APIResponse(responseCode 400, description Application Errors, content Content(schema Schema(implementation ErrorResponse.class))),APIResponse(responseCode 200, description Saved Employee Successfully, content Content(schema Schema(implementation Employee.class))),APIResponse(responseCode 500, description Other unspecified Errors, content Content(schema Schema(implementation ErrorResponse.class))) })public Response saveEmployee(Employee employee);
}实现语言可能有所不同。如果技术决策是使用Java作为编码语言则可以选择Java Quarkus作为Lambda函数的实现语言。Lambda 支持自定义运行时因此可以本地构建 Quarkus 代码来解决与 Lambda 冷启动相关的响应时间问题。
控制器实现代码如下所示。这里业务逻辑已通过业务服务类进行抽象
package com.example.proxypattern.controller.impl;import com.example.proxypattern.controller.IProxyPatternController;
import com.example.proxypattern.model.Employee;
import com.example.proxypattern.service.impl.EmployeeServiceImpl;import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.opentracing.Traced;import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.core.Response;import java.util.List;Slf4j
Traced
Singleton
public class ProxyPatternControllerImpl implements IProxyPatternController {InjectEmployeeServiceImpl employeeService;Overridepublic Response getAllEmployee() {Response.Status status Response.Status.OK;//The business logic is abstracted here in Service codeListEmployee listOfEmployee employeeService.getAllEmployee();if(listOfEmployee.isEmpty()){status Response.Status.NO_CONTENT;}log.info(Sending employee {}, listOfEmployee);return Response.status(status).entity(listOfEmployee).build();}Overridepublic Response saveEmployee(Employee employee) {//The business logic is abstracted here in Service codeEmployee employeeResponse employeeService.saveEmployee(employee);return Response.status(Response.Status.CREATED).entity(employeeResponse).build();}
}可以使用Maven来构建项目也可以使用以下依赖来构建项目 dependenciesdependencygroupIdio.quarkus/groupIdartifactIdquarkus-resteasy/artifactId/dependencydependencygroupIdio.quarkus/groupIdartifactIdquarkus-amazon-lambda-rest/artifactId/dependencydependencygroupIdio.quarkus/groupIdartifactIdquarkus-smallrye-openapi/artifactId/dependencydependencygroupIdio.quarkus/groupIdartifactIdquarkus-resteasy-jackson/artifactId/dependencydependencygroupIdio.quarkus/groupIdartifactIdquarkus-logging-json/artifactId/dependencydependencygroupIdorg.jboss.slf4j/groupIdartifactIdslf4j-jboss-logmanager/artifactId/dependencydependencygroupIdcommons-logging/groupIdartifactIdcommons-logging/artifactIdversion1.2/version/dependencydependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-annotations/artifactId/dependencydependencygroupIdio.quarkus/groupIdartifactIdquarkus-hibernate-validator/artifactId/dependencydependencygroupIdio.quarkus/groupIdartifactIdquarkus-smallrye-opentracing/artifactId/dependencydependencygroupIdio.quarkus/groupIdartifactIdquarkus-smallrye-health/artifactId/dependencydependencygroupIdio.quarkus/groupIdartifactIdquarkus-junit5/artifactIdscopetest/scope/dependencydependencygroupIdio.rest-assured/groupIdartifactIdrest-assured/artifactIdscopetest/scope/dependencydependencygroupIdio.quarkus/groupIdartifactIdquarkus-junit5-mockito/artifactIdscopetest/scope/dependencydependencygroupIdorg.jacoco/groupIdartifactIdorg.jacoco.agent/artifactIdclassifierruntime/classifierscopetest/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.20/versionscopeprovided/scope/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-lang3/artifactId/dependency/dependencies 结论 AWS API Gateway 集成可以轻松使用 Lambda Function 实现 REST API该函数符合 OpenAPI 标准并且可以轻松与 AWS API Gateway 集成。