用于编写可移植Node.js功能的开源FaaS(功能即服务)框架

网友投稿 939 2022-11-03

用于编写可移植Node.js功能的开源FaaS(功能即服务)框架

用于编写可移植Node.js功能的开源FaaS(功能即服务)框架

An open source FaaS (Function as a service) framework for writing portable Node.js functions -- brought to you by the Google Cloud Functions team.

The Functions Framework lets you write lightweight functions that run in many different environments, including:

Google Cloud FunctionsYour local development machineCloud Run and Cloud Run on GKEKnative-based environments

The framework allows you to go from:

exports.helloWorld = (req, res) => { res.send('Hello, World');};

To:

curl http://my-url# Output: Hello, World

All without needing to worry about writing an HTTP server or complicated request handling logic.

Features

Spin up a local development server for quick testingInvoke a function in response to a requestAutomatically unmarshal events conforming to the CloudEvents specPortable between serverless platforms

Installation

Add the Functions Framework to your package.json file using npm.

npm install @google-cloud/functions-framework

Quickstart: Hello, World on your local machine

Create an index.js file with the following contents:

exports.helloWorld = (req, res) => { res.send('Hello, World');};

Run the following command:

npx @google-cloud/functions-framework --function-target=helloWorld

Open http://localhost:8080/ in your browser and see Hello, World.

Quickstart: Set up a new project

Create an index.js file with the following contents:

exports.helloWorld = (req, res) => { res.send('Hello, World');};

To run a function locally, first create a package.json file using npm init:

npm init

Now install the Functions Framework:

npm install @google-cloud/functions-framework

Add a start script to package.json, with configuration passed via command-line arguments:

"scripts": { "start": "functions-framework --function-target=helloWorld" }

Use npm start to start the built-in local development server:

npm start...Serving function...Function: helloWorldURL: http://localhost:8080/

Send requests to this function using curl from another terminal window:

curl localhost:8080# Output: Hello, World

Run your function on serverless platforms

Google Cloud Functions

The Node.js 10 runtime on Google Cloud Functions is based on the Functions Framework. On Cloud Functions, the Functions Framework is completely optional: if you don't add it to your package.json, it will be installed automatically.

After you've written your function, you can simply deploy it from your local machine using the gcloud command-line tool. Check out the Cloud Functions quickstart.

Cloud Run/Cloud Run on GKE

Once you've written your function, added the Functions Framework and updated your start script in package.json, all that's left is to create a container image. Check out the Cloud Run quickstart for Node.js to create a container image and deploy it to Cloud Run. You'll write a Dockerfile when you build your container. This Dockerfile allows you to specify exactly what goes into your container (including custom binaries, a specific operating system, and more).

If you want even more control over the environment, you can deploy your container image to Cloud Run on GKE. With Cloud Run on GKE, you can run your function on a GKE cluster, which gives you additional control over the environment (including use of GPU-based instances, longer timeouts and more).

Container environments based on Knative

Cloud Run and Cloud Run on GKE both implement the Knative Serving API. The Functions Framework is designed to be compatible with Knative environments. Just build and deploy your container to a Knative environment.

Configure the Functions Framework

You can configure the Functions Framework using command-line flags or environment variables. If you specify both, the environment variable will be ignored.

Command-line flagEnvironment variableDescription
--portPORTThe port on which the Functions Framework listens for requests. Default: 8080
--function-targetFUNCTION_TARGETThe name of the exported function to be invoked in response to requests. Default: function
--function-signature-typeFUNCTION_SIGNATURE_TYPEThe signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: http; accepted values: http or event

You can set command-line flags in your package.json via the start script. For example:

"scripts": { "start": "functions-framework --function-target=helloWorld" }

Enable CloudEvents

The Functions Framework can unmarshall incoming CloudEvents payloads to data and context objects. These will be passed as arguments to your function when it receives a request. Note that your function must use the event-style function signature:

exports.helloEvents = (data, context) => { console.log(data); console.log(context);};

To enable automatic unmarshalling, set the function signature type to event using a command-line flag or an environment variable. By default, the HTTP signature will be used and automatic event unmarshalling will be disabled.

For more details on this signature type, check out the Google Cloud Functions documentation on background functions.

Contributing

Contributions to this library are welcome and encouraged. See CONTRIBUTING for more information on how to get started.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基本算法练习__约会问题
下一篇:基本算法练习——下起楼来我最快
相关文章

 发表评论

暂时没有评论,来抢沙发吧~