操作运行(使用easyfaas函数计算服务)

安装运行完成后,即可进行下一步

1. 创建函数

使用stub组件构建本地代码仓库其创建函数接口API详见接口API文档, 可以使用curl或者其他工具如Paw发起创建函数请求。

1.1 编写函数代码

首先编写一个简单的nodejs10的hello world 函数代码,代码存储在index.js文件中 备注: 您也可以直接跳过此步骤,直接运行1.1和1.2,直接运行1.3

  1. $ cat index.js
  2. exports.handler = (event, context, callback) => {
  3. callback(null, "Hello world!");
  4. };

1.2 打包函数代码并获得其base64编码

  1. $ zip code.zip index.js
  2. $ base64 code.zip
  3. UEsDBBQAAAAIAAxDX00vNEyNUAAAAFgAAAAIABwAaW5kZXguanNVVAkAA/ie2VuKQthfdXgLAAEE
  4. 6AMAAAToAwAAS60oyC8qKdbLSMxLyUktUrBV0EgtS80r0VFIzs8rSa0AMRJzcpISk7M1FWztFKq5
  5. FIAAJqSRV5qTo6Og5JGak5OvUJ5flJOiqKRpzVVrDQBQSwECHgMUAAAACAAMQ19NLzRMjVAAAABY
  6. AAAACAAYAAAAAAABAAAApIEAAAAAaW5kZXguanNVVAUAA/ie2Vt1eAsAAQToAwAABOgDAABQSwUG
  7. AAAAAAEAAQBOAAAAkgAAAAAA

1.3 调用func-registry创建函数接口创建函数

请求body中Code字段填入上一步骤中获得的base64编码

  1. $ curl -X POST "http://<YOUR_IP>:<YOUR_FUNC_REGISTRY_PORT>/v1/functions/testHelloWorld" -d '{"Version":"1","Description":"stubs create","Runtime":"nodejs10","Timeout":5,"MemorySize":128,"Handler":"index.handler","PodConcurrentQuota":10,"Code":"UEsDBBQAAAAAAHCjX00AAAAAAAAAAAAAAAAJABUAX19NQUNPU1gvVVgIALSf2Vu0n9lbVVQFAAG0n9lbUEsDBBQACAAIAAyjX00AAAAAAAAAAAAAAAATABUAX19NQUNPU1gvLl9pbmRleC5qc1VYCACwn9lb+J7ZW1VUBQAB+J7ZW2JgFWNnYGJg8E1MVvAPVohQgAKQGAMnAwODEQMDQx0DA5i/gYEo4BgSEgRlgnQsYGBgEEBTwogQl0rOz9VLLCjISdXLSSwuKS1OTUlJLElVDggGKXw772Y0iO5J8tAH0YAAAAD//1BLBwgOCcksZgAAALAAAABQSwMEFAAIAAgAAAAAAAAAAAAAAAAAAAAAAAgAAABpbmRleC5qc0qtKMgvKinWy0jMS8lJLVKwVdBILUvNK9FRSM7PK0mtADESc3KSEpOzNRVs7RSquRQUFOBCGnmlOTk6CkoeqTk5+Qrl+UU5KYpKmtZctdaAAAAA//9QSwcILzRMjVUAAABYAAAAUEsBAhQDFAAAAAAAcKNfTQAAAAAAAAAAAAAAAAkAFQAAAAAAAAAAQP1BAAAAAF9fTUFDT1NYL1VYCAC0n9lbtJ/ZW1VUBQABtJ/ZW1BLAQIUAxQACAAIAAyjX00OCcksZgAAALAAAAATABUAAAAAAAAAAECkgTwAAABfX01BQ09TWC8uX2luZGV4LmpzVVgIALCf2Vv4ntlbVVQFAAH4ntlbUEsBAhQAFAAIAAgAAAAAAC80TI1VAAAAWAAAAAgAAAAAAAAAAAAAAAAA+AAAAGluZGV4LmpzUEsFBgAAAAADAAMA2AAAAIMBAAAAAA=="}' -H 'X-easyfaas-Account-Id: df391b08c64c426a81645468c75163a5' -H 'Content-Type: application/json; charset=utf-8'
  2. 请求示例
  3. POST /v1/functions/testHelloWorld HTTP/1.1
  4. Content-Type: application/json; charset=utf-8
  5. Host: 172.22.170.33:8002
  6. Connection: close
  7. User-Agent: Paw/3.1.10 (Macintosh; OS X/10.15.4) GCDHTTPRequest
  8. Content-Length: 990
  9. {"Version":"1","Description":"stubs create","Runtime":"nodejs10","Timeout":5,"MemorySize":128,"Handler":"index.handler","PodConcurrentQuota":10,"Code":"UEsDBBQAAAAAAHCjX00AAAAAAAAAAAAAAAAJABUAX19NQUNPU1gvVVgIALSf2Vu0n9lbVVQFAAG0n9lbUEsDBBQACAAIAAyjX00AAAAAAAAAAAAAAAATABUAX19NQUNPU1gvLl9pbmRleC5qc1VYCACwn9lb+J7ZW1VUBQAB+J7ZW2JgFWNnYGJg8E1MVvAPVohQgAKQGAMnAwODEQMDQx0DA5i/gYEo4BgSEgRlgnQsYGBgEEBTwogQl0rOz9VLLCjISdXLSSwuKS1OTUlJLElVDggGKXw772Y0iO5J8tAH0YAAAAD//1BLBwgOCcksZgAAALAAAABQSwMEFAAIAAgAAAAAAAAAAAAAAAAAAAAAAAgAAABpbmRleC5qc0qtKMgvKinWy0jMS8lJLVKwVdBILUvNK9FRSM7PK0mtADESc3KSEpOzNRVs7RSquRQUFOBCGnmlOTk6CkoeqTk5+Qrl+UU5KYpKmtZctdaAAAAA//9QSwcILzRMjVUAAABYAAAAUEsBAhQDFAAAAAAAcKNfTQAAAAAAAAAAAAAAAAkAFQAAAAAAAAAAQP1BAAAAAF9fTUFDT1NYL1VYCAC0n9lbtJ/ZW1VUBQABtJ/ZW1BLAQIUAxQACAAIAAyjX00OCcksZgAAALAAAAATABUAAAAAAAAAAECkgTwAAABfX01BQ09TWC8uX2luZGV4LmpzVVgIALCf2Vv4ntlbVVQFAAH4ntlbUEsBAhQAFAAIAAgAAAAAAC80TI1VAAAAWAAAAAgAAAAAAAAAAAAAAAAA+AAAAGluZGV4LmpzUEsFBgAAAAADAAMA2AAAAIMBAAAAAA=="}
  10. 响应结果:
  11. HTTP/1.1 200 OK
  12. Server: fasthttp
  13. Date: Fri, 11 Dec 2020 03:07:50 GMT
  14. Content-Length: 0
  15. Connection: close

