头图

How to build Mock Server with Serverless?

腾讯云加社区
中文

introduction

(1) 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.

(2) 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:

  • Quick build, no need to write code
  • Can simulate any data
  • low cost
  • 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.

1. 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, Github link: https://github.com/dreamhead/moco

(1) Preparations

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.

(2) Deploy Mock Server

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

  • Choose Custom Create
  • Select Web Functions
  • Select Java8 as the operating environment
  • Upload the zip file you just packaged in the function code

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.

(3) One-click deployment

Is the above method already very fast? But there are faster ones, yes! The mock server has been listed in the official template of Cloud Functions. 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.

Note: The mock server deployed by the template comes with a default response json file. You can download the deployed code package, replace the json file inside with your own, and then redeploy. For specific steps, please refer to the readme in the template source code.

Second, the advantages of using Serverless to build Mock Server

Building a Mock Server with Serverless has the following advantages:

(1) 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.

(2) 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.

(3) No need for operation and maintenance

We don't need to configure ports, firewalls, etc. like the management server. Just upload the mock server and you're done.

3. Conclusion

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.

Cloud Function official website: https://cloud.tencent.com/product/scf?from=15939
Event Bus official website: https://cloud.tencent.com/product/eb?from=15940

阅读 923

腾讯云技术社区
最专业的云解读社区
21.3k 声望
17.2k 粉丝
0 条评论
21.3k 声望
17.2k 粉丝
文章目录
宣传栏