In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to migrate the Serverless to the laboratory station. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Preface
In February, TencentServerless held a series of online classes to share technical knowledge such as Serverless concepts, architecture, best practices and how to develop a component.
Because I was very interested in Serverless, I took part in the live study and submitted my class homework every time, and I felt good all the way, so I decided to move my laboratory station to Serverless to try.
1. TencentServerless introduction
I have to lament the progress of science and technology in the Internet era. before, my laboratory station used the traditional method to publish the environment of the website, although it is not troublesome to be familiar with the operation now. but for people who have never been in touch with this area, it is more difficult to understand.
Now that you have Serverless, you can completely ignore the above steps. Here are two paragraphs from the official website:
Serverless Framework can help you quickly build Serverless applications with less cost and overhead. It can perfectly support the development, deployment, testing, monitoring and other aspects of serverless applications. Serverless is a future-oriented mode of operation and maintenance.
Serverless is built on the next generation of public cloud services, which are automatically expanded and charged only when they are used. 99% cost management savings can be achieved when scale, capacity used, and cost management are automated.
The serverless architecture is new, so we need to change our previous view of the old architecture and workflow. The goal of Serverless Framework is to provide developers and teams with all the tools they need to develop and run serverless applications in a simple, powerful and elegant experience.
This method is very convenient, but I now think that for individual developers, using Serverless should save a lot of time if they want to build lightweight applications. Of course, Serverless is different from traditional applications. At present, it can not be perfectly supported. For example, Flask CLI does not support it, but I believe that with the development of Serverless technology, the support of Serverless will be more comprehensive.
The same is true for enterprise developers. If you want to launch a set of websites quickly, it is easy to deploy on one server, but when the traffic increases, it will be troublesome when you need to expand your capacity. At this time, you have to deploy and configure load balancer on multiple servers and so on.
Personally, I think the greatest advantage of Serverless lies in the deployment of operation and maintenance, which is very convenient to deploy through Serverless.
two。 Install Serverless Framework
Serverless Framework is an open source CLI based on Node.js. Note: global installation of Node 8 + is required:
Npm install serverless-g
The reason why cnpm is not used here is that ok is not particularly time-consuming on the network, and I have forgotten where I have seen that cnpm will not update package-lock.json, so I no longer use third-party sources. If you update it later,
Npm install serverless-g
After the Quick start tutorial on the official website, a demo is quickly deployed, namely:
Serverless create-t tencent-nodejs
The tencent-nodejs in the command is one of the many components, component list: https://github.com/serverless/components
3. Deploy the Python Flask framework
Because I am quite familiar with Flask, I might as well regard the deployment of this Component as a Hello World. The introduction to the official website says: any Python server framework that supports WSGI (Web Server Gateway Interface) can be deployed through this component, such as the Falcon framework.
1) create a new project
Based on template
Create services directly from templates through sls. There are many templates on Serverless github, such as https://github.com/serverless/components/tree/master/templates/tencent-flask
Serverless create-- template-url https://github.com/serverless/components/tree/master/templates/tencent-flask
The source code is as follows
#-*-coding: utf8-*-import jsonfrom flask import Flask, jsonify, requestapp = Flask (_ name__) @ app.route ("/") def index (): return "Hello Flash" @ app.route ('/ user', methods = ['POST']) def addUser (): # we must get request body from clound function event Event = request.environ ['event'] user = json.loads (event [' body']) return jsonify (data=user) @ app.route ("/ user", methods = ['GET']) def listUser (): users = [{' name': 'test1'}, {' name': 'test2'}] return jsonify (data=users) @ app.route ("/ user/") Methods = ['GET']) def getUser (id): return jsonify (data= {' name': 'test1'})
Not based on template
Create a new Flask project in Pycharm: LAB_Serverless to distinguish the previous LAB
The source code is as follows
From flask import Flask app = Flask (_ _ name__) @ app.route ('/') def hello_world (): return 'Hello Worldwide' If _ _ name__ = ='_ _ main__': app.run ()
2) configure Serverless
Create serverless.yml, where several configuration changes have been made
MyComponent: component:'@ serverless/tencent-flask' inputs: region: ap-beijing functionName: LAB_Serverless code:. / functionConf: timeout: 10 memorySize: 128environment: variables: TEST: value Version: 2020-2-23mm 21V 01VR 44 vpcConfig: subnetId:''vpcId:' 'apigatewayConf: protocol: https environment: test
Create .env and write the key (because you are too lazy to pick up the phone and scan the authorization (^ _ −) ☆ every time you deploy.
TENCENT_SECRET_ID=TENCENT_SECRET_KEY=
3) deployment
The abbreviation for serverless is sls, so you can also use sls to simplify commands. But there was a mistake here... The error was reported because the requirements folder does not exist.
View terminal
Microsoft Windows [version 10.0.17763.1039] (c) 2018 Microsoft Corporation. All rights reserved. D:\ yuangezhizao\ Documents\ PycharmProjects\ LAB_Serverless > sls-- debug DEBUG ─ Resolving the template's static variables. DEBUG ─ Collecting components from the template. DEBUG ─ Downloading any NPM components found in the template. DEBUG ─ Analyzing the template's components dependencies. DEBUG ─ Creating the template's components graph. DEBUG ─ Syncing template state. DEBUG ─ Executing the template's components graph. DEBUG ─ Compressing function LAB_Serverless file to D:\ yuangezhizao\ Documents\ PycharmProjects\ LAB_Serverless\ .serverless / LAB_Serverless.zip. (node:22500) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory Stat'D:\ yuangezhizao\ Documents\ PycharmProjects\ LAB_Serverless\ .serverless\ requirements'eploying at Object.statSync (fs.js:946:3) at Object.statSync (C:\ Users\ yuangezhizao\ AppData\ Roaming\ npm\ node_modules\ serverless\ node_modules\ _ graceful-fs@4.2.3@graceful-fs\ polyfills.js:308:16) at WriteStream. (C:\ Users\ yuangezhizao\ .serverless\ components\ registry\ npm\ @ serverless\ tencent-flask@0.2.0\ node_modules\ @ serverless\ tencent-flask\ node_modules\ @ serverless\ tencent-scf\ library\ utils.js:124:20) at WriteStream.emit (events.js:304:20) at C:\ Users\ yuangezhizao\ .serverless\ components\ registry\ npm\ @ serverless\ tencent-flask@0.2.0\ node_modules\ @ serverless\ tencent-flask Node_modules\ graceful-fs\ graceful-fs.js:298:14 at C:\ Users\ yuangezhizao\ .serverless\ components\ registry\ npm\ @ serverless\ tencent-flask@0.2.0\ node_modules\ @ serverless\ tencent-flask\ node_modules\ graceful-fs\ graceful-fs.js:325:16 at C:\ Users\ yuangezhizao\ AppData\ Roaming\ npm\ node_modules\ serverless\ node_modules\ _ graceful-fs@4.2.3@graceful-fs\ graceful-fs. Js:325:16 at FSReqCallback.oncomplete (fs.js:152:23) (node:22500) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function withouta catch block, or by rejecting a promise which was not handled with. Catch (). (rejection id: 1) (node:22500) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. Will the batch operation be terminated by 194s »MyComponent »canceled? Y D:\ yuangezhizao\ Documents\ PycharmProjects\ LAB_Serverless >
Then go to the Template.MyComponent.pyRequirements.json file under the .serverless file and see requirements.txt. This is actually a deliberate operation (no requirements.txt has been added), which means that requirements.txt must exist!
So, to create a requirements.txt with a file content of Flask
D:\ yuangezhizao\ Documents\ PycharmProjects\ LAB_Serverless > sls-- debug DEBUG ─ Resolving the template's static variables. DEBUG ─ Collecting components from the template. DEBUG ─ Downloading any NPM components found in the template. DEBUG ─ Analyzing the template's components dependencies. DEBUG ─ Creating the template's components graph. DEBUG ─ Syncing template state. DEBUG ─ Executing the template's components graph. DEBUG ─ Generated requirements from D:\ yuangezhizao\ Documents\ PycharmProjects\ LAB_Serverless\ requirements.txt in D:\ yuangezhizao\ Documents\ PycharmProjects\ LAB_Serverless\ .serverless\ requirements.txt... DEBUG ─ Installing requirements from C:\ Users\ yuangezhizao\ AppData\ Local\ Yugasun\ serverless-python-requirements\ Cache\ 2a1a661c4e3e6faadab5d001bc10cc3acccf648921aad7c279d94f138eaaf833_slspyc\ requirements.txt. DEBUG ─ Using download cache directory C:\ Users\ yuangezhizao\ AppData\ Local\ Yugasun\ serverless-python-requirements\ Cache\ downloadCacheslspyc DEBUG ─ Running. DEBUG ─ Compressing function LAB_Serverless file to D:\ yuangezhizao\ Documents\ PycharmProjects\ LAB_Serverless\ .serverless / LAB_Serverless.zip. DEBUG ─ Compressed function LAB_Serverless file successful DEBUG ─ Uploading service package to cos [sls-cloudfunction-ap-beijing-code]. Sls-cloudfunction-default-LAB_Serverless-1582464464.zip DEBUG ─ Uploaded package successful D:\ yuangezhizao\ Documents\ PycharmProjects\ LAB_Serverless\ .serverless / LAB_Serverless.zip DEBUG ─ Creating function LAB_Serverless DEBUG ─ Created function LAB_Serverless successful DEBUG ─ Setting tags for function LAB_Serverless DEBUG ─ Creating trigger for function LAB_Serverless DEBUG ─ Deployed function LAB_Serverless successful DEBUG ─ Starting API-Gateway deployment with name MyComponent.TencentApiGateway in the ap-beijing region DEBUG ─ Service with ID service-0ok85tqh created. DEBUG ─ API with id api-ivk6tk0y created. DEBUG ─ Deploying service with id service-0ok85tqh. DEBUG ─ Deployment successful for the api named MyComponent.TencentApiGateway in the ap-beijing region. MyComponent: region: ap-beijing functionName: LAB_Serverless apiGatewayServiceId: service-0ok85tqh url: http://service-0ok85tqh-1251901037.bj.apigw.tencentcs.com/test/ 44s »MyComponent »done D:\ yuangezhizao\ Documents\ PycharmProjects\ LAB_Serverless >
Take a look at the .serverless folder after the deployment is successful:
Here Template.MyComponent.TencentCloudFunction.json is the cloud function.
{"deployed": {"Name": "LAB_Serverless", "Runtime": "Python3.6", "Handler": "api_service.handler", "MemorySize": 128, "Timeout": 10, "Region": "ap-beijing", "Description": "This is a template function"}}
The third-party packages are all here:
Template.MyComponent.TencentApiGateway.json is the API gateway
{"protocols": ["http"], "subDomain": "service-0ok85tqh-1251901037.bj.apigw.tencentcs.com", "environment": "test", "region": "ap-beijing", "service": {"value": "service-0ok85tqh", "created": true}, "apis": [{"path": "/", "method": "ANY" "apiId": {"value": "api-ivk6tk0y", "created": true}]}
In other words, CLI automatically helps us create SCF and upload the running environment, and then create an API gateway and configure it on the trigger of SCF.
ApigatewayConf: protocol: https environment: test
At this point, demo is done, and you can visit it normally.
4. In-depth principle
Go to the cloud function to see the actual running environment, and find that the .idea folder has also been uploaded. In addition, there are two more files that are not available locally:
In fact, this is the core of Serverless, Serverless configuration of static pages of the principle is clear. For example, Hexo can be accessed by generating a page and uploading it to COS.
However, it is more curious about dynamic pages, how is this achieved? In fact, it is relying on the serverless.wsgi file and so on. You can see the description of this module: this module converts AWS APIGateway proxy requests into WSGI requests.
"" This module converts an AWS API Gateway proxied request to a WSGI request. Inspired by: https://github.com/miserlou/zappa Author: Logan Raarup ""
It's still quite interesting.
5. Migrate LAB
Then you have to migrate bit by bit, and it's not hard to imagine that there should be a lot of holes, such as how to access your own MySQL, Redis, MongoDB, and Celery to plan tasks and use RabbitMQ's message queue. How do you get this thing on the cloud?
On how to carry out the laboratory station migration Serverless to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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: 253
*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.