compute 和host_使用Function Compute和MongoDB构建无服务器REST API

compute 和host

We received a lot of great entries in our recent competition to find the best tip for making the most out of Alibaba Cloud services. It was a fun but challenging task for our judges to pick the winners amongst so many helpful and interesting entries. But alas after fiery deliberations and heated debates they’ve decided that the second prize of the competition goes to Nhi Nam Ha. His winning tip is a part of a series on serverless app architecture on Alibaba Cloud, and it covers several Alibaba products.

在最近的竞赛中,我们收到了很多精彩的作品,以找到充分利用阿里云服务的最佳技巧。 对于我们的评委来说,从众多有用和有趣的参赛作品中选出获奖者是一项有趣而艰巨的任务。 但是,经过激烈的讨论和激烈的辩论,他们决定将比赛的二等奖授予Nhi Nam Ha。 他的获奖秘诀是有关阿里云上无服务器应用架构系列文章的一部分,其中涵盖了几种阿里巴巴产品。

This tutorial will show you how to deploy a MongoDB database on Alibaba Cloud and use Function Compute to develop a back-end API system to interact with the database.

本教程将向您展示如何在阿里云上部署MongoDB数据库以及如何使用Function Compute开发后端API系统以与数据库进行交互。

NoSQL和MongoDB概述 (Overview of NoSQL and MongoDB)

Relational databases have been selected as the primary system to manage data in software development for a long time. Its ACID principals promote the data persistency, transaction integrity and concurrency control. Over the last few years, NoSQL (Not only SQL) has become popular. This model solves the impedance mismatch between the relational data structures (tables, rows, fields) and the in-memory data structures of the application (objects). Most importantly, NoSQL is designed to scale horizontally which makes it an excellent choice for modern web applications.

关系数据库已被选为主要的系统,用于长期管理软件开发中的数据。 它的ACID主体可促进数据持久性,事务完整性和并发控制。 在过去的几年中,NoSQL(不仅限于SQL)已变得流行。 该模型解决了关系数据结构(表,行,字段)和应用程序(对象)的内存数据结构之间的阻抗不匹配的问题。 最重要的是,NoSQL设计为可水平扩展,这使其成为现代Web应用程序的绝佳选择。

NoSQL could be categorized into 4 groups:

NoSQL可以分为4组:

  • Key-Value

    核心价值
  • Document

    文件
  • Column family

    列族
  • Graph

    图形

MongoDB is the most popular system within the document database group. As defined on mongodb.com,

MongoDB是文档数据库组中最受欢迎的系统。 根据mongodb.com上的定义,

A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.

MongoDB中的记录是一个文档,它是由字段和值对组成的数据结构。 MongoDB文档类似于JSON对象。 字段的值可以包括其他文档,数组和文档数组。

阿里云云数据库MongoDB (Alibaba Cloud ApsaraDB for MongoDB)

MongoDB databases can be deployed on Alibaba Cloud via its ApsaraDB for MongoDB service. Users can select among 3 pricing schemes:

MongoDB数据库可以通过其ApsaraDB for MongoDB服务部署在阿里云上。 用户可以选择以下三种定价方案:

  • Subscription (Replica Set)

    订阅(副本集)
  • PAYG (Pay-As-You-Go) (Replica Set)

    PAYG(现收现付)(复制品套装)
  • PAYG (Sharding)

    PAYG(共享)

Replication and sharding refers to the data distribution models:

复制和分片是指数据分发模型:

  • Replication copies data across multiple servers. The same piece of data is stored in different places. ApsaraDB for MongoDB uses a 3-server replica set. Replication may help with horizontal scaling of reads.

    复制跨多个服务器复制数据。 相同的数据存储在不同的位置。 ApsaraDB for MongoDB使用3服务器副本集。 复制可能有助于水平扩展读取。
  • Sharding distributes data across multiple machines so different machine contains different subset of data. This model allows for horizontal scaling of writes.

    分片将数据分布在多台计算机上,因此不同的计算机包含不同的数据子集。 该模型允许写入的水平缩放。

创建一个实例 (Create an Instance)

In the Alibaba Cloud console, click on Products and you will see ApsaraDB for MongoDB under the ApsaraDB group. Alternatively, you can use the search box to filter the desired service.

在阿里云控制台中,单击“产品”,您将在ApsaraDB组下看到适用于MongoDB的ApsaraDB。 或者,您可以使用搜索框过滤所需的服务。

