使用 API Gateway Integrator 在 Quarkus 中实施适用于 AWS Lambda 的 OpenAPI

这篇具有很好参考价值的文章主要介绍了使用 API Gateway Integrator 在 Quarkus 中实施适用于 AWS Lambda 的 OpenAPI。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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 参考实现 

参考架构

使用 API Gateway Integrator 在 Quarkus 中实施适用于 AWS Lambda 的 OpenAPI,gateway,aws,云计算

 

参考实现由以下组件组成:

  • AWS Lambda函数实现业务需求/逻辑 
  • API 网关将功能公开为 API
  • Amazon 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: v1
  title: "Proxy Pattern Service"
paths:
  /v1/proxypattern/employee:
    options:
      summary: CORS support
      description: |
        Enable CORS by returning correct headers
      consumes:
        - application/json
      produces:
        - application/json
      tags:
        - CORS
      security:
        - NONE: []
      x-amazon-apigateway-integration:
        type: mock
        requestTemplates:
          application/json: |
            {
              "statusCode" : 200
            }
        responses:
          "default":
            statusCode: "200"
            responseParameters:
              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-Control'"
              method.response.header.Access-Control-Allow-Methods : "'*'"
              method.response.header.Access-Control-Allow-Origin : "'*'"
            responseTemplates:
              application/json: |
                {}
      responses:
        200:
          description: Default response for CORS method
          headers:
            Access-Control-Allow-Headers:
              type: "string"
            Access-Control-Allow-Methods:
              type: "string"
            Access-Control-Allow-Origin:
              type: "string"
    post:
      summary: "Save Employee"
      operationId: "saveemployees"
      tags:
        - saveemployees
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/employee'
      responses:
        '200':
          description: "Saved Employee Successfully"
          headers:
            Access-Control-Allow-Origin:
              schema:
                type: "string"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/employee"
        '400':
          description: "Application Errors"
          headers:
            Access-Control-Allow-Origin:
              schema:
                type: "string"
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

        '500':
          description: "Other unspecified Errors"
          headers:
            Access-Control-Allow-Origin:
              schema:
                type: "string"
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

      x-amazon-apigateway-integration:
        credentials:
          Fn::Sub: arn:aws:iam::${AWS::AccountId}:role/delegate-admin-lambda-proxy-pattern-role
        httpMethod: "POST"
        uri:
          Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ProxyPatternService.Arn}:live/invocations
        responses:
          "default":
            statusCode: "200"
            responseParameters:
              method.response.header.Access-Control-Allow-Origin : "'*'"
          "BAD.*":
            statusCode: "400"
            responseParameters:
              method.response.header.Access-Control-Allow-Origin : "'*'"
          "INT.*":
            statusCode: "500"
            responseParameters:
              method.response.header.Access-Control-Allow-Origin : "'*'"
        type: "aws_proxy"
    get:
      summary: "Get All the Employees"
      operationId: "getemployees"
      tags:
        - getemployees
      responses:
        '200':
          description: "All The Employees retrieved successfully"
          headers:
            Access-Control-Allow-Origin:
              schema:
                type: "string"
          content:
            application/json:
              schema:
                type: "array"
                items:
                  $ref: "#/components/schemas/employee"
        '204':
          description: "Employees not found"
          headers:
            Access-Control-Allow-Origin:
              schema:
                type: "string"
        '500':
          description: "Other unspecified Errors"
          headers:
            Access-Control-Allow-Origin:
              schema:
                type: "string"
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

      x-amazon-apigateway-integration:
        credentials:
          Fn::Sub: arn:aws:iam::${AWS::AccountId}:role/delegate-admin-lambda-proxy-pattern-role
        httpMethod: "POST"
        uri:
          Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ProxyPatternService.Arn}:live/invocations
        responses:
          "default":
            statusCode: "200"
            responseParameters:
              method.response.header.Access-Control-Allow-Origin : "'*'"
          "BAD.*":
            statusCode: "204"
            responseParameters:
              method.response.header.Access-Control-Allow-Origin : "'*'"
          "INT.*":
            statusCode: "500"
            responseParameters:
              method.response.header.Access-Control-Allow-Origin : "'*'"
        type: "aws_proxy"

