In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Abstract: Web application (Serverless web backend) is a very important usage scenario for function computing. Compared with traditional web applications built on servers, function computing does not require you to manage the server and other infrastructure. You only need to write code and upload it. Function computing will prepare computing resources for you to run your code in a flexible and reliable manner, achieving self-scaling in milliseconds and easily coping with peak pressure.
A brief introduction to function calculation
Ali Cloud functional Computing (Function Compute) is an event-driven fully managed computing service and is Ali Cloud's Serverless computing platform. To build applications based on functional computing, you do not need to manage infrastructure such as servers, but focus on the development of application logic, which greatly improves the efficiency of development.
With functional computing, you can build almost any type of application or back-end service, including Web application back-end services, large-scale file processing, real-time data streaming, and so on. Function computing can flexibly scale computing resources in real time to match your application load, so that your system can consistently and reliably support the rapid growth of business. In addition, you only need to pay for the resources actually used, and if the function is not executed, you will not pay, and the financial cost will be significantly reduced.
Function calculation meets Web application
Web application (Serverless web backend) is a very important usage scenario for function computation. Compared with traditional web applications built on servers, function computing does not require you to manage the server and other infrastructure. You only need to write code and upload it. Function computing will prepare computing resources for you to run your code in a flexible and reliable manner, achieving self-scaling in milliseconds and easily coping with peak pressure.
In order to better support Serverless web backend usage scenarios and enhance user experience, function computing introduces HTTP triggers, that is, functions can be triggered by HTTP requests. You only need to write the business code in the function to build Web applications. At the same time, various programming languages of HTTP triggers are compatible with the popular Web framework in varying degrees, so it is convenient for you to migrate the original services to function calculation and really implement a line of code. It's on the trigger.
HTTP trigger
HTTP triggers, as the name implies, that is, HTTP requests can trigger function execution. Functions that configure HTTP triggers can trigger execution through HTTP requests. At this point, the function can be regarded as a Web application, which processes the HTTP request and returns the processing result to the caller.
Currently, HTTP triggers for function computing support programming languages such as nodejs, python, php, and support the corresponding Web framework to varying degrees. For more information, please see the documentation.
Use the example
The following is an example of using HTTP triggers to build Web applications.
There are three steps to build a Web application based on HTTP triggers:
Configure HTTP triggers
Only functions configured with HTTP triggers can process HTTP requests
It is worth noting that the function interface configured with HTTP triggers is different from the normal function interface.
Write processing functions
When writing processing functions, it must be noted that the function interface of the HTTP trigger is different from the ordinary function interface.
Test function execution
Trigger function execution by sending a request to the function HTTP and verify that it meets expectations.
Configure HTTP triggers
Login function calculates the administrative console.
Create a new service. The name of the service in this example is web-server, and the region to which it belongs is East China 2. Enable "Advanced configuration", configure log items and log repositories, and create roles by creating new roles. Select AliyunLogFullAccess for the system authorization template, click "Authorization", and click "OK".
Select the service in the left navigation bar to enter the new service.
Click "create function", and on the create function page:
Select anonymous as the authentication method, which does not require authentication and supports anonymous access.
The request method is the HTTP Method that can be handled by this function.
Click Select all languages, and select the development language from the drop-down menu.
Select [blank function].
Configure the HTTP trigger as shown below, and click "next".
Create a function and fill in the service, function name, description information and running environment information.
Click [next].
After checking the information, click * * create * *.
For more information on configuring HTTP triggers, please refer to the official documentation for configuring HTTP triggers.
Write function code
Take Python as an example, a simple example code is as follows:
#-*-coding: utf-8-*-import jsonHELLO_WORLD = b "Hello world!\ n" def handler (environ, start_response): request_uri = environ ['fc.request_uri'] response_body = {' uri':environ ['fc.request_uri'],' method':environ ['REQUEST_METHOD']} # do something here status =' 200 OK' response_headers = [('Content-type'] 'text/json')] start_response (status, response_headers) # Python2 return [json.dumps (response_body)] # Python3 tips: When using Python3, the str and bytes types cannot be mixed. # Use str.encode () to go from str to bytes # return [json.dumps (response_body) .encode ()]
For more code examples and details of programming languages, see writing function code.
In just two steps, the Web application based on HTTP trigger is completed. Let's test the function to see if it meets our expectations.
Test function execution
You need to send a HTTP request to the function to test whether the function execution is as expected. You can send HTTP requests through the browser, send HTTP requests from curl to test, and the console can also send HTTP requests to facilitate your debugging.
For example, sending a HTTP request via curl triggers the function to execute, and the result is as follows:
~ curl https://123456.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/web-server/testHttp/{"uri": "/ 2016-08-15/proxy/web-server/testHttp/", "method": "GET"}%
To send a HTTP request through the console and browser, please refer to the article test function for execution.
Summary
Using HTTP triggers, you can quickly build Web services based on function computing, pay by quantity, free of operation and maintenance, and expand flexibly in milliseconds to solve user pain points perfectly.
The era of Serverless has come, one line of code, on the trigger.
Reference documentation
HTTP trigger document
HTTP trigger exampl
Set the Nodejs function entry for HTTP triggers
Set the Python function entry for HTTP triggers
Set the PHP function entry for HTTP triggers
Building Serverless Web applications by function calculation (2)-Custom Domain name
Building Serverless Web applications by function Computing (3)-Building Web applications in three minutes
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.