尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
openYuanrong开发指南——函数服务
openYuanrong 官网官网gitcode仓库仓库函数服务openYuanrong 提供了函数服务能力支持 openYuanrong 函数以 Serverless 服务方式运行使用 HTTP 请求访问。服务实例随请求并发量全自动弹性伸缩无请求时缩容到 0。函数服务定义了 Handler 方法作为请求处理入口其签名如下。Python 样例handler方法名称可自定义。event函数服务的请求参数包含请求头请求体等数据格式为 JSON 对象。context由 openYuanrong 运行时提供的上下文信息接口介绍详见函数服务 Python SDK。defhandler(event,context)C 样例handler方法名称可自定义。event函数服务的请求参数包含请求头请求体等数据格式为 string。context由 openYuanrong 运行时提供的上下文信息接口介绍详见函数服务 C SDK。std::stringHandleRequest(conststd::stringevent,Function::Contextcontext)Java 样例handler方法名称可自定义。event函数服务的请求参数包含请求头请求体等数据格式为 JSON 对象。context由 openYuanrong 运行时提供的上下文信息接口介绍详见函数服务 Java SDK 。publicStringhandler(JsonObjectevent,Contextcontext)Handler 方法返回值为字符串以下是一个完整的方法示例Python 样例importdatetime# 服务执行入口每次请求都会执行defhandler(event,context):print(received request,event content:,event)responsetry:nameevent.get(name)# 获取配置的环境变量环境变量在注册和更新函数时设置show_datecontext.getUserData(show_date)ifshow_dateisnotNone:responsehello name,today is datetime.date.today().strftime(%Y-%m-%d)else:responsehello nameexceptExceptionase:print(e)responseplease enter your name,for example:{name:yuanrong}returnresponseC 样例#includestring#includectime#includenlohmann/json.hpp#includeRuntime.h#includeFunction.h#includeyr/yr.hstd::stringHandleRequest(conststd::stringevent,Function::Contextcontext){std::coutreceived request,event content:eventstd::endl;std::string response;try{nlohmann::json jsonDatanlohmann::json::parse(event);// 读取 JSON 数据并输出std::string namejsonData[name];responsehello ;responsename;std::string showDatecontext.GetUserData(show_date);if(showDate!){time_t nowtime(0);tm*ltmlocaltime(now);std::stringstream timeStr;timeStrltm-tm_year1900-;timeStrltm-tm_mon1-;timeStrltm-tm_mday;response,today is ;responsetimeStr.str();}}catch(conststd::exceptione){std::coutJSON parsing error:e.what()std::endl;responseplease enter your name,for example:{name:yuanrong};}returnresponse;}intmain(intargc,char*argv[]){Function::Runtime rt;// 同时需要在 main() 函数中注册该方法rt.RegisterHandler(HandleRequest);rt.Start(argc,argv);return0;}Java样例importcom.services.runtime.Context;importcom.google.gson.JsonObject;importjava.time.LocalDate;publicStringhandler(JsonObjectevent,Contextcontext){System.out.println(received request,event content:event);Stringresponse;try{Stringnameevent.get(name).getAsString();// 获取配置的环境变量环境变量在注册和更新函数时设置StringshowDatecontext.getUserData(show_date);if(showDate!null){responsehello name,today is LocalDate.now();}else{responsehello name;}}catch(Exceptione){e.printStackTrace();responseplease enter your name,for example:{name:yuanrong};}returnresponse;}查看部署 openYuanrong 服务类应用了解如何部署函数服务。函数生命周期回调在函数实例生命周期事件发生时可以触发相应的回调方法包括 Initializer 和 PreStop。回调方法可根据实际业务需要选择是否实现。Initializer 回调初始化Initializer 回调方法在函数实例启动之后请求处理方法Handler之前执行。在函数实例生命周期内成功且只成功执行一次。如果初始化方法执行失败发送到该函数实例的请求将直接返回失败实例自动被系统回收。初始化方法可用于处理后端建链等逻辑在配置单个函数实例可处理多个并发的场景请求间可复用链路避免重复建链降低处理时延。Initializer 回调方法的签名如下Python 样例initializer方法名称可自定义。context由 openYuanrong 运行时提供的上下文信息接口介绍详见函数服务 Python SDK。definitializer(context)C样例Initializer方法名称可自定义。context由 openYuanrong 运行时提供的上下文信息接口介绍详见函数服务 C SDK。voidInitializer(Function::Contextcontext)Java样例initializer方法名称可自定义。context由 openYuanrong 运行时提供的上下文信息接口介绍详见函数服务 Java SDK。publicvoidinitializer(Contextcontext)Initializer 方法无返回值一个简单的示例如下Python样例definitializer(context):print(function instance initialization completed)C样例std::stringHandleRequest(conststd::stringevent,Function::Contextcontext){returnok;}voidInitializer(Function::Contextcontext){std::coutfunction instance initialization completedstd::endl;return;}intmain(intargc,char*argv[]){Function::Runtime rt;rt.RegisterHandler(HandleRequest);// 同时需要在 main() 函数中注册该方法rt.RegisterInitializerFunction(Initializer);rt.Start(argc,argv);return0;}Java样例publicvoidinitializer(Contextcontext){System.out.println(function instance initialization completed);}PreStop 回调预停止PreStop回调方法在函数实例退出前执行可用于断开链路保存持久化数据等操作。PreStop 回调方法的签名如下Python样例pre_stop方法名称可自定义。defpre_stop()C 样例PreStop方法名称可自定义。voidPreStop(Function::Contextcontext)Java样例preStop方法名称可自定义。publicvoidpreStop(Contextcontext)PreStop 方法无返回值一个简单的示例如下Python样例defpre_stop():print(function instance is being destroyed)C样例std::stringHandleRequest(conststd::stringevent,Function::Contextcontext){returnok;}voidPreStop(Function::Contextcontext){std::coutfunction instance is being destroyedstd::endl;}intmain(intargc,char*argv[]){Function::Runtime rt;rt.RegisterHandler(HandleRequest);// 同时需要在 main() 函数中注册该方法rt.RegisterPreStopFunction(PreStop);rt.Start(argc,argv);return0;}Java样例publicvoidpreStop(Contextcontext){System.out.println(function instance is being destroyed);}函数日志函数的各方法往标准输出 stdout 打印的日志会被 openYuanrong 收集存储你可以使用以下方式打印日志。使用 openYuanRong 的日志记录器您可通过上下文方法getLogger()打印日志将获得和 openYuanrong 组件一样的输出格式。每条日志中都包含时间、请求 ID 和日志级别等信息。一个简单的示例如下。Python 样例defhandler(event,context):context.getLogger().info(hello world)returnokC样例std::stringHandlerRequest(conststd::stringevent,Function::Contextcontext){Function::FunctionLogger loggercontext.GetLogger();logger.setLevel(INFO);logger.Info(hello world);returnok;}intmain(intargc,char*argv[]){Function::Runtime rt;// 同时需要在 main() 函数中注册该方法rt.RegisterHandler(HandleRequest);rt.Start(argc,argv);return0;}Java 样例publicStringhandler(JsonObjectevent,Contextcontext){context.getLogger().info(hello world);returnok;}运行函数预期输出的日志内容如下。2025-xx-xx xx:xx:xx xxxxxxxx-xxxx-xxxx-xxxx-xxxxx****xx[INFO]hello world使用编程语言的日志输出函数使用编程语言日志输出函数打印的日志内容将原样输出到日志文件中。一个简单的示例如下。Python 样例defhandler(event,context):print(hello world)returnokC 样例std::stringHandlerRequest(){std::couthello worldstd::endl;returnok;}intmain(intargc,char*argv[]){Function::Runtime rt;// 同时需要在 main() 函数中注册该方法rt.RegisterHandler(HandleRequest);rt.Start(argc,argv);return0;}Java 样例publicStringhandler(){System.out.println(hello world);returnok;}运行服务预期输出的日志内容如下。hello world实例弹性策略openYuanrong 支持基于并发度的弹性策略当并发度达到配置的单实例并发度时触发函数实例扩容。函数实例 1 分钟内无请求处理时触发缩容函数实例可缩容到 0。实例调度openYuanrong 会根据函数指定的资源量和配置的调度策略选择合适节点运行它。详情请参阅调度章节。请求调度openYuanrong 支持为函数配置不同的请求调度策略包括 concurrency、 round-robin 和 microservice 。concurrency调度策略即为根据当前实例的并发度情况完成调度请求会被优先调度到并发度较低的实例上。round-robin调度策略即轮询调度请求会被轮流分配给不同的实例。microservice调度策略代表您没有为单个函数指定调度策略具体使用什么调度策略将在 openYuanrong 集群部署时指定。更多使用方式配置函数版本管理
RELATED

