Serverless computing will become the default computing paradigm in the cloud era, replacing Serverful (traditional cloud) computing, thus marking the end of the server-client model. —— Simplifying Programming in the Cloud: Serverless Computing from the Berkeley Perspective

preface

Serverless computing enables developers to build modern applications with greater flexibility and lower cost. Without having to configure and manage infrastructure like servers, developers can focus on their core business.

Serverless has essential differences compared to the traditional Serverful development mode:

  1. Decoupling of computing and storage; They scale independently, they price independently;
  2. Code execution no longer requires manual allocation of resources;
  3. Charge by usage.

Serverless Quick build Build APP questionnaire survey

As the core computing component in the Serverless architecture, functional services can be applied to all kinds of Serverless architectures, including two categories: Web, mobile, IoT, AI and other Serverless backends and data processing system backends.

Among them, typical scenarios of Web and mobile Serverless back-end are most widely used. This paper introduces how to use function service +API gateway to construct user questionnaire in JINGdong Cloud APP in minutes.

APP questionnaire construction can be completed by two simple functions: obtaining the content of the questionnaire and submitting answers.

Step1: create a storage device

Create a database or object storage bucket for storing the question bank and user answers. In this example, create a cloud cache Redis instance (primary/secondary 4GB).

Step2: create the function

Create and test two functions in the function service (runtime: Python2.7)

Create function 1

Create function jcloud-app-survey-topic: request user PIN, questionnaire version version, get the questionnaire content from the database, package the entry file index.py with the dependent library Redis SDK and upload it to function service. Index.py looks like this:

#coding=utf-8

import json
import redis' ''Download questionnaire'' '
def handler(event,context):

    if  not bool(event):
        result = {
            'statusCode': 200,
            'headers': {},
            'body': "",}return result


    body = event['detail'] ['body']
    body = json.loads(body)
    pin = body.get('pin'."")
    version = body.get('version'."")
    print(pin)
    print(version)

    r = redis.Redis(host='redis-v214pzrgiicq-proxy-nlb.jvessel-open-hb.jdcloud.com', port=6379, db=0)

    topic = r.hget('topic_' + version, version)
    data = {'pin': pin, 'version': version, 'topic': topic}
    data = json.dumps(data)

    result = {
        'statusCode': 200,
        'headers': {},
        'body': data,
    }

return result
Copy the code

Function test

Create function 2

Create function 2jcloud-app-survey-submit: request user PIN, questionnaire version, user answer submit, upload to database, package entry file index.py and dependent library REDis SDK and upload to function service. The code for index.py is as follows.

#coding=utf-8

import json
import redis' ''Upload questionnaire results'' '
def handler(event,context):

    if  not bool(event):
        result = {
            'statusCode': 200,
            'headers': {},
            'body': "",}return result

    body = event['detail'] ['body']
    body = json.loads(body)
    pin = body.get('pin'."")
    version = body.get('version'."")
    submit = body.get('submit'."")
    print(pin)
    print(version)
    print(submit)

    r = redis.Redis(host='*********.jdcloud.com', port=6379, db=0)

    old = r.hget('submit_' + version, pin)
    ifold ! = None : data = {'code': 1, 'desc': 'user have submitted'}
      data = json.dumps(data)
      result = {
        'statusCode': 200,
        'headers': {},
        'body': data,
      }
      return result

    r.hset('submit_' + version, pin, submit)

    data = {'code': 0.'desc': 'success'}
    data = json.dumps(data)
    result = {
        'statusCode': 200,
        'headers': {},
        'body': data,
    }

    return result
Copy the code

Function test

Step 3: Create a publishing API

Create two apis at the API gateway, bind them to corresponding functions as triggers, respond to API requests through function, and publish the APIS to the test environment. Jcloud-app-survey-topic functions bind API triggers as follows:

The jcloud-app-survey-submit function binds the API trigger as follows:

Step 4: Verify online

Verify the CORRECTNESS of THE API interface through the local API interface test tool, and can be coordinated with the front-end H5 page development. After the test is completed, it can be released iteratively through the function version and alias function management line.

Questionnaire Submission page

Above, quickly complete the APP back end online, just wait for the activity online

The final result

Finally, the APP questionnaire results are as follows:

Jingdong cloud

Click “link” to learn more about jingdong Cloud highlights