Easily build applets based Serverless architecture

Foreword

Since 2017, the first line on a number of small programs, more and more of the mobile terminal applications presented in the form of applets. Applet has fingertips, run advantage and go, which greatly reduces the burden on the user, the small program has been widely disseminated. Ali Baba, a small program is also widely used in Taobao / Alipay / nails / High German and other platforms.

In order to support a large number of small programs, the challenges faced by the server are:

  • A large number of small program is not active, the traditional way of at least one server will be a waste of resources;
  • Call the program a small amount of surge in the peak of activity, it requires the server can quickly be resilient and elastic.

The small application developers tend to be client / front-end developer, focus more on fast service and application development line of code, and inadvertently leisure care service side of the operation and maintenance operations.

Ali cloud function computing is a fully hosted Serverless Computing Services, so developers do not need infrastructure management servers, just write and upload code, you can build reliable, resilient and secure service.

Function to calculate the elastic-free operation and maintenance, efficient and safe feature is very suitable as a small server program.

solution

Function calculates a set of packages applet server templates to help application developers to quickly build small applet-based functions calculated.

Use this template to build a small application program has the following features:

  • Operation and maintenance of high efficiency: no management server function can be deployed on-line
  • Development of high efficiency: based on encapsulated data interface, direct the development of service code
  • Zero cost startup: server-based computing functions, the database used to store tables, are paid according to the amount of free and have a greater amount

Workflow applet

Build process
A complete Alipay applet requires the following elements:

  • Alipay App: support Alipay is a small program that runs end user's mobile phone
  • The client applet: applet is presented to the user screen
  • Applet server: is a small program logic processing unit, such as the user authentication and data access
  • Alipay service side: a logic processing unit Alipay App, including the user's identity information

Function calculates a set of packages applet server templates to help application developers to quickly build small applet-based functions calculated. The following describes the work flow server template.

FC server template workflow

In addition to operation and maintenance of infrastructure problems, the server mainly to solve two common problems:

  1. 身份认证: 服务端提供的 API 如何对客户端的请求进行鉴权?客户端的信息如果泄露如何保证数据安全?
  2. 数据访问: 客户端如何进行数据库操作和文件操作?

这是每一个小程序开发都会遇到的共性的问题,可以通过一些框架或者模板来避免重复建设。因此我们基于函数计算开发了一个小程序应用模板,解决了这些通用的问题,让开发者能够专注在业务逻辑上,快速开发上线自己的小程序。

Server workflow

  1. 客户端小程序通过支付宝 App 的 API,获得 authCode,这个过程会在 App 中弹出用户授权框;
  2. 小程序客户端向小程序服务端发起 createSession 请求,用于初始化的身份认证;
  3. 小程序服务端将 authCode 传给支付宝服务端,支付宝服务端校验 authCode,返回 accessToken;
  4. 小程序服务端根据收到的 accessToken 生成一个 sessionId 和一个 STS token,在服务端记录这 2 个信息然后把它们返回给客户端。STS token 是用于访问服务端函数的凭证,而 sessionId 作为前面认证成功而建立的会话信息,通过这 个sessionId 可以识别当前请求的用户信息;
  5. 小程序客户端再次需要获取数据,带着证明自己身份的 sessionId 向小程序服务端发起获取数据请求;
  6. 小程序服务端首先根据 sessionId 获取用户身份信息,再根据用户信息获取相关数据;
  7. 将用户数据返回给小程序客户端。

搭建流程

注册支付宝小程序

  • 准备工作:注册支付宝开发者账号
    如果您尚未注册支付宝开发者账号,使用支付宝账号登录 蚂蚁金服开放平台,并完成开发者身份注册。详细信息请参见 开发者入驻说明
  • 创建支付宝小程序应用

    APPID

    • 生成密钥,需要在小程序服务端与支付宝服务端配置密钥,对交易数据进行双方校验
    • 将公钥配置在 【设置】->【开发设置】-【接口加签方式】,并记录私钥

搭建小程序服务端

  • 准备工作

  • 部署应用

    • 进入控制台应用中心,选择 Mini App Todo List Server Template 模板,填写刚才记录的 APPID 与 PrivateKey,为 DBInstance/LogProject 取个自定义的名字(如果名字已被占用,会报 Already Exists 的错误),部署应用

搭建小程序客户端

fcAccountId 阿里云AccountId
fcRegion 函数部署的区域,例如cn-shanghai
fcServiceName 函数计算服务名称
sessionFunctionName session函数的名称,例如session
dbFunctionName db函数的名称,例如db

由于 fc-nodejs-sdk 使用了ES6语法,在发布小程序时,需要在小程序项目的根目录下,新建一个文件mini.project.json,填入以下内容:

{
  "node_modules_es6_whitelist": [
     "@alicloud/fc2"
  ]
}

So far, the development has been completed, you can debug, on-line your applet friends.

to sum up

After the applet line with the increase in traffic during the visit or the sudden increase in activity on the back-end service stability and flexibility it is also a great test. Function calculates the upload code to run, greatly enhancing the efficiency of the development of back-end services; elastically stretchable hybrid mode, to easily respond to load changes. Server template and the template can quickly build client-based applet function computing applications, these features make the function of computing a support applet good choice.

Guess you like

Origin yq.aliyun.com/articles/745238