函数计算怎么调用函数-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

函数计算怎么调用函数

boxti 2017-10-19 16:03:18 3411


调用类型


您可以采用两种同步和异步两种方式调用函数。
  • 同步调用:事件被函数处理后再将结果返回。
  • 异步调用:事件在写入到消息队列后则返回。函数计算系统会保证该消息被可靠的处理。
  • 同步/异步请求对传入的事件有不同的限制,具体请参考限制项文档

您可以通过控制台/命令行工具的方式,手动调用函数,具体请参考快速入门中的 相关示例;也可通过 REST API 的方式调用函数,具体请参考 相关文档。我们也提供了各种语言的 SDK,进一步简化您的操作。下例是 Java SDK 的调用示例:
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]FcSample[backcolor=transparent] [backcolor=transparent]{
  2. [backcolor=transparent]    [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]final[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] CODE_DIR [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"/tmp/fc_code"[backcolor=transparent];
  3. [backcolor=transparent]    [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]final[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] REGION [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"cn-shanghai"[backcolor=transparent];
  4. [backcolor=transparent]    [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]final[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] SERVICE_NAME [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"test_service"[backcolor=transparent];
  5. [backcolor=transparent]    [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]final[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] FUNCTION_NAME [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"test_function"[backcolor=transparent];
  6. [backcolor=transparent]    [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] main[backcolor=transparent]([backcolor=transparent]final[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent][][backcolor=transparent] args[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]throws[backcolor=transparent] [backcolor=transparent]IOException[backcolor=transparent] [backcolor=transparent]{
  7. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] accessKey [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]getenv[backcolor=transparent]([backcolor=transparent]"ACCESS_KEY"[backcolor=transparent]);
  8. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] accessSecretKey [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]getenv[backcolor=transparent]([backcolor=transparent]"SECRET_KEY"[backcolor=transparent]);
  9. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] accountId [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]getenv[backcolor=transparent]([backcolor=transparent]"ACCOUNT_ID"[backcolor=transparent]);
  10. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] role [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]getenv[backcolor=transparent]([backcolor=transparent]"ROLE"[backcolor=transparent]);
  11. [backcolor=transparent]        [backcolor=transparent]// Initialize FC client
  12. [backcolor=transparent]        [backcolor=transparent]FunctionComputeClient[backcolor=transparent] fcClient [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]FunctionComputeClient[backcolor=transparent]([backcolor=transparent]REGION[backcolor=transparent],[backcolor=transparent] accountId[backcolor=transparent],[backcolor=transparent] accessKey[backcolor=transparent],[backcolor=transparent] accessSecretKey[backcolor=transparent]);
  13. [backcolor=transparent]        [backcolor=transparent]// Create a service
  14. [backcolor=transparent]        [backcolor=transparent]CreateServiceRequest[backcolor=transparent] csReq [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]CreateServiceRequest[backcolor=transparent]();
  15. [backcolor=transparent]        csReq[backcolor=transparent].[backcolor=transparent]setServiceName[backcolor=transparent]([backcolor=transparent]SERVICE_NAME[backcolor=transparent]);
  16. [backcolor=transparent]        csReq[backcolor=transparent].[backcolor=transparent]setDescription[backcolor=transparent]([backcolor=transparent]"FC test service"[backcolor=transparent]);
  17. [backcolor=transparent]        csReq[backcolor=transparent].[backcolor=transparent]setRole[backcolor=transparent]([backcolor=transparent]role[backcolor=transparent]);
  18. [backcolor=transparent]        [backcolor=transparent]CreateServiceResponse[backcolor=transparent] csResp [backcolor=transparent]=[backcolor=transparent] fcClient[backcolor=transparent].[backcolor=transparent]createService[backcolor=transparent]([backcolor=transparent]csReq[backcolor=transparent]);
  19. [backcolor=transparent]        [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"Created service, request ID "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] csResp[backcolor=transparent].[backcolor=transparent]getRequestId[backcolor=transparent]());
  20. [backcolor=transparent]        [backcolor=transparent]// Create a function
  21. [backcolor=transparent]        [backcolor=transparent]CreateFunctionRequest[backcolor=transparent] cfReq [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]CreateFunctionRequest[backcolor=transparent]([backcolor=transparent]SERVICE_NAME[backcolor=transparent]);
  22. [backcolor=transparent]        cfReq[backcolor=transparent].[backcolor=transparent]setFunctionName[backcolor=transparent]([backcolor=transparent]FUNCTION_NAME[backcolor=transparent]);
  23. [backcolor=transparent]        cfReq[backcolor=transparent].[backcolor=transparent]setDescription[backcolor=transparent]([backcolor=transparent]"Function for test"[backcolor=transparent]);
  24. [backcolor=transparent]        cfReq[backcolor=transparent].[backcolor=transparent]setMemorySize[backcolor=transparent]([backcolor=transparent]128[backcolor=transparent]);
  25. [backcolor=transparent]        cfReq[backcolor=transparent].[backcolor=transparent]setHandler[backcolor=transparent]([backcolor=transparent]"hello_world.handler"[backcolor=transparent]);
  26. [backcolor=transparent]        cfReq[backcolor=transparent].[backcolor=transparent]setRuntime[backcolor=transparent]([backcolor=transparent]"nodejs6"[backcolor=transparent]);
  27. [backcolor=transparent]        [backcolor=transparent]Code[backcolor=transparent] code [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Code[backcolor=transparent]().[backcolor=transparent]setDir[backcolor=transparent]([backcolor=transparent]CODE_DIR[backcolor=transparent]);
  28. [backcolor=transparent]        cfReq[backcolor=transparent].[backcolor=transparent]setCode[backcolor=transparent]([backcolor=transparent]code[backcolor=transparent]);
  29. [backcolor=transparent]        cfReq[backcolor=transparent].[backcolor=transparent]setTimeout[backcolor=transparent]([backcolor=transparent]10[backcolor=transparent]);
  30. [backcolor=transparent]        [backcolor=transparent]CreateFunctionResponse[backcolor=transparent] cfResp [backcolor=transparent]=[backcolor=transparent] fcClient[backcolor=transparent].[backcolor=transparent]createFunction[backcolor=transparent]([backcolor=transparent]cfReq[backcolor=transparent]);
  31. [backcolor=transparent]        [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"Created function, request ID "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] cfResp[backcolor=transparent].[backcolor=transparent]getRequestId[backcolor=transparent]());
  32. [backcolor=transparent]        [backcolor=transparent]// Invoke the function with a string as function event parameter, Sync mode
  33. [backcolor=transparent]        [backcolor=transparent]InvokeFunctionRequest[backcolor=transparent] invkReq [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]InvokeFunctionRequest[backcolor=transparent]([backcolor=transparent]SERVICE_NAME[backcolor=transparent],[backcolor=transparent] FUNCTION_NAME[backcolor=transparent]);
  34. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] payload [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"Hello FunctionCompute!"
  35. [backcolor=transparent]        invkReq[backcolor=transparent].[backcolor=transparent]setPayload[backcolor=transparent]([backcolor=transparent]payload[backcolor=transparent].[backcolor=transparent]getBytes[backcolor=transparent]())
  36. [backcolor=transparent]        [backcolor=transparent]InvokeFunctionResponse[backcolor=transparent] invkResp [backcolor=transparent]=[backcolor=transparent] fcClient[backcolor=transparent].[backcolor=transparent]invokeFunction[backcolor=transparent]([backcolor=transparent]invkReq[backcolor=transparent]);
  37. [backcolor=transparent]        [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]([backcolor=transparent]invkResp[backcolor=transparent].[backcolor=transparent]getContent[backcolor=transparent]()));
  38. [backcolor=transparent]        [backcolor=transparent]// Invoke the function, Async mode
  39. [backcolor=transparent]        invkReq[backcolor=transparent].[backcolor=transparent]setInvocationType[backcolor=transparent]([backcolor=transparent]Const[backcolor=transparent].[backcolor=transparent]INVOCATION_TYPE_ASYNC[backcolor=transparent]);
  40. [backcolor=transparent]        invkResp [backcolor=transparent]=[backcolor=transparent] fcClient[backcolor=transparent].[backcolor=transparent]invokeFunction[backcolor=transparent]([backcolor=transparent]invkReq[backcolor=transparent]);
  41. [backcolor=transparent]        [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]HttpURLConnection[backcolor=transparent].[backcolor=transparent]HTTP_ACCEPTED [backcolor=transparent]==[backcolor=transparent] invkResp[backcolor=transparent].[backcolor=transparent]getStatus[backcolor=transparent]())[backcolor=transparent] [backcolor=transparent]{
  42. [backcolor=transparent]            [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"Async invocation has been queued for execution, request ID: "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] invkResp[backcolor=transparent].[backcolor=transparent]getRequestId[backcolor=transparent]());
  43. [backcolor=transparent]        [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
  44. [backcolor=transparent]            [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"Async invocation was not accepted"[backcolor=transparent]);
  45. [backcolor=transparent]        [backcolor=transparent]}
  46. [backcolor=transparent]        [backcolor=transparent]// Delete the function
  47. [backcolor=transparent]        [backcolor=transparent]DeleteFunctionRequest[backcolor=transparent] dfReq [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]DeleteFunctionRequest[backcolor=transparent]([backcolor=transparent]SERVICE_NAME[backcolor=transparent],[backcolor=transparent] FUNCTION_NAME[backcolor=transparent]);
  48. [backcolor=transparent]        [backcolor=transparent]DeleteFunctionResponse[backcolor=transparent] dfResp [backcolor=transparent]=[backcolor=transparent] fcClient[backcolor=transparent].[backcolor=transparent]deleteFunction[backcolor=transparent]([backcolor=transparent]dfReq[backcolor=transparent]);
  49. [backcolor=transparent]        [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"Deleted function, request ID "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] dfResp[backcolor=transparent].[backcolor=transparent]getRequestId[backcolor=transparent]());
  50. [backcolor=transparent]        [backcolor=transparent]// Delete the service
  51. [backcolor=transparent]        [backcolor=transparent]DeleteServiceRequest[backcolor=transparent] dsReq [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]DeleteServiceRequest[backcolor=transparent]([backcolor=transparent]SERVICE_NAME[backcolor=transparent]);
  52. [backcolor=transparent]        [backcolor=transparent]DeleteServiceResponse[backcolor=transparent] dsResp [backcolor=transparent]=[backcolor=transparent] fcClient[backcolor=transparent].[backcolor=transparent]deleteService[backcolor=transparent]([backcolor=transparent]dsReq[backcolor=transparent]);
  53. [backcolor=transparent]        [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"Deleted service, request ID "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] dsResp[backcolor=transparent].[backcolor=transparent]getRequestId[backcolor=transparent]());
  54. [backcolor=transparent]    [backcolor=transparent]}
  55. [backcolor=transparent]}


