基本原理

更新时间: 2023-04-12 16:57:03

针对Custom Runtime,您的代码文件ZIP包是一个HTTP Server程序。本文介绍冷启动Custom Runtime的基本原理和HTTP Server配置要求。

基本原理

针对Custom Runtime,您的代码文件ZIP包是一个HTTP Server程序,您只需设置函数配置中的启动命令启动参数完成HTTP Server的启动。函数计算冷启动Custom Runtime时,会调用您设置的启动命令启动参数启动您自定义的HTTP Server,该HTTP Server接管了来自函数计算的所有请求。HTTP Server的默认端口是9000,如果您的HTTP Server是其他端口,例如8080,您可以设置函数配置中的监听端口为8080。

例如,函数的程序包名称为function.zip,该运行时各个语言程序包内的文件形式和相应的启动命令启动参数示例如下。

Java 8或Spring Boot

.
├── demo.jar


customRuntimeConfig:
 command:
 - java 
 args:
 - '-jar'
 - 'demo.jar'	

Python 3.7

.
├── server.py


customRuntimeConfig:
 command:
 - python
 args:
 - 'server.py'

Node.js 10

.
├── server.js


customRuntimeConfig:
 command:
 - node
 args:
 - 'server.js'

PHP 7.4

.
├── server.php


customRuntimeConfig:
 command:
 - php
 args:
 - 'server.php'
说明

customRuntimeConfig是函数的自定义启动命令配置。其中command是容器的入口命令列表,args是启动参数列表。函数计算依次把commandargs列表中的内容进行拼接,形成完整的启动命令。如果未配置启动命令及启动参数,HTTP Server将默认从/code/bootstrap启动。

HTTP Server配置要求

创建HTTP Server时您需要满足以下要求:

  • Custom Runtime启动的服务一定要监听0.0.0.0:CAPort*:CAPort端口。如果您使用127.0.0.1:CAPort端口,会导致请求超时,出现以下错误:

    {
    "ErrorCode":"FunctionNotStarted",
    "ErrorMessage":"TheCA'shttpservercannotbestarted:ContainerStartDuration:25000000000.PingCAfaileddueto:dialtcp21.0.XX.XX:9000:getsockopt:connectionrefusedLogs:2019-11-29T09:53:30.859837462ZListeningonport9000"
    }

    Custom Runtime的监听端口,即函数属性监听端口默认是9000。如果Custom Runtime使用默认的监听端口,那么您实现的Custom Runtime的HTTP Server监听的端口也必须是9000。如果Custom Runtime使用的监听端口是8080,那么您实现的Custom Runtime的HTTP Server监听的端口也必须是8080。

  • Connection需要设置为Keep-Alive,Server端请求超时时间需设置在24小时(函数最大运行时间)及以上。示例如下:

    //例如Node.js使用express时。  
    
    var server = app.listen(PORT, HOST);
    server.timeout = 0; // never timeout
    server.keepAliveTimeout = 0; // keepalive, never timeout
  • HTTP Server需要在120秒内启动完毕。

阿里云首页 函数计算(旧版) 相关技术圈