开发函数计算的正确姿势 —— 使用 Fun Init 初始化项目

news/2024/5/20 10:03:21 标签: git, 运维, serverless

首先介绍下在本文出现的几个比较重要的概念:

函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。函数计算更多信息 参考。
Fun: Fun 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算、API 网关、日志服务等资源。它通过一个资源配置文件(template.yml),协助您进行开发、构建、部署操作。Fun 的更多文档 参考。
2.0 版本的 Fun,在部署这一块做了很多努力,并提供了比较完善的功能,能够做到将云资源方便、平滑地部署到云端。但该版本,在本地开发上的体验,还有较多的工作要做。于是,我们决定推出 Fun Init 弥补这一处短板。
Fun Init: Fun Init 作为 Fun 的一个子命令存在,只要 Fun 的版本大于等于 2.7.0,即可以直接通过 fun init 命令使用。Fun Init 工具可以根据指定的模板快速的创建函数计算应用,快速体验和开发函数计算相关业务。官方会提供常用的模板,用户也可以自定自己的模板。

快速体验

hellworld-nodejs8 模板目录结构

helloworld-nodejs8          ① 模板项目根目录
├── metadata.json           ② 模板项目配置文件
└── {{ projectName }}       ③ 模板根目录
    ├── index.js
    └── template.yml

执行命令:fun init -n xxx helloworld-nodejs8

$ fun init -n xxx helloworld-nodejs8
Start rendering template...
+ /Users/kevin/xxx
+ /Users/kevin/xxx/index.js
+ /Users/kevin/xxx/template.yml
finish rendering template.

初始化生成的项目目录结构:

xxx
├── index.js
└── template.yml

Fun Init 命令格式

$ fun init --help

  Usage: init [options] [location]

  Initializes a new fun project

  Options:

    -o, --output-dir [outputDir]  where to output the initialized app into (default: .)
    -n, --name [name]             name of your project to be generated as a folder (default: fun-app)
    --no-input [noInput]          disable prompting and accept default values defined template config
    -V, --var [vars]              template variable
    -h, --help                    output usage information

  Examples:

    $ fun init
    $ fun init helloworld-nodejs8
    $ fun init foo/bar
    $ fun init gh:foo/bar
    $ fun init gl:foo/bar
    $ fun init bb:foo/bar
    $ fun init github:foo/bar
    $ fun init gitlab:foo/bar
    $ fun init bitbucket:foo/bar
    $ fun init git+ssh://git@github.com/foo/bar.git
    $ fun init hg+ssh://hg@bitbucket.org/bar/foo
    $ fun init git@github.com:foo/bar.git
    $ fun init https://github.com/foo/bar.git
    $ fun init /path/foo/bar
    $ fun init -n fun-app -V foo=bar /path/foo/bar

选项说明

选项默认值描述
-o, --output-dir.初始化的应用程序输出目录(可选)
-n, --namefun-app要作为文件夹生成的项目名称(可选)
--no-inputfalse禁用提示并接受默认值来定义的模板配置(可选)
-V, --var模板变量(可选)
-h, --help打印使用说明(可选)

示例:

  1. -o,--output-dir 选项
    输出初始化应用程序的输出目录。默认是 fun init 命令执行的当前路径。
$ fun init -o /path/foo/bar
  1. -n,--name 选项
    要作为文件夹生成的项目名称。默认值是 fun-app
$ fun init -n xxx
  1. -V,--var 选项
    模板变量。模板可能会有很多模板变量,模板变量一般都会设置默认值,如果某些模板变量希望用户填写自己的值,在初始化模板的过程中,会提示用户输入自定义的值,用户可以输入自定义的值,也可以直接回车使用默认值,还可以直接通过 -V,--var 选项设置模板变量值。通过该选项设置的模板变量,就不会再提示用户输入了。

选项格式要求:以等号分隔的键值对,键只能包含字母、数字和 _;值可以是任意字符。

$ fun init -V foo=bar -V bar=baz
  1. --no-input 选项
    禁用提示并接受默认值来定义的模板配置。假如不想提示输入模板变量,可以通过该选项,跳过提示输入,直接使用模板变量的默认值。
$ fun init --no-input

模板位置(Location)

支持如下方式指定模板:

  • 官方模板。包含离线和在线模板:

    • 离线模板。内嵌在 fun 工具中
    • 在线模板。通过模板名称来指定模板位置,fun 内部会将其转换为 github 地址
  • Git / Mercurial 仓库模板。支持缩写方式,具体请看下文
  • 本地文件系统路径模板

如果您需要自定义自己的模板,只需要将您的模板提交到 github 上,然后在执行 fun init 指定您自己的 github 地址。具体请参考:Fun Init 自定义模板

示例:

  1. 选择官方模板
    输入 fun init 命令,则提示用户选择模板,支持模糊查询。在不清楚自己需要什么样的模板时,简单的使用不带任何参数的 fun init 会提示用户官方模板有哪些,用户更具提示选择自己的需要模板。
