Introduction to the
Using Puppeteer combined with function computing, you can quickly build resilient services to perform a variety of functions, including:
- Generate web screenshots or PDFS
- Advanced crawler, can climb a large number of asynchronously rendered content of the web page
- Simulate keyboard input, form automatic submission, login web page, etc., to achieve UI automatic testing
- Capture a timeline of your site to track your site and help analyze site performance issues
Next, we will develop our own Puppeteer project based on a scaffolding project.
Download the project
>>> git clone -o starter-kit https://github.com/awesome-fc/puppeteer-fc-starter-kit.git your_project_nameCopy the code
The project structure
├─ chrome │ ├─ buildchrome. sh buildChrome headless shell script based on the ├─ chrome │ ├─ buildchrome. sh buildChrome headless shell script based on the ├─ chrome │ ├─ buildChrome. You can compile the latest version │ └ ─ ─ headless_shell. Tar. Gz compiled chrome headless binary package files ├ ─ ─ the SRC │ ├ ─ ─ index. The js function call entry documents, │ ├─ gene-starter -kit │ ├─ config.js └─ gene-starter -kit │ ├─ config.js └─ gene-starter │ ├─ local.js To check whether the Chrome Headless API is correct by running NPM runlocal├─ ├─ package.zip, └─ ├─ package.zip, └─ ├─ garbage, └─ garbage, └─ garbage, └─ garbage, └─ garbage NPM run package or NPM run package-nochromeCopy the code
How to write business code
Index. Js file
const setup = require('./starter-kit/setup'); exports.handler = async (event, context, callback) => { const browser = await setup.getBrowser(context); exports.run(browser).then( (result) => callback(null, result) ).catch( (err) => callback(err) ); }; Exports.run = async (browser) => {// Implement your own business code // here is a sample code, Const page = await browser.newPage(); const page = await browser.newPage(); await page.goto('https://fc.console.aliyun.com');
return page.screenshot({
clip: {
x: 200,
y: 60,
width: 780,
height: 450,
},
});
};Copy the code
Local test
Run NPM run local. Test that your API for operating Chrome Headless works correctly.
Compile the package
Contains chrome headless
Run the NPM run package command. The command will pack chrome Headless files into the project and place them in the root directory of the project. When this function is executed, chrome Headless will be installed in/TMP by default.
Chrome Headless is not included
Run NPM run package-nochrome. The chrome Headless command will not package chrome headless files into the project, you will need to upload Chrome-headless files to OSS in addition, and use environment variables to tell functions how to download and install from OSS. By default, Chrome Headless will be installed in/TMP directory. Set environment variables for function evaluation:
- CHROME_BUCKET(Mandatory): Chrome headless bucket uploaded to the OSS
- CHROME_REGION(Mandatory): Chrome headless region uploaded to OSS
- CHROME_KEY(Mandatory): Chrome headless key to upload to OSS. Default value: headless_shell.tar.gz
The deployment of
Compile the packaged output file: package.zip, and upload the packaged file using the function computing console or the fCLI command tool. Fcli Command for uploading packaged files:
# in the corresponding service directory
>>> mkf myFunction -h index.handler -f package.zip -t nodejs8 The code is in package.zipCopy the code
Why use a function
Functional computing is a relatively new thing, probably unfamiliar to most developers or architects. Microservice architecture is a very popular system architecture at present. Is there anything not perfect about microservice architecture? The answer is yes. Based on the microservice architecture, we need to consider what microservice framework to use to construct the system, how many servers are needed, and how to effectively reduce the burden of operation and maintenance. In addition, according to Forbes magazine, typical servers in commercial and enterprise data centers provide only 5 to 15 percent of their average maximum processing power output. This is undoubtedly a huge waste of resources.
Is there a way to solve the above shortcomings? The answer is also yes, Ali cloud function calculation is in this context emerged. If the cloud computing as an execution environment, so, in this environment, the function (i.e., the carrier of the business logic) + data (i.e., input and output related to business) is the core of the application, function, data, events, these three any other code and framework, is nothing but the application of glue and the UI. The ideal scenario, then, is to spend as little time writing glue as possible, devote more time to core application development, and even microservize the entire software stack. The function calculation is the glue here.
Development efficiency
Project development efficiency | Traditional micro services | Method of function calculation |
---|---|---|
Procurement of infrastructure such as servers | Need to be | Don’t need |
Manage infrastructure such as servers | Need to be | Don’t need |
Develop business service modules | Need to be | Need to be |
Deploy the business service module | Need to be | Yes, but it’s simple and provides a lot of tools |
Use Nginx to set up reverse proxy, HTTPS, and load balancing | Need to be | No need, through ali Cloud API gateway or function HTTP trigger can be implemented |
Set up related log services | Need to be | No, enable the log service through the log switch |
Configure secure access rules | Need to be | No, built-in basic security access configuration, API gateway provides more configuration |
Operational burden | heavy | light |
Development efficiency | low | B: High, usually in a few days |
reliability
Project reliability | Traditional micro services | Method of function calculation |
---|---|---|
The peak pressure | When the peak pressure increases, service overload may occur if computing resources are not expanded in a timely manner | Elastic expansion in the face of peak pressure |
Error handling | You need to handle server downtime and process crashes by yourself | Without having to deal with failures such as servers, functions are computed to make the infrastructure fault-tolerant |
The project architecture | Infrastructure management is complex | The system architecture has been verified by a large number of projects and polished for a long time, which is relatively stable and reliable |
The cost of
The project cost | Traditional micro services | Method of function calculation |
---|---|---|
Procurement of servers and other infrastructure costs | High cost and low utilization | There is no |
Code development cost | High cost, including infrastructure code and business code | Low cost, focus only on business code development |
Cost of function calculation | There is no | Low cost, pay on demand, and a certain monthly exemption |
Operational costs | It is expensive and difficult | The cost is low, the difficulty is very small, and special operation and maintenance personnel are not even needed |
Integrate ali Cloud other product costs, such as OSS and RDS, etc | The high cost | Low cost, providing seamless integration of ali Cloud’s other products |
security
The project safety | Traditional micro services | Method of function calculation |
---|---|---|
Authentication and access control | You need to implement it yourself | Unless you explicitly allow the function to be called anonymously, the function evaluation will authenticate each API call. Only with your explicit authorization (via Aliyun Access Control Service, RAM) can your functions access other cloud service resources or be called by other users/cloud services. With ali Cloud API gateway, you can also use mechanisms such as OpenID to authenticate function calls |
Operation tracking and audit | You need to implement it yourself | You need to implement it yourself |
Data security | You need to implement it yourself | By using features like access control authorization and functions to calculate environment variables, you no longer have to keep sensitive information like access credentials in your code. All data that you persist with the function computing service, including code, environment variables, and so on, is encrypted and stored. The system will check the integrity of the code before running it |
Runtime security | You need to implement it yourself | Each function is executed in a separate, isolated environment. Function computing provides the same isolation strength as Ali Cloud Elastic Computing (ECS). Function calculations periodically reset the runtime environment and automatically update the operating system and security patches for run-time dependent software |
Abnormal response | You need to implement it yourself | Function calculations allow you to set an upper limit on resource usage and ensure that your expenses are under control. At the same time, function calculation provides function call times, flow control times, error rate and other indicators. With the help of cloud monitoring, you can set monitoring alarms for relevant indicators and quickly respond to abnormal situations |
conclusion
Function calculation has the following advantages:
- No need to purchase and manage infrastructure such as servers
- Focus on business logic development
- Provides log query, performance monitoring, and alarm functions to quickly rectify faults
- Trigger an application to respond to a user request in an event-driven manner
- Flexible expansion at the millisecond level enables rapid expansion of the bottom layer to cope with peak pressure
- Pay on demand. You only need to pay for the computing resources actually used, which is suitable for user access scenarios with obvious peaks and troughs
- The original link