components:
  schemas:
    Employee:
      type: object
      description: EMPLOYEE
      properties:
        employeeId:
          type: string
          description: Id of the Employee
        employeeName:
          type: string
          description: Name of the Employee
      required:
        - employeeId
    ErrorResponse:
      type: "object"
      properties:
        errorCode:
          type: string
          description: |
            indicates an error in processing.
            XXXX - Error in saving message
        errorMessage:
          type: string
          description: "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: PRIVATE
    Auth:
      ResourcePolicy:
        CustomStatements: {
          Effect: 'Allow',
          Action: 'execute-api:Invoke',
          Resource: ['execute-api:/*/*/*'],
          Principal: '*'
        }
Resources:
  ProxyPatternServiceAWSApiGateway:
    Type: AWS::Serverless::Api
    Properties:
      TracingEnabled: true
      Name: ProxyPatternServiceSvcApiGateway
      StageName: dev
      DefinitionBody:
        'Fn::Transform':
          Name: 'AWS::Include'
          Parameters:
            Location: './openapi-apigateway-PxyPtrnSvc.yaml'
  ProxyPatternServiceLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: '/aws/lambda/ProxyPatternService'
      RetentionInDays: 30

  ProxyPatternService:
    Type: AWS::Serverless::Function
    Properties:
      Handler: not.used.in.provided.runtime
      Runtime: provided
      CodeUri: component1/function.zip
      MemorySize: 512
      Timeout: 900
      FunctionName: ProxyPatternService
      Environment:
        Variables:
          LOG_LEVEL: INFO
          DISABLE_SIGNAL_HANDLERS: true
      Role: !Sub "arn:aws:iam::${AWS::AccountId}:role/delegate-admin-lambda-proxy-pattern-role"
      Tracing: Active
