谈到了微服务, 我们有许多不同的方法来开发微服务; 我们可以使用 Docker, Kubernetes。Docker, Kubernetesg 是个很好的工具, 但, 同时也需要相当长时间的学习与经验的积累, 才能完全的掌握。为了要节省学习的时间, 我们往往就会使用 AWS Lambda 来开发微服务。

We have many different approaches to developing microservices; We can use Docker, Kubernetes. Docker, Kubernetes is a good tool, but at the same time, it takes a long time to learn and accumulate experience to masterfully. To save time on learning,   we often use AWS Lambda to develop microservices.

使用 AWS Lambda 来开发微服务, 我们就只需要用著自己所熟悉的编程语言与第三方的库、专注的在需求中的商业逻辑的开发。当开发完成时, 将我们所开发的代码与第三方的库, 压缩成一个 zip 文件。 然后, 将 zip 文件上传至 AWS Serverless 的云服务平台上即可。

Using AWS Lambda to develop microservices, we only need to use the programming language we are familiar with, third-party libraries and focus on developing business logic in the requirements. When development is complete, we compress the code we developed into a zip file with a third-party library. Then, upload the zip file to AWS Serverless’s cloud service platform.

在这篇文章中, 每一个 AWS Lambda Function 就代表著是一个微服务。

In this article, each AWS Lambda Function represents a microservice.

我们将在 AWS Lambda Function 中使用 node-fetch。

We will use node-fetch in the AWS Lambda Function.

关于 node-fetch 使用的方法, 请参考:

For the method used by node-fetch, please refer to :

当 node-fetch 遇到了 async/await

微服务: BookHotel

Microservices: BookHotel

  • BookHotel, 主要是当用户提供了所想要去旅游的景点与旅游的时间点, BookHotel 便会为用户预订好酒店。
  • BookHotel, mainly when the user provides the attractions and travel time points they want to travel, BookHotel will book a good hotel for the user.
  • BookHotel 接受 JSON 格式的信息; 如下的例子:
  • BookHotel accepts information in JSON format; The following example:
{
  "buyer_id": "mariano",      // 用户代码
  "start_date": "2020-03-13",  // 旅游开始的日期
  "end_date": "2020-03-15",  //  旅游结束的日期
  "near": "tate gallery"       //  旅游的景点
} 
{
  "start_date": "2020-03-13",
  "end_date": "2020-03-15",
  "reservation_id": "UARPQ",
  "name": "L3"
} 

AWS Lambda Function 代码如下:

The AWS Lambda Function code is as follows:

exports.handler = async (env) => {
    // hotel's booking process
    try {
        let response = await fetch('https://xxx.execute-api.ap-northeast-1.amazonaws.com/Prod/hotel', {
            method: 'POST',
            body: JSON.stringify(env),
            headers: {'Content-Type': 'application/json'}
        })
        let body = await checkResponseStatus(response);
        return await body.json();
    } catch (err) {
        console.error(err.message);
    }
}

AWS Console:

开发完成后, 将我们所开发的代码与第三方的库, 压缩成一个 zip 文件。 然后, 将 zip 文件上传至 AWS Serverless 的云服务平台上。

Once developed, we compress the code we developed into a zip file with a third-party library. Then, upload the zip file to AWS Serverless’s cloud services platform

完整的代码请参考:

For the complete code, please refer to :

https://github.com/KenFang/Book-Hotel

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据