定制 google/cloud-functions-framework 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

google/cloud-functions-framework

Composer 安装命令:

composer require google/cloud-functions-framework

包简介

Google Cloud Functions Framework for PHP

README 文档

README

PHP unit CI PHP lint CI PHP conformace CI Security Scorecard

An open source FaaS (Function as a service) framework for writing portable PHP functions.

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

  • Your local development machine
  • Knative-based environments

The framework allows you to go from:

use Psr\Http\Message\ServerRequestInterface;

function helloHttp(ServerRequestInterface $request)
{
    return "Hello World from a PHP HTTP function!" . PHP_EOL;
}

To:

curl http://my-url
# Output: "Hello World from a PHP HTTP function!"

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

Watch this video to learn more about Functions Frameworks.

Features

  • Spin up a local development server for quick testing
  • Invoke a function in response to a request
  • Automatically unmarshal events conforming to the CloudEvents spec
  • Portable between serverless platforms

Installation

Add the Functions Framework to your composer.json file using Composer.

composer require google/cloud-functions-framework

Define your Function

Create an index.php file with the following contents:

<?php

use Psr\Http\Message\ServerRequestInterface;

function helloHttp(ServerRequestInterface $request)
{
    return "Hello World from a PHP HTTP function!" . PHP_EOL;
}

Quickstarts

Run your function locally

After completing the steps under Installation and Define your Function, run the following commands:

export FUNCTION_TARGET=helloHttp
php -S localhost:8080 vendor/google/cloud-functions-framework/router.php

Open http://localhost:8080/ in your browser and see Hello World from a PHP HTTP function!.

Run your function in a container

After completing the steps under Installation and Define your Function, build the container using the example Dockerfile:

docker build . \
    -f vendor/google/cloud-functions-framework/examples/hello/Dockerfile \
    -t my-cloud-function

Run the cloud functions framework container:

docker run -p 8080:8080 \
    -e FUNCTION_TARGET=helloHttp \
    my-cloud-function

Open http://localhost:8080/ in your browser and see Hello World from a PHP HTTP function. You can also send requests to this function using curl from another terminal window:

curl localhost:8080
# Output: Hello World from a PHP HTTP function!

Run your function on Google Cloud Run Functions

NOTE: For an extensive list of samples, see the [PHP functions samples][functions-samples] and the [official how-to guides][functions-how-to].

Follow the Cloud Run function quickstart for PHP to learn how to deploy a function to Cloud Run.

Run your function as a container in Cloud Run

You can manually build your function as a container and deploy it into Cloud Run. Follow the Cloud Run instructions for building a function for complete instructions.

Use CloudEvents

The Functions Framework can unmarshall incoming CloudEvents payloads to a cloudevent object. This will be passed as arguments to your function when it receives a request. Note that your function must use the cloudevent function signature:

use Google\CloudFunctions\CloudEvent;

function helloCloudEvent(CloudEvent $cloudevent)
{
    // Print the whole CloudEvent
    $stdout = fopen('php://stdout', 'wb');
    fwrite($stdout, $cloudevent);
}

You will also need to set the FUNCTION_SIGNATURE_TYPE environment variable to cloudevent.

export FUNCTION_TARGET=helloCloudEvent
export FUNCTION_SIGNATURE_TYPE=cloudevent
php -S localhost:8080 vendor/google/cloud-functions-framework/router.php

In a separate tab, make a cURL request in Cloud Event format to your function:

curl localhost:8080 \
    -H "ce-id: 1234567890" \
    -H "ce-source: //pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC" \
    -H "ce-specversion: 1.0" \
    -H "ce-type: com.google.cloud.pubsub.topic.publish" \
    -d '{"foo": "bar"}'

Your original process should output the following:

CLOUDEVENT metadata:
- id: 1234567890
- source: //pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC
- specversion: 1.0
- type: com.google.cloud.pubsub.topic.publish
- datacontenttype:
- dataschema:
- subject:
- time:

IMPORTANT: The above tutorials to deploy to a docker container and to Cloud Run work for CloudEvents as well, as long as FUNCTION_TARGET and FUNCTION_SIGNATURE_TYPE are set appropriately.

Working with PSR-7 HTTP Objects

The first parameter of your function is a Request object which implements the PSR-7 ServerRequestInterface:

use Psr\Http\Message\ServerRequestInterface;

function helloHttp(ServerRequestInterface $request): string
{
    return sprintf("Hello %s from PHP HTTP function!" . PHP_EOL,
        $request->getQueryParams()['name'] ?? 'World');
}

You can return a PSR-7 compatible ResponseInterface instead of a string. This allows you to set additional request properties such as the HTTP Status Code and headers.

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;

function helloHttp(ServerRequestInterface $request): ResponseInterface
{
    $body = sprintf("Hello %s from PHP HTTP function!" . PHP_EOL,
        $request->getQueryParams()['name'] ?? 'World');

    return (new Response())
        ->withBody(Utils::streamFor($body))
        ->withStatus(418) // I'm a teapot
        ->withHeader('Foo', 'Bar');
}

A request to this function will produce a response similar to the following:

HTTP/1.1 418 I'm a teapot
Host: localhost:8080
Date: Wed, 03 Jun 2020 00:48:38 GMT
Foo: Bar

Hello World from PHP HTTP function!

See the PSR-7 documentation documentation for more on working with the request and response objects.

Use Google Cloud Storage

When you require the google/cloud-storage package with composer, the functions framework will register the gs:// stream wrapper. This enables your function to read and write to Google Cloud Storage as you would any filesystem:

// Get the contents of an object in GCS
$object = file_get_contents('gs://{YOUR_BUCKET_NAME}/object.txt');
// Make modifications
$object .= "\nadd a line";
// Write the new contents back to GCS
file_put_contents('gs://{YOUR_BUCKET_NAME}/object.txt', $object);

You can unregister this at any time by using stream_wrapper_unregister:

// unregister the automatically registered one
stream_wrapper_unregister('gs');

Run your function 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.

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).

Configure the Functions Framework

You can configure the Functions Framework using the environment variables shown below:

Environment variable Description
FUNCTION_TARGET The name of the exported function to be invoked in response to requests.
FUNCTION_SOURCE The name of the file containing the source code for your function to load. Default: index.php (if it exists)
FUNCTION_SIGNATURE_TYPE The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Can be either http, event, or cloudevent. Default: http

Contributing

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

google/cloud-functions-framework 适用场景与选型建议

google/cloud-functions-framework 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.57M 次下载、GitHub Stars 达 213, 最近一次更新时间为 2019 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 google/cloud-functions-framework 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 google/cloud-functions-framework 我们能提供哪些服务?
定制开发 / 二次开发

基于 google/cloud-functions-framework 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 15.57M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 214
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 213
  • Watchers: 34
  • Forks: 37
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2019-12-13