Outputs:
  ProxyPatternServiceAWSApiGateway:
    Description: 'API Gateway endpoint URL for dev stage for Proxy Pattern Service Template'
    Value: !Sub 'https://${ProxyPatternServiceAWSApiGateway}.execute-api.${AWS::Region}.amazonaws.com/dev/'
  ProxyPatternServiceAWSApiGatewayRestApiId:
    Description: 'API Gateway ARN for Basic AWS API Gateway'
    Value: !Ref ProxyPatternServiceAWSApiGateway
    Export:
      Name: ProxyPatternServiceAWSApiGateway-RestApiId
  ProxyPatternServiceAWSApiGatewayRootResourceId:
    Value: !GetAtt ProxyPatternServiceAWSApiGateway.RootResourceId
    Export:
      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 {

    @GET
    @Path("/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();

    @POST
    @Path("/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 {

    @Inject
    EmployeeServiceImpl employeeService;

    @Override
    public Response getAllEmployee() {
        Response.Status status = Response.Status.OK;
        //The business logic is abstracted here in Service code
        List<Employee> listOfEmployee = employeeService.getAllEmployee();
        if(listOfEmployee.isEmpty()){
            status = Response.Status.NO_CONTENT;
        }
        log.info("Sending employee {}", listOfEmployee);
        return Response.status(status).entity(listOfEmployee).build();
    }

    @Override
    public Response saveEmployee(Employee employee) {
    	//The business logic is abstracted here in Service code
        Employee employeeResponse = employeeService.saveEmployee(employee);
        return Response.status(Response.Status.CREATED).entity(employeeResponse).build();
    }
}

可以使用Maven来构建项目,也可以使用以下依赖来构建项目:


<dependencies>
      
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-resteasy</artifactId>
        </dependency>

        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-amazon-lambda-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-smallrye-openapi</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-resteasy-jackson</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-logging-json</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jboss.slf4j</groupId>
            <artifactId>slf4j-jboss-logmanager</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-hibernate-validator</artifactId>
        </dependency>

        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-smallrye-opentracing</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-smallrye-health</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-junit5-mockito</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.agent</artifactId>
            <classifier>runtime</classifier>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
    </dependencies>

结论

AWS API Gateway 集成可以轻松使用 Lambda Function 实现 REST API,该函数符合 OpenAPI 标准,并且可以轻松与 AWS API Gateway 集成。文章来源地址https://www.toymoban.com/news/detail-642888.html

到了这里,关于使用 API Gateway Integrator 在 Quarkus 中实施适用于 AWS Lambda 的 OpenAPI的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用AWS Lambda函数的最佳实践!

    主题 函数代码 函数配置 指标和警报 处理流 安全最佳实践 有关 Lambda 应用程序最佳实践的更多信息,请参阅 Serverless Land 中的 Application design。 从核心逻辑中分离 Lambda 处理程序。这样您可以创建更容易进行单元测试的函数。在 Node.js 中可能如下所示: 利用执行环境重用来提

    2024年02月07日
    浏览(38)
  • 在AWS Lambda中使用FFmpeg处理m3u8视频流

    在直播里领域,我们经常需要对视频流进行处理。FFmpeg则是该领域中处理的利器。这篇文章,我们将以视频流截图为例,讲解如何在AWS Lambda中对m3u8视频流进行截图。因为Lambda是无服务架构,其本地存储是不可靠的,于是最后我们需要将截图文件上传S3桶。 这块的方案可以见

    2024年02月03日
    浏览(43)
  • 【Terraform学习】使用 Terraform创建Lambda函数启动EC2(Terraform-AWS最佳实战学习)

     本站以分享各种运维经验和运维所需要的技能为主 《python》:python零基础入门学习 《shell》:shell学习 《terraform》持续更新中:terraform_Aws学习零基础入门到最佳实战 《k8》暂未更新 《docker学习》暂未更新 《ceph学习》ceph日常问题解决分享 《日志收集》ELK+各种中间件 《运

    2024年02月11日
    浏览(38)
  • vivado 使用IP Integrator源

    使用IP Integrator源 在Vivado Design Suite中,您可以在RTL中添加和管理IP子系统块设计(.bd)项目或设计。使用Vivado IP集成程序,您可以创建IP子系统块设计。IP集成程序使您能够通过实例化和将Vivado IP目录中的多个IP核互连。可以交互创建设计通过Vivado IDE中的IP integrator画布或通过

    2024年01月18日
    浏览(26)
  • AWS Lambda函数实战

    实战效果:开发一个函数,它会关注事件中的某个名字,并返回“Hello名字!”。如果输入事件没有提供名字,则函数返回一个更加通用的问候语“Hello World!”。 AWS Lambda接口可以通过AWS命令行或可在服务器、浏览器及移动设备上运行的AWS软件开发包(SDK)来调用。 首先要在

    2024年01月22日
    浏览(49)
  • aws lambda入门之基础

    文档:AWS Lambda foundations - AWS Lambda Lambda 函数是 Lambda 的基本。 可以使用 Lambda 控制台、Lambda API、AWS CloudFormation 或 AWS SAM 配置函数。 Lambda 在事件发生时调用该函数。 Lambda 并行运行您的函数的多个实例,并受并发和扩展限制的约束。 Lambda 运行函数实例来处理事件。 可以使用

    2024年02月01日
    浏览(41)
  • AWS中Lambda集成SNS

    在Lambda中,创建名为AWSSNSDemo的函数 创建名为:SNSTopicTest的主题        发布消息后,在DynamoDB中, 如果检测到发布的消息数据,则说明集成成功。  

    2024年02月13日
    浏览(36)
  • AWS Lambda 操作 RDS 示例

    创建一个 Lambda 接收调用时传入的数据, 写入 RDS 数据库 Post 表存储文章信息. 表结构如下: id title content create_date 1 我是标题 我是正文内容 2023-10-21 15:20:00 RDS 控制台创建 MySQL 实例, 不允许 Public access (后面 Lambda 需要通过 VPC 访问) Secrets Manager 控制台创建 Credentials for Amazon RDS da

    2024年02月08日
    浏览(36)
  • 【SERVERLESS】AWS Lambda上实操

    通过Serverless的发展历程及带给我们的挑战,引出我们改如何改变思路,化繁为简,趋利避害,更好的利用其优势,来释放企业效能,为创造带来无限可能。 无服务器计算近年来与云原生计算都是在互联网背景下产生,其顾名思义是指开发者在构建和运行应用时无需管理服务

    2024年04月15日
    浏览(33)
  • AWS Lambda - 同步/异步调用,事件源,目标

    Hello大家好,我们今天继续讨论AWS Lambda的内容。 Lambda函数有三种调用方式。 第一种方式是同步调用。 当我们使用API、CLI以及API网关等调用函数时,就是同步调用。 当您同步调用函数时,Lambda会运行该函数并等待响应,当函数完成时将结果同步返回,所以您的代码将等待这个

    2024年02月10日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包