Easily build small programs based on serverless architecture

foreword

Since the first batch of Mini Programs were launched in 2017, more and more mobile applications have been presented in the form of Mini Programs. Mini Programs have the advantages of being within reach and ready to go, which greatly reduces the user's burden of use and enables Mini Programs to be widely disseminated. In Alibaba, Mini Programs are also widely used on Taobao/Alipay/DingTalk/AutoNavi 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 programs are inactive, and the traditional method of at least one server will cause waste of resources;
  • During the peak activity period, the call volume of the applet surges, requiring the server to be able to perform elastic scaling quickly.

Small program developers are often client/front-end developers, and they focus more on developing business code and the rapid launch of applications, rather than worrying about server-side operation and maintenance.

Alibaba Cloud Function Compute is a fully managed serverless computing service that allows developers to build reliable, elastic, and secure services without managing servers and other infrastructure, just by writing and uploading code.

Function computing is flexible, free of operation and maintenance, efficient, and secure, and is very suitable for serving as a small program server.

solution

Function Compute encapsulates a set of applet server-side templates to help applet developers quickly build function computing-based applet.

Using this template to build an applet application has the following characteristics:

  • High operation and maintenance efficiency: no need to manage servers, deploy functions to go online
  • High development efficiency: directly develop business code based on the encapsulated data interface
  • Zero-fee startup: The server is based on function computing, and the database uses table storage, all of which are pay-as-you-go and have a large free quota

applet workflow

Build processA complete Alipay applet requires the following elements:

  • Alipay App: It is the carrier of Alipay applet and runs on the user's mobile phone
  • Mini Program Client: It is the operation page displayed by the Mini Program to the user
  • Small program server: is the logic processing unit of the applet, such as user authentication and data access
  • Alipay server: It is the logical processing unit of Alipay App, including the user's identity information

Function Compute encapsulates a set of applet server-side templates to help applet developers quickly build function computing-based applet. The following describes the workflow of the server-side template.

FC server-side template workflow

In addition to the operation and maintenance of infrastructure, the server mainly solves two general problems:

  1. Authentication: How does the API provided by the server authenticate the client's request? How to ensure data security if client information is leaked?
  2. Data Access: How does the client perform database operations and file operations?

This is a common problem that every small program development will encounter. Some frameworks or templates can be used to avoid repeated construction. Therefore, we developed a small program application template based on function computing to solve these common problems, allowing developers to focus on business logic and quickly develop and launch their own small programs.

Server-side workflow

  1. The client applet obtains the authCode through the API of the Alipay App, and the user authorization box will pop up in the App during this process;
  2. The applet client initiates a createSession request to the applet server for initial authentication;
  3. The applet server sends the authCode to the Alipay server, and the Alipay server verifies the authCode and returns accessToken;
  4. The applet server generates a sessionId and an STS token according to the received accessToken, records these two pieces of information on the server and returns them to the client. STS token is a credential used to access server-side functions, and sessionId is used as the session information established by the previous authentication successfully, through which sessionId can identify the currently requested user information;
  5. The applet client needs to obtain data again, and initiates a data acquisition request to the applet server with the sessionId that proves its identity;
  6. The applet server first obtains user identity information according to sessionId, and then obtains relevant data according to user information;
  7. Return user data to the applet client.

Build process

Register Alipay Mini Program

  • Preparations: Registering an Alipay developer account If you have not yet registered an Alipay developer account, use your Alipay account to log in to the Ant Financial Open Platform and complete the developer identity registration. For details, please refer to the developer settlement instructions .
  • Create an Alipay applet application
    • Use Alipay account to log in Ant Financial Open Platform Enter the developer center to create an applet, fill in the basic information, and record the APPID APPID
    • To generate a key , you need to configure the key on the applet server and Alipay server to verify the transaction data.
    • Configure the public key in [Settings]->[Development Settings]-[Interface Signing Method], and record the private key

Build a small program server

  • Ready to work

  • Deploy the application

    • Enter the console application center, select Mini App Todo List Server TemplateTemplate , fill in the APPID and PrivateKey just recorded, give a custom name for DBInstance/LogProject (if the name is already occupied, an error of Already Exists will be reported), deploy the application

Build a Mini Program Client

  • Ready to work

    • Download and install the Alipay Mini Program Developer Tool .
  • Build an application

    npm install git://github.com/aliyun/fc-nodejs-sdk.git#mini-app --save
    
    • TODO LIST client code in mini-app-todo-list-client
      • Modify the variables defined at the beginning of server.js, these values ​​will be automatically output after the server-side template is deployed successfully:
information illustrate
fcAccountId Alibaba Cloud AccountId
fcRegion The region where the function is deployed, e.g.cn-shanghai
fcServiceName Function Compute service name
sessionFunctionName The name of the session function, such as session
dbFunctionName the name of the db function, such as db

Since fc-nodejs-sdk uses ES6 syntax, when publishing the Mini Program, you need to create a new file mini.project.json in the root directory of the Mini Program project and fill in the following content:

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

At this point, the development work has been completed, you can debug and launch your applet.

Summarize

After the Mini Program is launched, with the increase in the number of visits or the sudden increase in visits during the event, the stability and elasticity of the back-end service is also a big test. Function Compute can run after uploading the code, which greatly improves the development efficiency of back-end services; the elastic scaling of the hybrid mode makes it easy to cope with load changes. Server-side templates and client-side templates can quickly build small program applications based on function computing. These features make function computing a good choice for supporting small programs.

" Alibaba's cloud-native technology circle focuses on microservices, serverless, containers, service mesh and other technical fields, focuses on cloud-native popular technology trends, and cloud-native large-scale implementation practices, and is a technology circle that understands cloud-native developers best."

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324134494&siteId=291194637