$ fun init
? Select a tempalte to init (Use arrow keys or type to search)
 helloworld-nodejs8
  helloworld-nodejs6
  helloworld-python3
  helloworld-python2.7
  helloworld-java8
  helloworld-php7.2
  1. 指定官方模板
    helloworld-nodejs8 是内嵌在 fun 工具的官方模板,所以支持离线,该模板最终生成一个运行时为 nodejs8 的 helloworld 函数计算应用。对于一些常用的模板,我们会把它放到官方模板列表中,用户只需要指定简短模板名称即可,不需要指定完整的模板仓库地址。
$ fun init helloworld-nodejs8
  1. 指定 Git / Mercurial 仓库模板
    支持 Git / Mercurial 仓库模板,支持多种前缀的缩写形式,对于 github,甚至可以进一步缩写为 user/repo
$ fun init foo/bar
$ fun init gh:foo/bar
$ fun init gl:foo/bar
$ fun init bb:foo/bar
$ fun init github:foo/bar
$ fun init gitlab:foo/bar
$ fun init bitbucket:foo/bar
$ fun init git+ssh://git@github.com/foo/bar.git
$ fun init hg+ssh://hg@bitbucket.org/bar/foo
$ fun init git@github.com:foo/bar.git
$ fun init https://github.com/foo/bar.git

说明:需要提前安装好相应的版本控制工具。

  1. 指定本地文件系统路径模板
    我们可以将在线模板克隆放到本地文件系统,通过指定模板路径,实现离线初始化。
$ fun init /path/foo/bar

小结

通过 Fun Init 工具可以快速体验和开发函数计算的应用,避免重复机械劳动,你也可以将自己觉得有价值的函数计算应用制作成模板项目,方便自己同时也可以方便别人。

相关文章

  • Fun Init 自定义模板
  • 开发函数计算的正确姿势 —— 使用 Fun Local 本地运行与调试
  • 开发函数计算的正确姿势 —— 爬虫
  • 开发函数计算的正确姿势 —— 排查超时问题
  • Fun Repo
  • Fun specs
  • Fun examples
  • Fun 发布 2.0 新版本啦

http://www.niftyadmin.cn/n/1660578.html

相关文章

wxPython TextCtrl类

https://www.cnblogs.com/ankier/archive/2012/09/17/2689364.html wx.TextCtrl的构造函数 wx.TextCtrl(parent,id,value,poswx.DefaultPostion, sizewx.DefaultSize,style0,validatorwx.DefaultValidator,namewx.TextCtrlNameStr) ------------------------------------------…

Java B2B2C多用户电子商务平台SpringCloud/Boot

2019独角兽企业重金招聘Python工程师标准>>> 用java实施的电子商务平台太少了,使用spring cloud技术构建的b2b2c电子商务平台更少,大型企业分布式互联网电子商务平台,推出PC微信APP云服务的云商平台系统,其中包括B2B、…

ActiveMq报错Channel was inactive for too (30000)long

生成环境的activemq 隔一到两周,就报错: 查看 activeme的日志: 2018-12-04 11:59:44,744 | WARN | Transport Connection to: tcp://127.0.0.1:63807 failed: org.apache.activemq.transport.InactivityIOException: Channel was inactive f…

流量隔离方案 Dpath 护航双十一新零售

摘要:本文作者: 思邪,阿里巴巴中间件技术专家,全程参与2018 双11的技术支撑工作,长期关注RPC及微服务技术领域,擅长系统架构和性能优化。需求在今年的双11准备期间,业务同学提出要针对新零售进行…

gitlab改root密码

1. ~$ sudo gitlab-rails console production 2.查询要改的用户 irb(main):008:0> u User.where(id: 1).first > #<User id:1 root> 3.重置密码 irb(main):009:0> u.password 12345678 4.确认密码 irb(main):009:0> u.password_confirmation 12345678 5.保…

Apache Kylin v2.5.2 正式发布

2019独角兽企业重金招聘Python工程师标准>>> Apache Kylin社区于日前宣布&#xff1a;Apache Kylin v2.5.2 正式发布&#xff01;欢迎大家下载使用。 Apache Kylin 是一个开源的分布式分析引擎&#xff0c;提供 Hadoop 之上的 SQL 查询接口及多维分析&#xff08;OL…

重装windows系统后配置Anaconda

给电脑换了系统&#xff0c;十分担心anaconda需要重装。还好以下方法完美解决。&#xff08;同是win10 64位&#xff09; 原始anaconda安装路径&#xff1a;D:\ProgramData\Anaconda3 &#xff08;不能有空格哦&#xff09; 一、添加环境变量 D:\ProgramData\Anaconda3 &#…

docker命令合集

文章目录 docker 镜像基础命令docker容器基础命令常用镜像启动命令及常用命令gitlab及runnergitlab启动命令gitlab-runner启动命令gitlab-runner注册命令 nacosmysql8postgresqlnginxspring boot docker 镜像基础命令 # 查看当前镜像列表 docker images # 查询镜像 docker sea…