函数计算 custom runtime 使用集锦

本文涉及的产品
简介: 函数计算 custom runtime 使用集锦

前言

该文章过期

直接参考官方文档, 比如 go

函数计算目前原生支持的开发语言有 nodejs, python, java, php 和 c#, 在实现这些开发语言 runtime 的时候, 函数计算开发团队花了很大的精力去让各自语言的传统应用能够简单快速迁移到函数计算平台:

如上述所列的各自语言的传统应用迁移到函数计算的迁移方案, 虽然已经足够简单, 但是还是需要去理解一下函数计算的接口以及各自语言在函数计算环境中运行起来的原理, 比如 python, 用户需要理解 WSGI 协议, 然后才编写一个符合要求的入口函数。 为了彻底解放生产力, Custom Runtime 应运而生, Custom Runitme 可以解决以下两个重要需求:

  • 可以随心所欲持定制个性化语言执行环境(例如 golang、lua、ruby)以及各种语言的小版本(例如python3.7、Nodejs12)等,打造属于自己的自定义runtime
  • 现有的 web 应用或基于传统开发 web 项目基本不用做任何改造,即可将项目一键迁移到函数计算平台

用户要实现一个最简单的 Custom runtime,只要符合以下两条:

  • 创建一个http server,监听在固定端口(端口可以读取环境变量 FC_SERVER_PORT,默认为 9000)
  • http server 需要在 15s 内完成启动

接下来, 我们梳理一下基于 Custom Runtime 一键迁移迁移案例。