并发执行


并发执行是指在任意指定时间您的函数代码同时执行的数量。您可以用下述公式来估算并发的函数调用数。
请求速率 * 函数执行时间
其中,请求速率是指函数被调用的速率,单位为“每秒请求数/事件数”。例如,考虑一个处理阿里云 OSS 事件的函数。假定函数平均用时 3 秒,OSS 每秒发布 10 个事件。因此,您的函数有 30 个并发执行。

安全限制


在某些情况下,由于用户的错误设置,函数执行可能会失控。考虑如下场景,您设置了 OSS 触发器,当图片文件上传到 OSS 上的 foo bucket 后,会调用相关函数处理。该函数将原图片调整为3张不同分辨率的图片,并将结果错误地写回到了 foo bucket 下。因此又会造成新的函数被调用,无限循环下去。为了避免因为失控函数导致财务损失,函数计算在每个账户粒度设置了并发执行限制,默认为100。您可以通过函数在 云监控中的相关指标(throttles)来观察流控行为。如果您需要提高该限制,请提交工单。

流控错误处理


根据调用方式,流控错误的处理方式有所不同。

  • 同步调用:发出调用的应用程序会收到 429 错误,并将负责执行重试操作。如果您通过 API 网关调用函数,您需要确保将函数计算的响应错误映射到 API 网关的错误代码上。如果您直接调用该函数(例如使用函数计算的 SDK 或者 CLI),则您的客户端将会收到 429 错误,您可以根据实际需求选择是否重试。

  • 异步调用:如果您的函数被异步调用并被流控,函数计算会自动重试受限制的事件,最长六个小时,并在重试之间有一定的延迟。在使用异步事件来调用函数之前,会将它们排队。
消息中间件 编解码 监控 安全 Java Serverless API 开发工具 对象存储 网络架构
分享到
取消 提交回答
全部回答(1)
  • 小生1111
    2018-04-10 18:14:33
    Re函数计算怎么调用函数
    乱码了
    。。。。。
    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程