The introduction

What is Serverless

Serverless is a cloud-native development model that enables developers to focus on building and running applications without managing servers. Serverless Cloud Function (SCF) is the Serverless execution environment provided by Tencent Cloud, which can run the code without purchasing and managing the server.

What is a Mock Server

Today’s business systems rarely exist in isolation, and they more or less need to use or rely on other services, which makes our tuning and testing difficult. In response, we often set up a temporary server to simulate those services and provide simulated data for tuning and testing. This temporary server is the mock Server. So mock Servers typically have the following characteristics:

  • Fast setup, no need to write code

  • Can simulate any data

  • Low cost

  • Simple configuration

It is these characteristics that are consistent with the characteristics of Serverless, so we use Serverless to do this thing is the most appropriate. Let’s use Tencent Cloud’s cloud function as an example to explain how to quickly build a Mock Server.

How to quickly build a Mock Server using cloud functions

There are many Mock Server tools available, both open source and not.

Here’s an example of how to quickly deploy a Mock Server using Moco.

Moco is an open source framework linked to Github :github.com/dreamhead/m…

(1) Preparation

Start by going to Moco’s Github page to download the prepared JAR file.

Secondly, you need to prepare a JSON file to define response, as follows. The contents need to define what the mock data to return is based on their business.

[  {    "response" :      {        "text" : "Hello, Moco"      }  }]
Copy the code

Finally, to run in the cloud function, you need a startup file, the name of which must be scf_bootstrap, which reads as follows:

#! /bin/bash /var/lang/java8/bin/java -jar moco-runner-1.2.0-standalone. Jar HTTP -p 9000 -c foo.jsonCopy the code

The port number must be 9000. If the JSON configuration file name is not foo. JSON, change the file name to your own.

Then package the three files into a zip file, as shown below.

Deploy the Mock Server

Open the cloud function console and create a new cloud function. The diagram below.

  • Select Custom Creation

  • Select Web functions

  • The runtime environment is Java8

  • Upload the packed ZIP file in the function code

Finally, click Finish.

Then, you can see the access path in the function management interface. As shown below, sending an HTTP request to the URL gets the response you defined in the JSON file.

(3) One-click deployment

The above method is already very fast. But there are faster ones, yes! Mock Server has been installed in the official template of cloud functions. As you can see by searching for Mock in the template below, you can deploy a Mock Server with one click.

Note: The mock Server for template deployment comes with a default Response JSON file. You can download the deployed code package, replace the json file with your own, and redeploy it. For details, see readme in the template source code.

Advantages of Serverless Mock Server

Using Serverless to build a Mock Server has several advantages:

(I) Rapid construction

All development teams want to be able to quickly build a Mock Server in very little time. So with Serverless you don’t have to worry about and maintain the server, so you can quickly set up and run a mock Server.

(2) extremely low cost

Since Mock Server is only used for testing, if we buy a server to build it, there will be a lot of money cost and maintenance cost.

The characteristics of Serverless charging by volume and free operation and maintenance can save both money cost and maintenance cost.

Mock Server is usually called infrequently, whereas cloud functions are charged per call, with 100,000 free calls per month. So using cloud functions you can collect wool for free.

(iii) No operation and maintenance

We don’t need to configure ports, firewalls, etc., as we do with managing servers. Just upload the Mock Server and you’re done.

Three, endnotes

Serverless can do much of the same, as its high performance, automatic scaling, and pay-as-you go features make it a cost-effective choice for many solutions.

The function of cloud website: cloud.tencent.com/product/scf…

The event bus’s official website: cloud.tencent.com/product/eb?…