Huang Wenjun: Serverless applet back-end technology sharing

Welcome to Tencent Cloud + Community to get more Tencent's massive technical practice dry goods~

Huang Wenjun, currently the senior product manager of Tencent Cloud SCF serverless cloud function, has many years of experience in enterprise-level system development and architecture, and has been involved in enterprise-level storage, container platform, microservice architecture, serverless computing and other fields.

Today I'm going to talk about how to use Serverless for back-end technology sharing. My career preference is the back-end, maybe not writing the front-end, not using Node.js, but more using CR as the back-end language. Today, the WeChat applet I focus on, I share more about how to implement its back-end . What I use is Serverless technology, which is also a new architecture in the past two years. Before talking about the serverless architecture, I will also tell you about small programs and traditional background technologies.

Background technology of applet

Mini Programs are a brand-new way to connect users and services. They can be easily acquired and spread in WeChat, and have an excellent user experience. Its loading method is faster than the traditional APP method, and the experience is not bad. In addition to its own interface display and refresh, the data acquisition in the applet interacts with the backend through WeChat. The operation of the applet is actually The operation mode of a front-end class, the whole operation is in WeChat, and the interaction between it and the back-end is actually forwarded through WeChat. After running, it will make an api request, this request is first sent to WeChat, WeChat and then through the network apr goes to your own server, the server processes the data after getting the request, and then responds to the front end. This is an architecture for the interaction between the applet and the background.

For back-end services, this picture is the traditional way of doing, exposing api, which can be used to develop business applications, after business applications need to have corresponding storage files, structured data storage, or unstructured data storage , need to have database and cache, in order to achieve this set of architecture and will not be down due to a certain server, or there are some loopholes, etc., our usual implementation is a relatively complex process. For example, in order to ensure that the serverless and the server will not collapse, we need to establish a cluster, we need to provide services to the outside world, we need LB requests, and then the requests are allocated to a certain server. For example, for file storage, if only one device is used, if this device fails, the entire file service will fail, so we need to use distributed storage to solve the problem of file storage. The same is true for databases and caches. It is necessary to build a cluster, whether it is two, three or more. After building a cluster, it can ensure that the entire service will not be unavailable due to a single point of problems, resulting in service paralysis.

 

If you are a small program developer, this set of architecture has already been built in an Internet company. As an individual, this is too heavy. You need to understand a certain piece and its configuration. For example, how to configure a database cluster? So that everyone can't focus on your business and the applet itself, but spend too much on operation and maintenance and support.

Serverless Architecture

The serverless architecture that I will introduce below adopts a serverless approach. I will mainly introduce how to combine serverless and cloud, and how to use cloud services to ease the work of architecture. Before introducing its architecture, let me introduce the serverless architecture, which is called serverless in English and serverless in Chinese. You don't need to buy servers, virtual machines or physical machines. How does this work? It uses the computing hosting method. In Serverless, we can see it as two parts. The first one is Function as a Service, which truly realizes the managed computing of your business. The other is backend-as-a-service, including object storage. You don’t have to build distributed storage yourself, and you don’t have to worry about data loss and security issues. At the same time, the database, message queue and object storage provided on the cloud are the same, and you don’t need to worry about data loss and security issues. Buying a server and building it yourself, we can call it Serverless in the process of purchasing and using it. Because these are hosted, you don't need to care about its security when using it, and you don't need to care about the failures that may be caused by server downtime.

The serverless computing managed cloud is in the service function. Let's talk about the architecture of the cloud function. After everyone sees this architecture, we will take a practical case later to see how to implement specific api services. The cloud server architecture itself is a computing hosting type. Computing hosting means hosting the real business code on the cloud, and then running it on the cloud. One of the features of its operation mode is triggered operation. After getting through with each product, each product The generated event, the latter case is combined with the API gateway, the event from the api gateway is the api event, when the request reaches the api gateway, we consider it as an event, and then run it. When you first start hosting, you submit the code and the configuration of the trigger period to the cloud. It does not mean that the code will run after the submission, but only when the event arrives, and the code will process the event. In this process, for each event, the process of pulling up each code is actually a separate event, why? Because we use the concurrent mode here, if you have tens of thousands of users accessing your applet at the same time, you need to serve tens of thousands of users and start tens of thousands of instances at the same time. Running, this is in line with the concept of WeChat applet itself, which is click-to-use and run-and-go. It only runs when there is a request, and it does not run when there is no request. The billing model of the product is also billed according to the actual running time.

Use of Serverless

How to use Serverless? The traditional architecture is the web service mentioned above, and then the use of storage and cache. After we serve externally, there are corresponding APIs that are exposed to the outside world. In fact, the business logic of the user is placed in the cloud function, which requires structured storage and needs to be implemented. For caching or object storage, we need data services or cloud caching services, etc. Other services can be directly served online, and these services are called directly through code.

The architecture of Serverless was introduced earlier, followed by an introduction to the background development, followed by a detailed case description based on this method.

In addition to the start of its own page, the subsequent interaction with the network is initiated by the applet. After passing through WeChat itself, the first request reaches the api gateway. For the management of external api, this api is exposed to the official website, which can be requested by the program get access. It can also provide API release and version switching capabilities. After the API gateway is the cloud function. The cloud function is the logic that actually processes the business. If you need to use the database, send the connection to the database in the code. If you need to store the file, call the corresponding window to write the file.

