尧图网络 高端网站定制 · 原创设计
免费咨询热线
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/10 3:39:53
ChatGPT | 科研写作加速器:从论文评审到优雅回复的智能Prompt指南

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

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

📅 2026/9/16 8:04:42
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/9 17:30:33
MORE NEWS

更多资讯

📰

STM32F407老人健康监测系统:心率跌倒检测与嵌入式医疗级设计

简介:本资源是一套面向嵌入式开发初学者与老年健康设备项目实践者的完整智能硬件方案,聚焦于STM32F4平台的老人健康监测智能手表设计,解决跌倒预警、生命体征实时采集与远程监护等实际需求,适用于课程设计、毕业设计及IoT健康终端…

📰

闪存多通道并发如何压垮DDR带宽?建模与实测指南

1. 项目概述:为什么闪存多通道并发会把DDR压得喘不过气?你手里的SSD、UFS模组、甚至车载eMMC,只要标着“多通道”“高性能”“顺序读写3GB/s”,背后一定藏着一个被反复拷问的问题:它到底在多大程度上吃掉了DDR的带宽&a…

📰

Amino-PEG37-COOH在生物偶联中的应用与操作指南

1. 项目概述:Amino-PEG37-COOH的结构特性Amino-PEG37-COOH(NH2-PEG37-COOH)是一种具有明确分子结构的聚乙二醇(PEG)衍生物,其名称中的数字37代表乙二醇重复单元的数量。这种化合物同时具备末端的氨基&#…

📰

STM32两路步进电机控制:定时器脉冲、双轴联动与S型加减速

简介:这是一份基于STM32的双路步进电机控制工程,面向嵌入式初学者与自动化项目开发者,解决两路电机速度调节、方向切换和停止控制等常见问题。程序以定时器PWM输出为控制核心,初始化、速度设置、方向控制、中断处理与多任务调度等…

📰

使用 Litestar 组装完整 TODO 应用:路由处理器、参数注入与 ASGI 启动实战

使用 Litestar 组装完整 TODO 应用:路由处理器、参数注入与 ASGI 启动实战 【免费下载链接】litestar Light, flexible and extensible ASGI framework | Built to scale 项目地址: https://gitcode.com/GitHub_Trending/li/litestar 本篇技术指南基于 Lites…

📰

使用 AWS CLI 的 codebuild batch-get-reports 批量获取 CodeBuild 测试与覆盖率报告详情

使用 AWS CLI 的 codebuild batch-get-reports 批量获取 CodeBuild 测试与覆盖率报告详情 【免费下载链接】aws-cli Universal Command Line Interface for Amazon Web Services 项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli 导读 本文围绕 AWS CLI 中 a…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

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

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

📞 💬