Jingdong Serverless use cloud services to quickly build era of IoT applications 5G

Alt

October 31, 2019 at the China International Exhibition, the Ministry of Industry announced: 5G commercial was officially launched. 5G commercial come of age!

5G commercial, so that the data transmission speed, response speed, data connection, the data transfer amount, transmission reliability, have a significantly improved break this technology makes many fields of application scenarios is true landing embodiment, take into the lives of ordinary people, and these include things.

Although the concept is very simple things, the article is connected to the Internet and the exchange of information and communication, in order to achieve intelligent things, but for various reasons, commercial applications and no large-scale landing, which was due to the reason for limiting the network transmission. Today, 5G soon so that things are no longer limited applications at the network level, to better promote the development of things. For businesses, to quickly build a commercial service of things, it is important to seize the initiative.

Jingdong Serverless cloud services are adapted to the needs of the enterprise and now this. Serverless ecological range of products allows users to business-oriented, without concern for the underlying server deployment as well as carrying capacity, short implementation cycle, no pay-per-use prepaid price. These features are the best choice to adapt to the Internet era 5G fast, quickly build, in order to save costs while enterprises solve the technical problems.

The following data networking with a car as an example, we look at how to use the service queue to build a highly reliable non-availability service application. Client load may be expanded in the car networking applications within 24 hours / down three or four orders of magnitude, these natural properties suitable for dynamic scaling Serverless service, pay by volume.

Case scenarios : vehicles networked data collection sensors mounted on automobiles with data transmission to the cloud, the cloud peak load shifting using the queue service, the ability to receive data dynamically scalable, and then dump Elasticsearch better retrieval and application data, as to provide users with better service or provide more business value for the company.
Demand : Queue Service SDK, queue service access point address, Elasticsearch access point address (ES already created instance), Jingdong cloud user AK / SK
code language : Go

Step1

Create a queue client service and resource creation

 func CreateClient() *sqs.SQS {
     ses, _ := session.NewSession(&aws.Config{
         Region: aws.String("cn-north-1"),
         Credentials: credentials.NewStaticCredentials("your AccessKey",
             "your SecretKey", ""),
         Endpoint:   aws.String("http://jqs.cn-north-1.jdcloud.com"),
         DisableSSL: aws.Bool(true),
     })
     _, err := ses.Config.Credentials.Get()
    if err != nil {
        log.Fatal("凭据创建失败", err)
    }
    client := sqs.New(ses)
    return client
}

func CreateQueueTask(name string) string {
    resp, err := sqsClient.CreateQueue(&sqs.CreateQueueInput{
        QueueName: aws.String(name),
    })
    if err != nil {
        log.Println("Create Queue Failed:", err)
        return ""
    }
    return *resp.QueueUrl
}

Step2

Each vehicle device sends a message

 func SendTask(url string, message interface{}) {
    body, _ := json.Marshal(message)
     _, err := sqsClient.SendMessage(&sqs.SendMessageInput{
         MessageBody: aws.String(string(body)),
         QueueUrl:    aws.String(url),
     })
     if err != nil {
         log.Println("Send Message Failed:", err)
         return
    }
    return
}

Test data :

Alt

Test machine configuration : CPU64, memory 256G, bandwidth of 100Mbps (Jingdong and so the host)

Scene : public network; send-single (of JQS)

Step3

Pull message from the queue in the service dump Elasticsearch

func ReceiveMessageTask(url string) interface{} {
     result, err := sqsClient.ReceiveMessage(&sqs.ReceiveMessageInput{
         AttributeNames:        aws.StringSlice([]string{"All"}),
         MaxNumberOfMessages:   aws.Int64(1),
         MessageAttributeNames: aws.StringSlice([]string{"All"}),
         QueueUrl:              aws.String(url),
         VisibilityTimeout:     aws.Int64(30),
         WaitTimeSeconds:       aws.Int64(0),
     })
    log.Println(*result.Messages[0].Body)
    if err != nil {
        log.Println("Receive Message Failed:", err)
        return ""
    }

    if len(result.Messages) > 0 {
        _, delErr := sqsClient.DeleteMessage(&sqs.DeleteMessageInput{
            QueueUrl:      aws.String(url),
            ReceiptHandle: result.Messages[0].ReceiptHandle,
        })
        if err != nil {
            log.Println("Delete Message Failed:", delErr)
        }

        message := new(gpsMessage)
        Err := json.Unmarshal([]byte(*result.Messages[0].Body), message)
        if Err != nil {
            log.Println("Receive Message Unmarshal Failed", Err)
            return ""
        }
        return message
    }
    return ""
}

func Build(url string, method string, body interface{}) []byte {
    client := &http.Client{}
    //向服务端发送get请求
    bodyData, _ := json.Marshal(body)
    request, _ := http.NewRequest(method, url, bytes.NewReader(bodyData))

    request.Header.Set("Accept", "application/json")
    request.Header.Set("Content-Type", "application/json")
    //接收服务端返回给客户端的信息
    response, _ := client.Do(request)
    r, _ := ioutil.ReadAll(response.Body)
    return r
}

func PostMessageToES(p string, body interface{}) string {
    postReturn := new(postRes)
    postResponse := Build(p, "POST", body)
    err := json.Unmarshal(postResponse, postReturn)
    if err != nil {
        log.Println("Unmarshal Failed", err)
    }
    jsonS, _ := json.Marshal(postReturn)
    log.Println("postResult:", string(jsonS))
    return postReturn.Id
}

Step4

Demand information from Elasticsearch search by index

func GetMessageFromES(p string) {
     message := new(esMessage)
     getResponse := Build(p, "GET", nil)
     log.Println("getPath:", p)
     Err := json.Unmarshal(getResponse, message)
     if Err != nil {
         log.Println("Unmarshal Failed", Err)
     }
     jsonM, _ := json.Marshal(message)
    log.Println("getResult:", string(jsonM))
}

Example result :

Alt

It can be carried out in accordance with the vehicle information to retrieve data, to map out the car dynamic map, use the data to help autopilot, dynamic navigation, vehicle health analysis and other scenarios to achieve.

Jingdong queue cloud service as a Serverless development of BaaS service, operation and maintenance and to achieve a non-millisecond scaling capabilities of middleware services to support the cloud partner Jingdong zero cost to start the business and pay-per-usage model to help users solve complex issues and resource scaling threshold monitoring. FaaS use in conjunction with the service function, to meet the richer scene, and calls the entire cloud Serverless Jingdong ecology, creating new applications based on open cloud native 21st century. Click on " Learn ", come to experience it!

Welcome Click " Jingdong cloud " for more exciting content

Alt
Alt

Guess you like

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