2. 查看函数

请求示例

  1. $ curl -X GET "http://<YOUR_IP>:<YOUR_FUNC_REGISTRY_PORT>/v1/functions/brn:cloud:faas:bj:8f6e5a28c663957ea04522547a66d08f:function:testHelloWorld:1" -H 'X-easyfaas-Account-Id: df391b08c64c426a81645468c75163a5' -H 'Content-Type: application/json; charset=utf-8'
  2. GET /v1/functions/testHelloWorld HTTP/1.1
  3. Host: 127.0.0.1:8002
  4. Connection: close

响应结果

  1. {
  2. "Code": {
  3. "_": {},
  4. "Location": "/var/faas/funcData/brn:cloud:faas:bj:8f6e5a28c663957ea04522547a66d08f:function:testHelloWorld:1/code.zip",
  5. "RepositoryType": "filesystem",
  6. "LogType": ""
  7. },
  8. "Concurrency": {
  9. "_": {},
  10. "ReservedConcurrentExecutions": null,
  11. "AccountReservedSum": 0
  12. },
  13. "Configuration": {
  14. "_": {},
  15. "CodeSha256": "VoLwl2uaH/cF0hsvKQMkLbbd7JKtGgG5MNExk8whT+M=",
  16. "CodeSize": 625,
  17. "DeadLetterConfig": null,
  18. "Description": "stubs create",
  19. "Environment": {
  20. "_": {},
  21. "Error": null,
  22. "Variables": null
  23. },
  24. "FunctionArn": "brn:cloud:faas:bj:8f6e5a28c663957ea04522547a66d08f:function:testHelloWorld:1",
  25. "FunctionName": "testHelloWorld",
  26. "Handler": "index.handler",
  27. "KMSKeyArn": null,
  28. "LastModified": "2021-02-28T11:11:07Z",
  29. "Layers": null,
  30. "MasterArn": null,
  31. "MemorySize": 128,
  32. "RevisionId": null,
  33. "Role": null,
  34. "Runtime": "nodejs10",
  35. "Timeout": 5,
  36. "TracingConfig": null,
  37. "Version": "1",
  38. "VpcConfig": null,
  39. "CommitId": "349dd8a4-db7d-4fc3-a4cd-8c8b482e00c3",
  40. "Uid": "df391b08c64c426a81645468c75163a5",
  41. "PodConcurrentQuota": 10
  42. },
  43. "LogConfig": {
  44. "LogType": "",
  45. "BosDir": "",
  46. "Params": ""
  47. },
  48. "Tags": {}
  49. }

3. 运行函数

向controller发起函数调用请求

  1. $ curl -X "POST" "http://<YOUR_IP>:<YOUR_CONTROLLER_PORT>/v1/functions/brn:cloud:faas:bj:8f6e5a28c663957ea04522547a66d08f:function:testHelloWorld:1/invocations" -H 'X-easyfaas-Account-Id: df391b08c64c426a81645468c75163a5' -H 'Content-Type: application/json; charset=utf-8' -d $'{}'
  2. 请求示例
  3. POST /v1/functions/brn:bce:cfc:bj:cd64f99c69d7c404b61de0a4f1865834:function:testHSF2:1/invocations HTTP/1.1
  4. Content-Type: application/json; charset=utf-8
  5. X-easyfaas-Account-Id: df391b08c64c426a81645468c75163a5
  6. Host: 127.0.0.1:8001
  7. Connection: close
  8. 响应示例:
  9. Hello world!