跳到主要内容

本地开发云函数

云函数不仅可以运行在 云端环境 中,通过云开发提供的 tcb-ff 命令行工具,云函数也可以运行在 本地环境 中,这样就可以方便的在本地环境进行云函数的开发调试。

相比 云端环境本地环境 需要通过 tcb-ff 命令先将云函数运行起来,然后再通过 http 请求触发云函数执行。 各种发起 http 的方式都可以调用云函数,例如 curlpostman 或者 各编程语言提供的请求库进行调用 等。

本地开发云函数示例项目可参考:基于云函数2.0的全栈项目

以下为如何本地开发云函数的详细步骤,可结合示例项目进行体验。

调用本地运行的云函数

触发云函数的方式主要有:

  1. 通过云开发提供的SDK进行调用
    1. @cloudbase/js-sdk 运行在浏览器环境中,例如 小程序、web 等
    2. @cloudbase/node-sdk 运行在服务端 node.js 环境中,例如云函数中
  2. 通过 curl 命令行工具进行调用

通过云开发提供的SDK进行调用

如下图所示,图中演示了 通过 @cloudbase/js-sdk 调用 云函数-A云函数-A 中再通过 @cloudbase/node-sdk 调用 云函数-B 的场景。

调用链路:客户端(@cloudbase/js-sdk) ----> 云函数-A@cloudbase/node-sdk) ----> 云函数-B

云开发SDK默认会调用云端环境中运行的云函数,如果需要调用本地运行的云函数,则需要将到云端环境的中请求切换到本地运行的云函数。通过增加一个 本地代理层,即可实现将请求转发到本地。

call-local-cbrf

实现对本地运行的云函数的调用,首先需要启动一个 本地代理层,将请求转发到本地运行的云函数。

搭建 本地代理层 服务

whistle 是一个常用的跨平台的网络代理工具,可以实现将请求转发到本地。可以实现上图描述的 本地代理层 的功能,将请求转发到本地运行的云函数。

whistle 参考:https://wproxy.org/whistle/install.html

安装 whistle

npm install -g whistle

启动 whistle

$ w2 start
[!] whistle@2.9.94 is running
[i] 1. use your device to visit the following URL list, gets the IP of the URL you can access:
http://127.0.0.1:8899/
http://10.0.0.123:8899/
http://192.168.0.123:8899/
Note: If all the above URLs are unable to access, check the firewall settings
For help see https://github.com/avwo/whistle
[i] 2. set the HTTP proxy on your device with the above IP & PORT(8899)
[i] 3. use Chrome to visit http://local.whistlejs.com/ to get started

浏览器打开配置页面

open http://127.0.0.1:8899/

配置规则(将请求重写到本地):

^https://*.api.tcloudbasegateway.com/v1/cloudrun/*/* http://127.0.0.1:3000/$3?@fn=$2
^http://*.api.tcloudbasegateway.com/v1/cloudrun/*/* http://127.0.0.1:3000/$3?@fn=$2

最后,将浏览器 HTTP 代理配置为 http://127.0.0.1:8899 即可,可通过 Chrome 浏览器插件 ZeroOmega/SwitchyOmega 配置浏览器代理。 也可以通过配置系统级别的代理进行配置,执行 whistle 提供的 w2 proxy 命令会配置系统代理。

使用 @cloudbase/js-sdk 调用本地运行的云函数

使用 @cloudbase/js-sdk 在浏览器环境中调用本地运行的云函数,无需修改任何代码,只需要将浏览器发起的请求转发到本地 whistle 代理服务器即可实现调用本地运行的云函数。

例如如下示例的前端代码:

import cloudbase from '@cloudbase/js-sdk'

const tcbapp = cloudbase.init({
env: 'your-env-id',
clientId: 'your-client-id'
})

const auth = tcbapp.auth()

await auth.signInAnonymously()

// 调用 cloudbaserunfunctions/helloworld 函数
const result = await tcbapp.callFunction({
name: 'helloworld',
type: 'cloudrun'
})

console.log('result:', result)

完整示例项目代码可查看:https://github.com/TencentCloudBase/cloudbase-examples/blob/master/cloudrunfunctions/fullstack-project/webapp/src/index.js

使用 @cloudbase/node-sdk 调用本地运行的云函数

服务端使用 @cloudbase/node-sdk 调用本地运行的云函数,需要 tcb.init({}) 初始化时,增加 proxy 参数,即可将请求转发到本地 whistle 代理服务器。

注意:通常仅在本地环境中调用本地运行的云函数时,才需要配置 proxy 参数。该参数可以通过动态的方式进行配置。

  const tcbapp = tcb.init({
// 这里 Proxy 配置为本地代理层服务 Whistle
// Whistle 可以将请求转发到本地运行的云函数
proxy: 'http://127.0.0.1:8899'
})

完整代码:

import tcb from '@cloudbase/node-sdk'

exports.main = async (event, context) => {
const { httpContext } = context
const { url, httpMethod } = httpContext

const tcbapp = tcb.init({
// 这里 Proxy 配置为本地代理层服务 Whistle
proxy: 'http://127.0.0.1:8899',
context: {
extendedContext: {
tmpSecret: {
secretId: 'this-is-a-fak-secretId',
secretKey: 'this-is-a-fake-secretKey',
}
},
...context,
}
})

const result = await tcbapp.callFunction({
name: event.otherFuncName,

// 云函数2.0 参数
type: 'cloudrun',
method: 'POST',
path: '/abc',
data: {
key1: 'test value 1',
key2: 'test value 2'
}
}, {
timeout: 5000
})

return { result }
}

完整示例项目代码可查看:https://github.com/TencentCloudBase/cloudbase-examples/blob/master/cloudrunfunctions/fullstack-project/cloudbaserunfunctions/call-other-fn/index.js

通过 curl 命令行工具进行调用

通过 curl 可以直接发起对 云函数的 http 请求,无需进行任何配置。

该方式与调用云端运行的云函数时,使用的 云函数默认域名、HTTP访问服务 的方式是类似的。

# 发送 GET 请求
curl -v -XGET http://localhost:3000/path/to/xxx

# 发送 POST 请求
curl -v -XPOST 'http://127.0.0.1:3000/path/to/xxx \
-H 'Content-Type: application/json' \
--data-raw '{"name":"xxxxx"}'

# 发送 PUT 请求
curl -v -XPUT 'http://127.0.0.1:3000/path/to/xxx \
-H 'Content-Type: application/json' \
--data-raw '{"name":"xxxxx"}'

# 发送 DELETE 请求
curl -v -XDELETE 'http://127.0.0.1:3000/path/to/xxx

一个通过云函数开发AI智能体请求的示例:

curl -XPOST 'http://127.0.0.1:3000/v1/aibot/bots/ibot-xxxx/send-message' \
-H 'Accept: text/event-stream' \
-H 'Content-Type: application/json' \
--data-raw '{"name":"xxxxx"}'

以上是通过 curl 发起请求的示例,可以通过任意其他方式发起 http 请求,例如 postman浏览器 fetchnode.js http.request 等。