foreword

What is Serverless

Serverless is a cloud-native development model that allows developers to focus on building and running applications without managing servers.

Serverless Cloud Function (SCF) is a serverless execution environment provided by Tencent Cloud, which can run code without purchasing and managing servers.

What is Mock Server

Today's business systems rarely exist in isolation, and they more or less need to use or depend on other services, which causes trouble for our joint coordination and testing.

In order to deal with this situation, we often build a temporary server, simulate those services, and provide simulated data for joint debugging and testing.

This temporary server is the mock server.

Therefore, a mock server usually has the following characteristics:

  1. Quick build, no need to write code
  2. Can simulate any data
  3. low cost
  4. Simple configuration

It is these characteristics that are in line with the characteristics of serverless, so it is more appropriate for us to use serverless to do this.

Next, we will use Tencent Cloud's cloud functions as an example to explain how to quickly build a Mock Server.

How to quickly build a Mock Server with cloud functions

At present, there are many Mock server tools on the market, both open source and not open source.

Here we use Moco as an example to teach you how to quickly deploy a Mock Server.

Moco is an open source framework, here is its Github link .

Ready to work

First go to Moco's github page to download the prepared jar file.

Secondly, you need to prepare a JSON file that defines the response, as follows. The content in it needs to define what the mock data to be returned is according to its own business.

[
  {
    "response" :
      {
        "text" : "Hello, Moco"
      }
  }
]

Finally, a startup file is required to run in the cloud function, the file name must be scf_bootstrap , and the content is as follows:

#!/bin/bash
/var/lang/java8/bin/java -jar moco-runner-1.2.0-standalone.jar http -p 9000 -c foo.json

The port number must be 9000 . If the JSON configuration file name is not foo.json, it needs to be changed to its own file name.

Then package the three files into a zip file, as shown below.
截屏2021-12-29 16.58.53.png

Deploy Mock Server

Open the cloud function console and create a new cloud function. As shown below.

  • select from scratch
  • Select Web Function
  • Operating environment selection Java8
  • Upload the zip file you just packaged in the function code
    Untitled.png

Finally, click Finish.

Then, you can see the access path in the function management interface. As shown below.

Send an HTTP request to the URL to get the response you defined in the JSON file.
1.png

One-click deployment

Is the above method already very fast? But there are faster ones, that's right!

Mock server has been listed in the official template of cloud function.

As shown in the figure below, you can see that by searching for mock in the template, you can deploy a mock server with one click.
34.png

The advantages of using Serverless to build Mock Server

Building a Mock Server with Serverless has the following advantages.

Quick build

All development teams hope to quickly build a Mock Server with minimal time.

Therefore, using Serverless does not need to pay attention to and maintain the server, so you can quickly build and run a mock server.

very low cost

Since the Mock server is only used for testing, if we buy a server to build it, it will increase a lot of money and maintenance costs.

The features of serverless charging by volume and free operation and maintenance can not only save money cost, but also save maintenance cost.

Usually we call Mock Server very few times, and cloud functions are charged by the number of calls, and there are 100,000 free calls per month. So using cloud functions can be free.

No operation and maintenance required

We don't need to configure ports, firewalls, etc. like the management server.

Just upload the mock server and you're done.

At last

Serverless can also do a lot of similar things, because of its high performance, automatic scaling, pay-as-you-go, and other features, making it the first choice for cost-effectiveness in many solutions.

In the future, I will continue to explore more practical scenarios of serverless to share with you.


Woody
843 声望62 粉丝

ThoughtWorks|高级咨询师