Dry | quickly build Serverless survey questionnaire used in the APP

Alt

Serverless era of cloud computing will become the default computing paradigm, and replace Serverful (traditional cloud) computing model and, therefore, means that the server -
end client mode.
------ "simplify programming cloud: Serverless in Berkeley Perspective computing"

Foreword

Serverless computing allows developers to build modern applications with greater flexibility and lower costs. Developers do not need to configure and manage the servers and other infrastructure, all energy can be put into the core business.

Serverless essentially different development methods compared to traditional serverful:

  1. Computing and storage decoupling; independently extension thereof, independent pricing;
  2. Execution of the code is no longer necessary to manually assign resources;
  3. Per-use billing.

Serverless quickly build build APP survey

Service as a function Serverless architecture core computing components, can be applied to various types Serverless architecture, mainly include two categories: non-server back-end and back-end data processing Web, mobile, IoT, AI systems.

Which, Web, mobile serverless typical scene of the most extensive back-end, this article describes how to build every minute + API Gateway user questionnaire used in Jingdong APP cloud service functions.

Construction of APP questionnaires completed by two simple functions: to obtain and submit the questionnaire answers .

Step1: Creating storage

Create a user and for storing the questionnaire answers exam or object database storage bucket, this example creates a cache Redis instance (master from 4GB) cloud.

Step2: Creating Functions

Create and test two functions in the function Service (Runtime: Python2.7)

Create a function 1

Create a function jcloud-app-survey-topic: requesting user PIN, questionnaire version version, get the questionnaire from the database, the entry file index.py and dependent libraries redis SDK package upload to the service function. index.py code is as follows:

#coding=utf-8

import json
import redis'''下载问卷'''
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

Function test

Alt

Create a function 2

Create a function 2jcloud-app-survey-submit: request a user PIN, questionnaire version version, users answer submit, upload to the database, and import documents index.py dependent libraries redis SDK package upload to the service function. index.py code is as follows.

#coding=utf-8

import json
import redis'''
上传问卷结果'''
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)
    if old != 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

Function test

Alt

Step 3: Creating Publishing API

Create two Gateway API in the API, binding to the corresponding function as a trigger, through the API function in response to a request, and to release API test environment. jcloud-app-survey-topic triggers binding API function as follows:

Alt

jcloud-app-survey-submit triggers binding API function as follows:

Alt

Step 4: verification on-line

Validation API by local API interface testing tool interface is correct, you can develop with the front page H5 FBI, the test is completed, by function version, alias function to manage online publishing function iteration.

Page questionnaire submitted

Alt

Above, APP quickly back-end on-line, on-line and other activities only

Alt

final effect

Finally, APP questionnaire results are as follows:

Alt
Click " Jingdong cloud " to learn more about Queue Service

Alt

Alt

Guess you like

Origin www.cnblogs.com/jdclouddeveloper/p/11764832.html