custom 实现注意细节:

  • Custom Runtime 启动的服务一定监听 0.0.0.0:9000 或者 *:9000 端口,不用使用127.0.0.1:9000, 会导致请求超时。
    {"ErrorCode":"FunctionNotStarted","ErrorMessage":"The CA's http server cannot be started:ContainerStartDuration:25000000000. Ping CA failed due to: dial tcp 21.0.5.7:9000: getsockopt: connection refused Logs : 2019-11-29T09:53:30.859837462Z Listening on port 9000rn"}
  • Custom Runtime 的 bootstrap 一定需要添加 #!/bin/bash,不然会遇见如下错误
    {"ErrorCode":"CAExited","ErrorMessage":"The CA process either cannot be started or exited:ContainerStartDuration:25037266905. CA process cannot be started or exited already: rpc error: code = 106 desc = ContainerStartDuration:25000000000. Ping CA failed due to: dial tcp 21.0.7.2:9000: i/o timeout Logs : 2019-11-29T07:27:50.759658265Z panic: standard_init_linux.go:178: exec user process caused "exec format error"

使用 windows 一些文本编辑器要注意文件格式, 比如:

image

  • bootstrap 一定需要可执行权限
  • bootstrap 代码一定要执行到 http server 启动成功的逻辑, 不能被前面的逻辑阻塞, 比如启动server之前, 尝试连接一个不可达的数据库,造成启动时间 timeout
  • http server 的实现 connection keep alive, request timeout 至少10分钟以上

案例

java

Serverless 实战 —— 快速搭建 SpringBoot 应用
Serverless 实战 —— 移植 spring-petclinic 到函数计算
轻松搭建基于 SpringBoot + Vue 的 Web 商城应用

python

import tornado.ioloop
import tornado.web
import os

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        rid = self.request.headers.get('x-fc-request-id',None)
        print("FC Invoke Start RequestId: " + str(rid));
        
        # your logic
        self.write("GET: Hello world")
            
        print("FC Invoke End RequestId: " + str(rid));
     
    def post(self):
        rid = self.request.headers.get('x-fc-request-id',None)
        print("FC Invoke Start RequestId: " + str(rid));
        
        # your logic
        self.write("GET: Hello world")
        
        print("FC Invoke End RequestId: " + str(rid));

def make_app():
    return tornado.web.Application([
        (r"/.*", MainHandler),
    ])
 
if __name__ == "__main__":
    app = make_app()
    port = os.environ.get("FC_SERVER_PORT", "9000")
    app.listen(int(port))
    tornado.ioloop.IOLoop.current().start()

本地安装第三方包 tornado

然后编写一个具有可执行权限的名字为bootstrap (注:#!/bin/bash注释是必需的)文件启动上面代码的 http server:

#!/bin/bash
python server.py

go

基于custom runtime 打造 golang runtime

nodejs

'use strict';

var express = require('express');
var app = express();

var crypto = require('crypto');

app.post(/.*/, function (req, res) {
      var rid = req.headers["x-fc-request-id"];
      console.log(`FC Invoke Start RequestId: ${rid}`);
      
      // your logic, for example, get hash
      var secret = 'abcdefg';
      var hash = crypto.createHmac('sha256', secret)
          .update('I love cupcakes')
          .digest('hex');

      // c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
      console.log(hash);

      res.send(hash);

      console.log(`FC Invoke End RequestId: ${rid}`);
});

var port = process.env.FC_SERVER_PORT || 9000

app.listen(port, function () {
      console.log("FunctionCompute custom-nodejs runtime inited.");
});

app.timeout = 0; // never timeout
app.keepAliveTimeout = 0; // keepalive, never timeout

本地安装第三方包 express

然后编写一个具有可执行权限的名字为bootstrap (注:#!/bin/bash注释是必需的)文件启动上面代码的 http server:

#!/bin/bash
node server.js

php

<?php

define('FC_LOG_TAIL_START_PREFIX', 'FC Invoke Start RequestId: '); // Start of log tail mark

define('FC_LOG_TAIL_END_PREFIX', 'FC Invoke End RequestId: '); // End of log tail mark


$http = new swoole_http_server("0.0.0.0", 9000);

$options = [
    'worker_num' => 3,
];

$http->set($options);

$http->on("start", function ($server) {
    echo "FC customruntime Swoole http server is started at http://0.0.0.0:9000" . PHP_EOL;
});

$http->on('shutdown', function ($server){
    echo "FC customrutime Swoole http server shutdown" . PHP_EOL;
});

$http->on("request", function ($request, $response) {
    $rid = $request->header["x-fc-request-id"];
    echo FC_LOG_TAIL_START_PREFIX . $rid . PHP_EOL;
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World");
    echo FC_LOG_TAIL_END_PREFIX . $rid . PHP_EOL;
});

$http->start();

基于custom runtime + nginx + php-fpm 运行 wordpress
customruntime-php

.NETCORE CSharp

.Net Core 2.1 MVC Web应用迁移到函数计算 custom runtime

教程同样适用于 .netcore 3.0

相关实践学习
基于函数计算一键部署掌上游戏机
本场景介绍如何使用阿里云计算服务命令快速搭建一个掌上游戏机。
建立 Serverless 思维
本课程包括: Serverless 应用引擎的概念, 为开发者带来的实际价值, 以及让您了解常见的 Serverless 架构模式
目录
相关文章
|
运维 Kubernetes Cloud Native
serverless 学习 | QCon2022-深圳: Serverless 高密度部署与 Web-interoperable Runtime 在字节跳动的实践
serverless 学习 | QCon2022-深圳: Serverless 高密度部署与 Web-interoperable Runtime 在字节跳动的实践
349 0
serverless 学习 | QCon2022-深圳: Serverless 高密度部署与 Web-interoperable Runtime 在字节跳动的实践
|
Java Serverless
FC Java runtime 找不到函数入口问题
在函数计算中使用Java编程时,需要定义一个函数作为入口函数。由于Java有包的概念,因此执行方法和其他语言有所不同,需要带有包信息。在入口函数需要配置包信息
593 0
FC Java runtime 找不到函数入口问题
|
弹性计算 运维 监控
.Net Core 2.1 MVC Web应用迁移到函数计算 custom runtime
前言 Custom Runtime 就是自定义的执行环境, 用户基于 Custom Runtime 可以完成以下目标: 可以随心所欲持定制个性化语言执行环境(例如 golang、lua、ruby)以及各种语言的小版本(例如python3.7、Nodejs12)等,打造属于自己的自定义runtime; 现有的 web 应用或基于传统开发 web 项目基本不用做任何改造,即可将项目一键迁移到函数计算平台。
1454 0
|
Serverless Python 文件存储
函数计算搬站新利器---Custom Runtime演示
功能介绍 今天在函数计算上实现一个Web站点是比较简单的,比如: 部署基于 python wsgi web 框架的工程到函数计算 介绍的用WSGI框架结合函数计算部署Python的Web App。最近函数计算发布了一个新功能,使得搬站进一步简化了。
2871 0
|
Serverless API PHP
函数计算 php runtime 编译非内置的扩展
在本文中,以 `php-gd` 扩展为例, 我们讲解如果利用fcli 工具编译函数计算php runtime 中其他没有内置的扩展
3295 0
|
2月前
|
人工智能 数据管理 Serverless
阿里云数据库走向Serverless与AI驱动的一站式数据平台具有重大意义和潜力
阿里云数据库走向Serverless与AI驱动的一站式数据平台具有重大意义和潜力
404 2
|
2月前
|
人工智能 运维 Cloud Native
、你如何看待阿里云数据库走向Serverless与AI驱动的一站式数据平台?
、你如何看待阿里云数据库走向Serverless与AI驱动的一站式数据平台?
149 2
|
2月前
|
人工智能 数据管理 大数据
阿里云数据库走向Serverless与AI驱动的一站式数据平台是一个很有前景和意义的发展方向
阿里云数据库走向Serverless与AI驱动的一站式数据平台是一个很有前景和意义的发展方向
33 2
|
2天前
|
消息中间件 运维 Serverless
阿里云函数计算是一种FaaS(Function as a Service)云服务
【4月更文挑战第17天】阿里云函数计算是一种FaaS(Function as a Service)云服务
12 3

相关产品

  • 函数计算