相关推荐

Path of Building:流放之路离线构建规划器的深度技术解析

Path of Building:流放之路离线构建规划器的深度技术解析

Path of Building:流放之路离线构建规划器的深度技术解析 【免费下载链接】PathOfBuilding Offline build planner for Path of Exile. 项目地址: https://gitcode.com/GitHub_Trending/pa/PathOfBuilding Path of Building(简称PoB)是…

📅 2026/9/17 12:59:13
ChatGPT | 科研写作加速器:从论文评审到优雅回复的智能Prompt指南

ChatGPT | 科研写作加速器:从论文评审到优雅回复的智能Prompt指南

1. ChatGPT如何成为科研写作的智能助手 第一次用ChatGPT处理论文评审意见时,我盯着屏幕上那句"Results lack statistical significance"发呆了半小时。直到把审稿人意见粘贴进对话框,看着AI逐条生成专业回复框架,才意识到这可能是学…

📅 2026/9/17 15:17:44
SolidWorks到URDF转换插件终极指南:3步实现机器人模型快速导出

SolidWorks到URDF转换插件终极指南:3步实现机器人模型快速导出

SolidWorks到URDF转换插件终极指南:3步实现机器人模型快速导出 【免费下载链接】solidworks_urdf_exporter SolidWorks to URDF Exporter 项目地址: https://gitcode.com/gh_mirrors/so/solidworks_urdf_exporter 你是否正在寻找一种高效的方式将SolidWorks机…

