
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 集群部署时指定。更多使用方式配置函数版本管理