Based on this solution, let's take a look. The traditional one is the middle piece, because the front end is the user's applet, the back end is the interface service provided by WeChat itself, and the developer's own server is recommended in the middle. A case we are going to show now is how to replace the middle developer server and implement the serverless solution. We use the api gateway and cloud database to achieve what the developer server can do without purchasing a server. Landing our api.

First of all, from the front of the applet, this case is also the demo on the applet developer. The front end of the demo includes the login port and the session display. We first get a talk on the applet end, and the developer will follow up after getting it. Wechat interaction, after verification is legal, here we choose to record to cloud database, this is the location where the applet interface provides click login, and the subsequent business will send requests to the cloud.

For the core of this applet, our path on a certain api is to initiate a GET operation on the login url of hos, construct the request header and body content according to Wx.login, and send the obtained code and encrypted data to the background.

api gateway

Let's take a look at what the api gateway does. It is presented as an api interface to the outside world. We directly provide the domain name for external access. Users bind their own domain names based on this domain name. In this case, the publishing requirements can be achieved. The developers of the WeChat applet require the domain name to be filed, bind their domain name to the api service, and provide it to the outside world. On the gateway of the api on the right, configure a /login Get, which can be configured before the background is implemented. For the mock method, the front and back ends are decoupled, and the applet can be developed based on the mock data constructed by the api. After implementing the cloud function, connect the api to the cloud function and update and publish the api to avoid affecting the online business during development.

The processing flow of cloud functions

From the perspective of cloud functions, users will carry computing services. According to the official picture given by the user at the beginning, after getting the api request, parse the request content, connect to the WeChat authentication server according to the specification, obtain the authentication status and record the session, and return the session information to the requester. After getting the return from the WeChat server, you can judge whether the user's login process is successful or unsuccessful. If it is successful, you can get the corresponding value of the user. Here we initiate a connection to the database, establish a masico connection, and complete the session record.

After the processing flow of the cloud function, the following shows how we establish a connection with the database, the detailed process of logging in information, create a connection and reuse the connection, assemble the SQL statement and execute it.

database configuration

This place is that after we purchase, after the database is started and logged in, we can enter the database to view the data and view the session records.

Practical case - user login and session display

Create and initialize an instance, operate according to the standardized usage of mysql, and calculate the advantages of managed type. Users care about the core code, not the surrounding operation and maintenance. Due to the managed service, whether it is a personal request or a small program of an individual developer, it is very possible A small program of yours will become a hit, and after the hit, the number of visits may be in the form of a sudden increase.

We use the session architecture to implement small programs without worrying about operation and maintenance; start in seconds, and the elastic computing power can meet the concurrency of tens of thousands of users. The core point is to focus on the business code, not the web. This is the way to quickly implement the application of small programs.

Q/A

Q: Is the HTTPS certificate deployed on the api gateway?

A: Yes, this certificate is provided by Tencent Cloud. When you bind your own domain name, you can implement HTTPS support.

Q: The certificate provided by Tencent Cloud when we are in the production environment?

A: Yes.

Q: Do you need the certificate provided by Tencent Cloud to launch our service in the production process?

A: Yes, this is a requirement for Mini Programs, because Mini Programs must use their own domain name to connect with Mini Programs.

Q: The applet can be deleted immediately after use. If there are too many in the mobile phone, it will be very stuck. If the applet has been used on the WeChat page, it will be displayed. If there are tens of thousands of applet, what effect will it have on WeChat itself?

A: It can be seen from some restrictions that there is a limit on the size of the WeChat applet, and its own size is a requirement. At present, I remember it is the size of 5 megabytes and 2 megabytes. If it is thousands, for your mobile phone For example, it may store some more data. The maximum usage of each package may be 5 MB. The loading of the applet page itself is limited. From the perspective of WeChat, this limitation is to ensure the smooth operation of the applet. , it will not have a big impact on the user's mobile phone. This one needs to be introduced by a classmate of WeChat. I am partial to the back end.

Q: Let me give another example. When the applet is entered, the loading speed is relatively fast. To give an extreme one, such as jumping, when it is used for the first time, its loading speed, for example, if it is used up today, delete it. If you drop it and then go in after a while, is there any difference in the loading speed? Or is it faster the second time I use it? what is the reason?

A: It’s better to have WeChat students answer this question, because these are all manifestations of the mini program itself, or a manifestation of speed.

Q: Hello, I am doing back-end development, what kind of model is not made with Serverless.

A: Some features of Serverless itself also limit its usage scenarios, such as memory configuration, cpu configuration, and running time restrictions, which are not applicable to all occasions. The use of api, including the use of memory, including the application of cpu, such as animation rendering, long-term batch calculation, these are not suitable for api services, because its request must respond quickly to users after arrival, api is more suitable.

 

For more sharing information, please click the link below:

Use serverless to build applet background.pdf

 

Q&A

How does the WeChat applet interact with the database?

Related Reading

Zhu Zhan: Tencent Cloud Mini Program Solution

Shi Delai: Practice of Youzan E-commerce Mini Program

Zou Wei: How to develop a small game

 

This article has been authorized by the author to publish Tencent Cloud + Community, the original link: https://cloud.tencent.com/developer/article/1116854?fromSource=waitui

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326065455&siteId=291194637