📅 2026/9/18 15:48:18
MORE NEWS

更多资讯

📰

汽车软件工程师ASPICE实战指南:核心流程、产物清单与避坑技巧

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

CoffeeScript 1.9.3 发布详解:REPL 错误修复、bare 模式 Source Map 优化与词法细节改进

编程语言编译器 【免费下载链接】coffeescript Unfancy JavaScript 项目地址: https://gitcode.com/gh_mirrors/co/coffeescript 点击查看 免费下载 导读 CoffeeScript 1.9.3 于 2015 年 5 月 27 日发布,是 1.9.x 系列的一个 bugfix 版本。本篇文章基于…

📰

从 0 到 60 FPS:用 LiveTalking 搭建实时交互数字人服务的完整指南

从 0 到 60 FPS:用 LiveTalking 搭建实时交互数字人服务的完整指南 【免费下载链接】metahuman-stream Real time interactive streaming digital human 项目地址: https://gitcode.com/GitHub_Trending/me/metahuman-stream LiveTalking 是一个开源的实时交…

📰

SkyReels-V2 AI视频生成入门指南:无限长度视频快速上手教程

SkyReels-V2 AI视频生成入门指南:无限长度视频快速上手教程 【免费下载链接】SkyReels-V2 SkyReels-V2: Infinite-length Film Generative model 项目地址: https://gitcode.com/GitHub_Trending/sk/SkyReels-V2 SkyReels-V2 是一个开源的 AI 视频生成模型&a…

📰

AltTab macOS 人工按键重复定时器(KeyRepeatTimer)的时序守卫设计:可见性锚点与迟到判定规则解析

AltTab macOS 人工按键重复定时器(KeyRepeatTimer)的时序守卫设计:可见性锚点与迟到判定规则解析 【免费下载链接】alt-tab-macos Windows alt-tab on macOS 项目地址: https://gitcode.com/gh_mirrors/al/alt-tab-macos 导读 AltTa…

📰

WSL2搭建RK3566开发环境实战:从串口调试到NPU加速

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