作者说:Serverless 架构下的应用开发,与传统架构的应用开发还是有比较大的区别点的,例如天然分布式架构会让很多框架丧失一定的 "便利性",无状态的特点又让很多 "传统架构下看起来再正常不过的操作" 变得异常风险。
所以本篇我会介绍一些在 Serverless 架构下,常见的应用开发注意事项,分享一些个人的实战经验心得。如果你在 Serverless 开发过程中遇到问题,不妨往下听听看吧。
关于应用开发的七个心得
1、如何上传文件
f = request.files['file']
f.save('my_file_path')
一些云平台的 API 网关触发器会将二进制文件转换成字符串;不便直接获取和存储;
此外,API 网关与 FaaS 平台之间传递的数据包有大小限制,很多平台被限制在 6M;
FaaS 平台大都是无状态的,即使存储到当前实例中,也会随着实例释放而导致文件丢失;
一种是 BASE64 后上传,持久化到对象存储或者是 NAS 中,这种做法可能会触及到 API 网关与 FaaS 平台之间传递的数据包有大小限制,所以一般使用这种上传方法的通常是上传头像等小文件的业务场景; 第二种上传方法是,通过对象存储等平台来上传,因为客户端直接通过密钥等信息,来将文件直传到对象存储是有一定风险的。所以通常情况是客户端发起上传请求,函数计算根据请求内容进行预签名操作,并将预签名地址返回给客户端,客户端再使用指定的方法进行上传,上传完成之后,可以通过对象存储触发器等来对上传结果进行更新,详情如下图所示:
2、文件读写与持久化方法
3、慎用部分 Web 框架的特性
4、要注意应用组成结构
5、传统框架迁移方案与策略
传统框架迁移案例
# index.pyp
import bottle
@bottle.route('/hello/<name>')
def index(name):
return "Hello world"
if __name__ == '__main__':
bottle.run(host='localhost', port=8080, debug=True)
# index.py
import bottle
@bottle.route('/hello/<name>')
def index(name):
return "Hello world"
app = bottle.default_app()
if __name__ == '__main__':
bottle.run(host='localhost', port=8080, debug=True)
# index.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(
host="0.0.0.0",
port=int("8001")
)
#!/usr/bin/env bash
export PORT=9000
npm run star
通过开发者工具快速迁移/部署
详情可以参考: https://github.com/devsapp/start-web-framework
s init start-express
_____
| ___|
| |____ ___ __ _ __ ___ ___ ___
| __\ \/ / '_ \| '__/ _ \/ __/ __|
| |___> <| |_) | | | __/\__ \__ \
\____/_/\_\ .__/|_| \___||___/___/
| |
|_|
? please select credential alias default
Welcome to the start-express application
This application requires to open these services:
FC : https://fc.console.aliyun.com/
Express development docs: https://www.expressjs.com.cn/4x/api.html
* 额外说明:s.yaml中声明了actions:
部署前执行:npm install --production
如果遇到npm命令找不到等问题,可以适当进行手动项目构建,并根据需要取消actions内容
* 项目初始化完成,您可以直接进入项目目录下,并使用 s deploy 进行项目部署
🏄 Thanks for using Serverless-Devs
👉 You could [cd /Users/jiangyu/Desktop/fc-custom-lua-event/image-prediction-app/start-express] and enjoy your serverless journey!
🧭️ If you need help for this example, you can use [s -h] after you enter folder.
💞 Document ❤ Star:https://github.com/Serverless-Devs/Serverless-Devs
$ s deploy
framework:
region: cn-beijing
service:
name: web-framework
function:
name: express
runtime: custom
handler: index.handler
memorySize: 128
timeout: 60
url:
system_url: https://1583208943291465.cn-beijing.fc.aliyuncs.com/2016-08-15/proxy/web-framework/express/
custom_domain:
-
domain: http://express.web-framework.1583208943291465.cn-beijing.fc.devsapp.net
triggers:
-
type: http
name: httpTrigger
#!/bin/bash
node index.js
# ------------------------------------
# 欢迎您使用阿里云函数计算 FC 组件进行项目开发
# 组件仓库地址/帮助文档:https://github.com/devsapp/fc
# Yaml参考文档:https://github.com/devsapp/fc/blob/jiangyu-docs/docs/zh/yaml.md
# 关于:
# - Serverless Devs和FC组件的关系、如何声明/部署多个函数、超过50M的代码包如何部署
# - 关于.fcignore使用方法、工具中.s目录是做什么、函数进行build操作之后如何处理build的产物
# 等问题,可以参考文档:https://github.com/devsapp/fc/blob/jiangyu-docs/docs/zh/tips.md
# 关于如何做CICD等问题,可以参考:https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/cicd.md
# 有问题快来钉钉群问一下吧:33947367
# ------------------------------------
edition: 1.0.0 # 命令行YAML规范版本,遵循语义化版本(Semantic Versioning)规范
name: framework # 项目名称
access: "default" # 秘钥别名
services:
framework: # 业务名称/模块名称
component: fc # 组件名称
actions:
pre-deploy: # 在deploy之前运行
- run: npm install --production # 要运行的命令行
path: ./code # 命令行运行的路径
props: # 组件的属性值
region: cn-beijing
service:
name: web-framework
description: 'Serverless Devs Web Framework Service'
function:
name: express
description: 'Serverless Devs Web Framework Express Function'
codeUri: './code'
runtime: custom
timeout: 60
caPort: 9000
triggers:
- name: httpTrigger
type: http
config:
authType: anonymous
methods:
- GET
customDomains:
- domainName: auto
protocol: HTTP
routeConfigs:
- path: '/*'
6、可观测性
通过工具进行 Metrics 查看
详情可以参考: https://github.com/devsapp/fc/blob/main/docs/zh/command/metrics.md
有资源描述文件(Yaml)时,可以直接执行 s metrics 查看函数的指标信息; 纯命令行形式(在没有资源描述 Yaml 文件时),需要指定服务所在地区以及服务名称,函数名等(例如 sclifcmetrics--regionch-hangzhou--service-namemyService--function-namemyFunction)
[2021-06-07T12:20:06.661] [INFO ] [FC-METRICS] - 请用浏览器访问Uri地址进行查看: http://localhost:3000
通过工具进行 Logs 查看
详情可以参考: https://github.com/devsapp/fc/blob/main/docs/zh/command/logs.md
有资源描述文件(Yaml)时,可以直接执行 s logs 进行线上函数的日志查询; 纯命令行形式(在没有资源描述 Yaml 文件时),需要指定服务所在地区以及服务名称,函数名等(例如 s cli fc logs --region cn-hangzhou --service-name fc-deploy-service --function-name http-trigger-py36)
FunctionCompute python3 runtime inited.
FC Invoke Start RequestId: 84d6ae81-02ff-4011-b3ca-45e65b210cc3
FC Invoke End RequestId: 84d6ae81-02ff-4011-b3ca-45e65b210cc3
FC Invoke Start RequestId: de4812be-9137-4a33-9869-370cb61ac427
FC Invoke End RequestId: de4812be-9137-4a33-9869-370cb61ac427
7、如何对应用进行调试
结语
统计时间截至 4月30日 18:00