Select the pricing scheme, the region, the server specification, and set a password for your database. Alibaba Cloud will tell you how much the service cost you based on what you chose.

选择定价方案,区域,服务器规格,然后为数据库设置密码。 阿里云将根据您的选择告诉您服务费用。

More info about the instance parameters is here.

有关实例参数的更多信息在这里

Note: if you are using a free trial account, remember to select a subscription instance. PAYG instances do not include in the trial program.

注意:如果您使用的是免费试用帐户,请记住选择一个订阅实例。 PAYG实例不包括在试用程序中。

设置IP白名单 (Set IP Whitelists)

To ensure database security Alibaba Cloud automatically block all access to the database. You have to specify IP addresses in the whitelist to gain access to the target instance.

为确保数据库安全,阿里云会自动阻止所有对数据库的访问。 您必须在白名单中指定IP地址才能访问目标实例。

ip

连接字符串 (Connection String)

After you set your IP whitelist, click on “Database Connection” to see the connection parameters

设置IP白名单后,单击“ 数据库连接 ”以查看连接参数

connection

Use this connection string to connect to the database in your Node.js code.

使用此连接字符串连接到Node.js代码中的数据库。

函数计算 (Function Compute)

Function Compute lets you run code without provisioning or managing servers. This service prepares computing resources for you and runs your codes on your behalf elastically and reliably. You only pay for resources actually consumed when running the codes. If your code isn’t executed, you don’t pay.

通过功能计算,您可以在不配置或管理服务器的情况下运行代码。 该服务为您准备计算资源,并代您灵活,可靠地运行代码。 您只需为运行代码时实际消耗的资源付费。 如果您的代码未执行,则无需付款。

Function Compute runs your code in response to events. When the event source service triggers an event, the associated function is automatically called to process the event.

Function Compute响应事件运行您的代码。 当事件源服务触发事件时,将自动调用关联的函数来处理事件。

From the Alibaba Cloud console, select Function Compute and click on the “+” icon to create a new service

在阿里云控制台中,选择“函数计算”,然后单击“ + ”图标以创建新服务。

1

In the newly created service, click on the “+” icon to create a new function. You will go through a multi-step wizard to select options for your function.

在新创建的服务中,单击“ + ”图标以创建新功能。 您将通过一个多步骤向导来选择功能选项。

For Function Template, select “Empty Function”

对于功能模板,选择“空功能”

2

In the “Configure Triggers” step, select “HTTP Trigger” and give it a name. Other settings are as the image below

在“配置触发器”步骤中,选择“ HTTP触发器”并为其命名。 其他设置如下图

trigger

In the “Configure Function Settings” step, set a name for your function and select “nodejs6” as runtime

在“配置功能设置”步骤中,为您的功能设置一个名称,然后选择“ nodejs6”作为运行时

4

Click “Next” in the last two steps to finish the wizard and create the fucntion.

在最后两个步骤中,单击“下一步”以完成向导并创建功能。

编写功能 (Program Your Function)

Click on the function you have just created and click on the “Code” tab. This is where you provide your code for the function to run

单击刚刚创建的函数,然后单击“代码”选项卡。 在此提供您要运行的函数的代码

code

Use the connection string from your MongoDB server.

使用您的MongoDB服务器中的连接字符串。

Also in this screen you can view the HTTP trigger that will invoke your function. You can also run the trigger to test you function here.

同样在此屏幕中,您可以查看将调用您的函数的HTTP触发器。 您也可以运行触发器以测试您的功能。

run

摘要 (Summary)

In this tutorial we have learnt about NoSQL database with MongoDB as a popular example. Alibaba Cloud provides its ApsaraDB for MongoDB service to those who want to run MongoDB servers on its cloud. The tutorial then moves to discuss the Function Compute service as a new way to build your application following the emerging Serverless architecture. It shows an example of a Node.js function triggered by an HTTP request to connect to the MongoDB database and perform an “insert” command.

在本教程中,我们以MongoDB为例,了解了NoSQL数据库。 阿里云为想要在其云上运行MongoDB服务器的用户提供其MonsDB的ApsaraDB服务。 然后,本教程将讨论功能计算服务,这是一种根据新兴的无服务器架构构建应用程序的新方法。 它显示了一个由HTTP请求触发的Node.js函数示例,该请求连接到MongoDB数据库并执行“ insert ”命令。

翻译自: https://www.sitepoint.com/building-a-serverless-rest-api-with-function-compute-and-mongodb/

compute 和host

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值