controller + stubs使用

1. 启动

此处,对于其余模块启动不赘述。详见README。

stubs

启动参数 eg: function-dir设置为 /var/faas/funcData

  1. ./stubs --function-dir="/var/faas/funcData" --logtostderr --port 8002

controller

启动参数

  1. ./controller --port=8001 --logtostderr --repository-endpoint=http://127.0.0.1:8002 --repository-version="v1" --repository-auth-type="noauth" --repository-auth-params="{}" --max-runtime-idle=10 --enable-metrics -v10

2. 使用stubs构建本地代码仓库

2.1 利用stubs创建函数

请求结构

  1. POST /v1/functions/<functionName> HTTP/1.1
  2. base64_code_bytes

参数

参数名称类型是否必需参数位置描述
functionNamestringPath参数函数名称,您可以指定一个函数名(例如,Thumbnail),或者您可以指定函数的BRN资源名(例如,brn:bce:faas:bj:account-id:function:thumbnail:$LATEST)。faas也允许您指定一个部分的BRN(例如,account-id:Thumbnail)。注意,BRN长度限制为1-170。如果只指定函数名,则长度限制为64个字符
VersionstringBody参数函数版本,若functionName指定为brn且指定了版本,此参数会被忽略。默认值为$LATEST
DescriptionstringBody参数函数描述
RuntimestringBody参数函数运行时,现支持nodejs6.11/nodejs8.5/nodejs10/lua5.3,默认值为nodejs8.5
TimeoutintBody参数函数超时时间,默认值为5
MemorySizeintBody参数函数运行内存,默认值为128
HandlerstringBody参数调用的入口函数,默认值为index.handler
PodConcurrentQuotaintBody参数预留并发度,默认值为0
CodestringBody参数函数代码zip包的base64字符串,默认为一个nodejs的hello world函数

请求示例

  1. POST /v1/functions/testHelloWorld HTTP/1.1
  2. Content-Type: application/json; charset=utf-8
  3. Host: 127.0.0.1:8002
  4. Connection: close
  5. User-Agent: Paw/3.1.8 (Macintosh; OS X/10.15.2) GCDHTTPRequest
  6. Content-Length: 990
  7. {
  8. "Version":"1",
  9. "Description":"stubs create",
  10. "Runtime":"python2.7",
  11. "Timeout":20,
  12. "MemorySize":512,
  13. "Handler":"index.hanler",
  14. "PodConcurrentQuota":10,
  15. "Code":"UEsDBBQACAAIAAyjX00AAAAAAAAAAAAAAAAIABAAaW5kZXguanNVWAwAsJ/ZW/ie2Vv6Z7qeS60o\nyC8qKdbLSMxLyUktUrBV0EgtS80r0VFIzs8rSa0AMRJzcpISk7M1FWztFKq5FIAAJqSRV5qTo6Og\n5JGak5OvUJ5flJOiqKRpzVVrDQBQSwcILzRMjVAAAABYAAAAUEsDBAoAAAAAAHCjX00AAAAAAAAA\nAAAAAAAJABAAX19NQUNPU1gvVVgMALSf2Vu0n9lb+me6nlBLAwQUAAgACAAMo19NAAAAAAAAAAAA\nAAAAEwAQAF9fTUFDT1NYLy5faW5kZXguanNVWAwAsJ/ZW/ie2Vv6Z7qeY2AVY2dgYmDwTUxW8A9W\niFCAApAYAycQGwFxHRCD+BsYiAKOISFBUCZIxwIgFkBTwogQl0rOz9VLLCjISdXLSSwuKS1OTUlJ\nLElVDggGKXw772Y0iO5J8tAH0QBQSwcIDgnJLFwAAACwAAAAUEsBAhUDFAAIAAgADKNfTS80TI1Q\nAAAAWAAAAAgADAAAAAAAAAAAQKSBAAAAAGluZGV4LmpzVVgIALCf2Vv4ntlbUEsBAhUDCgAAAAAA\ncKNfTQAAAAAAAAAAAAAAAAkADAAAAAAAAAAAQP1BlgAAAF9fTUFDT1NYL1VYCAC0n9lbtJ/ZW1BL\nAQIVAxQACAAIAAyjX00OCcksXAAAALAAAAATAAwAAAAAAAAAAECkgc0AAABfX01BQ09TWC8uX2lu\nZGV4LmpzVVgIALCf2Vv4ntlbUEsFBgAAAAADAAMA0gAAAHoBAAAAAA=="
  16. }

响应示例

  1. HTTP/1.1 200 OK
  2. Server: fasthttp

2.2 本地代码仓结构

  1. $ tree /var/faas/funcData
  2. /var/faas/funcData
  3. └── brn:bce:faas:bj:cd64f99c69d7c404b61de0a4f1865834:function:testHelloWorld:1 // 函数brn
  4. ├── code.zip // 代码zip包
  5. └── meta.json // 函数元信息

function meta

  1. {
  2. "_": {},
  3. "Code": {
  4. "_": {},
  5. "Location": "/var/faas/funcData/brn:cloud:faas:bj:cd64f99c69d7c404b61de0a4f1865834:function:testHelloWorld:1/code.zip",
  6. "RepositoryType": "filesystem"
  7. },
  8. "Concurrency": {
  9. "_": {},
  10. "ReservedConcurrentExecutions": null,
  11. "AccountReservedSum": 0
  12. },
  13. "Configuration": {
  14. "_": {},
  15. "CodeSha256": "4OFxEke82hUugwILdGb/BxnQdSUTsPAYcSU9PNVdFlU=",
  16. "CodeSize": 610,
  17. "DeadLetterConfig": null,
  18. "Description": "stubs create",
  19. "Environment": {
  20. "_": {},
  21. "Error": null,
  22. "Variables": null
  23. },
  24. "FunctionArn": "brn:cloud:faas:bj:cd64f99c69d7c404b61de0a4f1865834:function:testHelloWorld:1",
  25. "FunctionName": "testHelloWorld",
  26. "Handler": "index.hanler",
  27. "KMSKeyArn": null,
  28. "LastModified": "2020-01-06T14:04:19+08:00",
  29. "Layers": null,
  30. "MasterArn": null,
  31. "MemorySize": 512,
  32. "RevisionId": null,
  33. "Role": null,
  34. "Runtime": "python2.7",
  35. "Timeout": 20,
  36. "TracingConfig": null,
  37. "Version": "1",
  38. "VpcConfig": null,
  39. "CommitId": "349dd8a4-db7d-4fc3-a4cd-8c8b482e00c3",
  40. "Uid": "df391b08c64c426a81645468c75163a5",
  41. "SourceTag": "",
  42. "DeadLetterTopic": "",
  43. "PodConcurrentQuota": 10
  44. },
  45. "LogConfig": {
  46. "LogType": "",
  47. "BosDir": "",
  48. "Params": ""
  49. },
  50. "Tags": {}
  51. }

function code

  1. exports.handler = (event, context, callback) => {
  2. callback(null, "Hello world!WooHoo");
  3. };

3. 测试

测试stubs

  1. $ curl http://127.0.0.1:8002/v1/functions/brn:bce:faas:bj:cd64f99c69d7c404b61de0a4f1865834:function:testHelloWorld:1

controller invoke一个请求

  1. $ curl -X "POST" "http://127.0.0.1:8001/v1/functions/brn:bce:faas:bj:cd64f99c69d7c404b61de0a4f1865834:function:testHelloWorld:1/invocations" -H 'X-Bce-Account-Id: df391b08c64c426a81645468c75163a5' -H 'Content-Type: application/json; charset=utf-8'