Using JD cloud Serverless service to quickly build IoT application in 5G Era

Keywords: AWS JSON network SDK

On October 31, at the 2019 China International Information and Communication Exhibition, the Ministry of industry and information technology announced that 5G business was officially launched. 5G business age is coming!

The commercial application of 5G makes the data transmission speed, response speed, connection data, data transmission volume, transmission reliability and other aspects have been significantly improved. The breakthrough of this technology enables the application scenarios in many fields to be truly implemented and enter the life of ordinary people, including the Internet of things.

Although the concept of the Internet of things is very simple, which is to connect the goods with the Internet and carry out information exchange and communication, so as to realize the intelligence of everything, but for various reasons, the commercial application has not been implemented on a large scale, one of which is due to the limitations of network transmission and other reasons. Nowadays, the arrival of 5G makes the application of Internet of things no longer limited at the network level, which better promotes the development of Internet of things. For enterprises, it is very important to build a commercial Internet of things service quickly and seize the opportunity.

JD cloud Serverless service is adapting to the needs of today's enterprises. A series of product ecology of Serverless enables users to be business oriented, without caring about the deployment and bearing capacity of the underlying server, with short implementation cycle and no prepaid price based on usage. These features are the only choice to adapt to the rapid era of Internet 5G and build quickly, which not only saves costs for enterprises, but also solves technical problems.

Taking the following data of the Internet of vehicles as an example, let's take a look at how to use queue service to build a highly available and highly reliable application without service. In the application of Internet of vehicles, the client load may expand / reduce by 3 or 4 orders of magnitude within 24 hours. These features are naturally suitable for the dynamic expansion and contraction of Serverless services and pay as you go.

Case scenario: in the Internet of vehicles, vehicles equipped with data collection sensors transmit driving data to the cloud. The cloud uses the ability of queue service to cut peak and fill valley, and dynamically expand and shrink to receive data, and then transfers it to elastic search for better data retrieval and application, so as to provide better services for users or provide more business value for companies.
Requirements: queue service SDK, queue service access point address, Elasticsearch access point address (ES instance has been created), JD cloud user AK/SK
Code language: Go


Create queue service client and resource creation

 1func CreateClient() *sqs.SQS {
 2    ses, _ := session.NewSession(&aws.Config{
 3        Region: aws.String("cn-north-1"),
 4        Credentials: credentials.NewStaticCredentials("your AccessKey",
 5            "your SecretKey", ""),
 6        Endpoint:   aws.String(""),
 7        DisableSSL: aws.Bool(true),
 8    })
 9    _, err := ses.Config.Credentials.Get()
10    if err != nil {
11        log.Fatal("Credential creation failed", err)
12    }
13    client := sqs.New(ses)
14    return client
17func CreateQueueTask(name string) string {
18    resp, err := sqsClient.CreateQueue(&sqs.CreateQueueInput{
19        QueueName: aws.String(name),
20    })
21    if err != nil {
22        log.Println("Create Queue Failed:", err)
23        return ""
24    }
25    return *resp.QueueUrl


Each vehicle device sends a message

 1func SendTask(url string, message interface{}) {
 2    body, _ := json.Marshal(message)
 3    _, err := sqsClient.SendMessage(&sqs.SendMessageInput{
 4        MessageBody: aws.String(string(body)),
 5        QueueUrl:    aws.String(url),
 6    })
 7    if err != nil {
 8        log.Println("Send Message Failed:", err)
 9        return
10    }
11    return

Test data:

Configuration of test machine: CPU64, memory 256G, bandwidth 100Mbps (JD cloud virtual machine)

Scenario: public network; send single (JQS)


Pull messages from the queue service and transfer them to elastic search

 1func ReceiveMessageTask(url string) interface{} {
 2    result, err := sqsClient.ReceiveMessage(&sqs.ReceiveMessageInput{
 3        AttributeNames:        aws.StringSlice([]string{"All"}),
 4        MaxNumberOfMessages:   aws.Int64(1),
 5        MessageAttributeNames: aws.StringSlice([]string{"All"}),
 6        QueueUrl:              aws.String(url),
 7        VisibilityTimeout:     aws.Int64(30),
 8        WaitTimeSeconds:       aws.Int64(0),
 9    })
10    log.Println(*result.Messages[0].Body)
11    if err != nil {
12        log.Println("Receive Message Failed:", err)
13        return ""
14    }
16    if len(result.Messages) > 0 {
17        _, delErr := sqsClient.DeleteMessage(&sqs.DeleteMessageInput{
18            QueueUrl:      aws.String(url),
19            ReceiptHandle: result.Messages[0].ReceiptHandle,
20        })
21        if err != nil {
22            log.Println("Delete Message Failed:", delErr)
23        }
25        message := new(gpsMessage)
26        Err := json.Unmarshal([]byte(*result.Messages[0].Body), message)
27        if Err != nil {
28            log.Println("Receive Message Unmarshal Failed", Err)
29            return ""
30        }
31        return message
32    }
33    return ""
36func Build(url string, method string, body interface{}) []byte {
37    client := &http.Client{}
38    //Send get request to server
39    bodyData, _ := json.Marshal(body)
40    request, _ := http.NewRequest(method, url, bytes.NewReader(bodyData))
42    request.Header.Set("Accept", "application/json")
43    request.Header.Set("Content-Type", "application/json")
44    //Receive the information returned by the server to the client
45    response, _ := client.Do(request)
46    r, _ := ioutil.ReadAll(response.Body)
47    return r
50func PostMessageToES(p string, body interface{}) string {
51    postReturn := new(postRes)
52    postResponse := Build(p, "POST", body)
53    err := json.Unmarshal(postResponse, postReturn)
54    if err != nil {
55        log.Println("Unmarshal Failed", err)
56    }
57    jsonS, _ := json.Marshal(postReturn)
58    log.Println("postResult:", string(jsonS))
59    return postReturn.Id


Search information by demand index from elastic search

 1func GetMessageFromES(p string) {
 2    message := new(esMessage)
 3    getResponse := Build(p, "GET", nil)
 4    log.Println("getPath:", p)
 5    Err := json.Unmarshal(getResponse, message)
 6    if Err != nil {
 7        log.Println("Unmarshal Failed", Err)
 8    }
 9    jsonM, _ := json.Marshal(message)
10    log.Println("getResult:", string(jsonM))

Example results:

It can retrieve the data according to the vehicle information, draw the dynamic map of the vehicle, and use the data to help the automatic driving, dynamic navigation, vehicle health analysis and other scenarios.

As a BaaS service in the development of Serverless, the queue service of JD cloud realizes the non operation and millisecond expansion capability of middleware service, supports JD cloud's partners to start business at zero cost and pay as you use mode, and helps users solve complex problems such as resource expansion and threshold monitoring. Combined with the use of function service FaaS, it can meet more rich scenarios, and call the entire JD cloud Serverless ecosystem to create an open new application based on cloud native in the 21st century. Click " understand ”Come and experience!

Welcome to“ Jingdong cloud ”Learn more

Posted by ramez_sever on Tue, 14 Jan 2020 19:11:58 -0800