In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you what Serverless architecture programming learning workers have, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
I have done an online programming software before, the current number of users is about hundreds of thousands, through this App can not only write code, run but also learn programming. I have always had a fondness for Serverless architecture, so I happened to arrive at my App learning section, which is complained by many people that it is difficult to use. I simply restructure this learning section, and intend to put this learning section directly on the Serverless architecture when refactoring.
Refactoring based on Serverless architecture is based on two considerations-one is that Serverless architecture makes the operation and maintenance work of individual developers easier, especially without worrying about servers and traffic peaks (of course, there are not too many flood peaks for my personal project), and the other is the pay-by-usage of Serverless architecture, resulting in great cost savings.
Overall design database design
This part used to be a number of large modules, and now it is consolidated into one module for project refactoring, so here we continue to reuse the previous database:
In this database, the four modules are: news articles, development documents, basic tutorials and book resources. The development document includes large categories, sublists and text, where table associations do not use foreign keys, but directly use ID to associate tables.
To tell you the truth, the design of this database is not very good, because most of the data was collected from other sites when this data part was built for the first time. At that time, because the module was launched quickly, it was stored directly in the original format. Therefore, it can be considered that the fields of many tables in this database are actually invalid, or are not used for this project.
Back-end design
The backend will be deployed to a function as a whole, and the overall structure of the function:
The overall function is that the cloud function SCF is bound to the API gateway trigger. The user accesses the address specified by the API gateway and triggers the cloud function. Then the function splits the function at the entrance and requests different methods to obtain the corresponding data.
I would like to make it clear here that the reason why the overall backend interface is deployed in a function is that the usage of my module is not very frequent, so deploying to a function will not exceed the maximum instance limit. If the limit is exceeded, you can apply for capacity expansion.
Secondly, all the interfaces add, delete, change and query the database, and put it into a function, which can ensure the activity of the container to a certain extent, reduce the problems caused by some cold start, and at the same time reuse the container. It can also reduce the pressure of the background database link pool to a certain extent. In addition, all interface functions only need the least memory (64m) to run completely, and will not affect the overall cost because of the estimated large memory of individual interfaces.
Therefore, after the evaluation here, multiple interfaces can be put into one function to provide the corresponding services.
Front-end design
In the front-end design, it is estimated that there will be 8 pages in the learning resources section, which are mainly science and technology news, tutorials, documents, books and other related functions. The prototype image drawn by the ink knife is as follows:
The front-end project development will adopt Vue.js and deploy it to COS to provide services through the static website feature of Tencent Cloud COS.
Project development back-end function development
Back-end function development mainly consists of three parts.
Partial resource initialization and partial resource initialization need to be carried out outside the function, which ensures that the link will not be established again when the instance is reused, thus preventing database connection pool problems:
Def getConnection (dbName): conn = pymysql.connect (host= "", user= "root", password= "", port=3306, db=dbName, charset='utf8', cursorclass=pymysql.cursors.DictCursor ) conn.autocommit (1) return connconnectionArticle = getConnection ("anycodes_article")
Database query operation
This part is mainly about querying databases for different interfaces, such as getting article categories:
Def getArticleCategory (): connectionArticle.ping (reconnect=True) cursor = connectionArticle.cursor () search_stmt = ('SELECT * FROM `roomy`ORDER BY `sort`) cursor.execute (search_stmt () data = cursor.fetchall () cursor.close () result = {} for eve_data in data: if eve_data ['pre_name'] not in result: result [eve _ data [' pre_name']] = [] result [eve _ data ['pre_name']] .append ({"id": eve_data ["sort"] "name": eve_data ["name"]}) return result
For example, get a list of articles:
Def getArticleList (cid): connectionArticle.ping (reconnect=True) cursor = connectionArticle.cursor () search_stmt = ('SELECT * FROM `returle`sort` =% s ORDER BY `sort`) cursor.execute (search_stmt, (cid,)) data = cursor.fetchall () cursor.close () result = [{"id": eve_data ["aid"] "title": eve_data ["title"]} for eve_data in data] return result
The last part is the entry of the function, which is to distribute the function and identify the interface:
Def main_handler (event Context): try: result_data = {"error": False} req_type = event ["pathParameters"] ["type"] if req_type = = "get_book_list": result_data ["data"] = getBookList () elif req_type = = "get_book_info": result_data ["data"] = getBookContent ( Event ["queryString"] ["id"]) elif req_type = = "get_daily_content": result_data ["data"] = getDailyContent (event ["queryString"] ["id"]) elif req_type = = "get_daily_list": result_data ["data"] = getDailyList (event ["queryString"] ["category"]) elif req_type = = "get_dictionary_result" : result_data ["data"] = getDictionaryResult (event ["queryString"] ["word"]) elif req_type = = "get_dev_content": result_data ["data"] = getDevContent (event ["queryString"] ["id"]) elif req_type = = "get_dev_section": result_data ["data"] = getDevSection (event ["queryString"] ["id") ]) elif req_type = "get_dev_chapter": result_data ["data"] = getDevChapter (event ["queryString"] ["id"]) elif req_type = = "get_dev_list": result_data ["data"] = getDevList () elif req_type = = "get_article_content": result_data ["data"] = getArticle (event [" QueryString "] [" id "]) elif req_type = =" get_article_list ": result_data [" data "] = getArticleList (event [" queryString "] [" id "]) elif req_type = =" get_article_category ": result_data [" data "] = getArticleCategory () return result_data except Exception as e: print (e) return {" error ": True}
After the function part is complete, you can configure the API gateway part:
In fact, there is no big problem in the whole back-end interface development process, because the module of this learning function is basically the operation of querying the database, so it is relatively smooth.
Effect preview
Overall preview result: a total of more than a dozen pages are included. Here, 8 main pages are selected to display the results:
The whole page basically restores the appearance of the design manuscript, and partially integrates with the original project, whether it is the list page or the book page, the data loading speed is good.
Basic testing through PostMan:
Perform 1000 access tests on the interface:
As you can see, the interface performs well without failure, and the test result is time-consuming to visualize:
The maximum time consumption is 219 milliseconds, the minimum is 27 milliseconds, the average is 35 milliseconds, you can see that the overall effect is still very good.
After the development of such a project is completed, the front-end part is put into the object storage COS, the back-end business is put into the cloud function SCF, and the trigger uses the API gateway. At the monitoring level, function calculation has a good monitoring latitude:
At the same time, problems such as function concurrency and auto scaling are solved by cloud vendors. It can be said that since this component has been deployed to the Serverless architecture, what I have done is to simply fix and maintain if there is something wrong with the business code. To tell you the truth, the whole effect is good.
By paying by quantity, you can see the fees generated by my back-end services:
Since the cloud function cannot see the cost of a single resource, I have dozens of functions, and the total cost is much cheaper than that of the server in a month.
Of course, although the overall cost of computing service here is only a few yuan, it is relatively cheap, but it also has the cost of API gateway and the cost of object storage, such as API gateway fee:
Similarly, my API gateway here also has a lot of services, not only generated by such a service as Anycodes, but only 1 yuan in February as a whole, which is relatively low.
Summary
Deploy the project to the Serverless architecture through a sub-module refactoring process in the personal project:
In the process of development, I think it is quite convenient. On the one hand, I do not need to install all kinds of software in the server, do not need to build web services, do not need to optimize web services, but only read the database and return according to a certain format. As for web services and other related modules to be implemented by the API gateway, the whole back-end development takes about more than an hour. Front-end development is more time-consuming, because I am not a professional front-end, so whether it is layout or logical development, it is a bit of an obstacle, but it only takes 2 days; so this module only takes 2 days from development to launch
The deployment of the project is very smooth. The Serverless Framework-based developer tools are deployed with one click, and the later update and maintenance only need to be redeployed, and the online switch is seamless. There is no service interruption caused by the update service, and there is no need to do additional operations for the update service may cause service interruption. The overall post-update process is fast and easy to use.
Resource consumption part is the use of pay-per-view, through a month of observation, the overall resource consumption is quite low, while the overall performance is guaranteed, the cost is also gradually depressed, for individual developers, it is indeed a boon.
Through such a simple process of Serverless architecture, I also have a deeper understanding and understanding of Serverless architecture. As a new technology or new architecture, it will take some time for Serverless to grow. But I believe that his growth will be very fast.
The above is what the programming study workers of Serverless architecture have, and have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.