初窥FaaS
在云计算不断发展中,软件服务化趋势越加明显,用户通过网络即可使用应用提供的服务,服务慢慢变成应用构建基础,成为云产品的基本形态。FaaS(Functionas a Service)以函数为单元提供服务,符合云发展的趋势,并且作为一种新型计算方式成为了云计算未来发展的一个方向。FaaS 的出现使用户专心于编写和上传核心的业务代码,由FaaS负责创建和维护相应的计算、存储、网络等资源。用户完成编写并上传代码之后,运行即可获得相应的数据结果或服务。以阿里云函数计算FC为代表的FaaS服务的出现降低了运维的成本,使用户更专注于业务代码,实现高效工作,让业务发展节奏加快。
抛砖引玉
放出个实现订阅游戏降价提醒服务的小栗子,测试阿里云serverless函数计算FC的服务情况:(数据来源参考:https://steamdb.info/)
背景知识
SMTP
SMTP 是一种提供可靠且有效的电子邮件传输的协议。SMTP 是建立在 FTP 文件传输服务上的一种邮件服务,主要用于系统之间的邮件信息传递,并提供有关来信的通知。SMTP 独立于特定的传输子系统,且只需要可靠有序的数据流信道支持,SMTP 的重要特性之一是其能跨越网络传输邮件,即“SMTP 邮件中继”。使用 SMTP,可实现相同网络处理进程之间的邮件传输,也可通过中继器或网关实现某处理进程与其他网络之间的邮件传输。
SMTP 协议的工作原理可分为如下3个过程:
- 建立连接:在这一阶段,SMTP 客户请求与服务器的25端口建立一个 TCP 连接。一旦连接建立,SMTP 服务器和客户就开始相互通告自己的域名,同时确认对方的域名;
- 邮件传送:利用命令,SMTP 客户将邮件的源地址、目的地址和邮件的具体内容传递给 SMTP 服务器,SMTP 服务器进行相应的响应并接收邮件;
- 连接释放:SMTP 客户发出退出命令,服务器在处理命令后进行响应,随后关闭 TCP 连接。
API调用示例
https://store.steampowered.com/api/appdetails/?appids=1918680
{"1918680":{"success":true,"data":{"type":"game","name":"仙剑客栈 2","steam_appid":1918680,"required_age":0,"is_free":false,"detailed_description":"《仙剑客栈2》由大宇资讯有限公司开发。是一款集餐厅经营、角色扮演、恋爱养成…等诸多要素为一体的模拟经营角色扮演游戏,由仙剑系列的众多角色共同经营「逍遥客栈」,打造一个温馨浪漫大团圆的平行异时空世界。仙剑系列内的知名角色李逍遥、赵灵儿、林月如、酒剑仙、景天、云天河…以及众多仙剑人物将在这个客栈陆续登场重现……<h2 class=\"bb_tag\">招募伙计,建造属于自己的餐厅客栈<\/h2>仙剑历代系列作的知名角色,可招募成为客栈重要的伙计帮手,共同打造理想的家园客栈。<br><img src=\"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/extras\/Note01-2.gif?t=1657252021\" \/><h2 class=\"bb_tag\">钻研菜谱,提升美食佳肴的评价体验<\/h2>研究提升各式佳肴美食,获得每日来店客人的优良评价,提高客栈名声等级。<br><img src=\"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/extras\/Note02-2.gif?t=1657252021\" \/><h2 class=\"bb_tag\">菜单配置,排列组合最佳的主打菜单<\/h2>搭配安排每日菜单组合,温暖满足每一位来店的饕客味蕾。<br><img src=\"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/extras\/Note03-2.gif?t=1657252021\" \/><h2 class=\"bb_tag\">云游四海,探索访求传说的厨具食材<\/h2>游历探访世界各地,寻找提升客栈的经典厨具以及取得生长异地的特色食材。<br><img src=\"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/extras\/Note04-2.gif?t=1657252021\" \/>","about_the_game":"《仙剑客栈2》由大宇资讯有限公司开发。是一款集餐厅经营、角色扮演、恋爱养成…等诸多要素为一体的模拟经营角色扮演游戏,由仙剑系列的众多角色共同经营「逍遥客栈」,打造一个温馨浪漫大团圆的平行异时空世界。仙剑系列内的知名角色李逍遥、赵灵儿、林月如、酒剑仙、景天、云天河…以及众多仙剑人物将在这个客栈陆续登场重现……<h2 class=\"bb_tag\">招募伙计,建造属于自己的餐厅客栈<\/h2>仙剑历代系列作的知名角色,可招募成为客栈重要的伙计帮手,共同打造理想的家园客栈。<br><img src=\"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/extras\/Note01-2.gif?t=1657252021\" \/><h2 class=\"bb_tag\">钻研菜谱,提升美食佳肴的评价体验<\/h2>研究提升各式佳肴美食,获得每日来店客人的优良评价,提高客栈名声等级。<br><img src=\"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/extras\/Note02-2.gif?t=1657252021\" \/><h2 class=\"bb_tag\">菜单配置,排列组合最佳的主打菜单<\/h2>搭配安排每日菜单组合,温暖满足每一位来店的饕客味蕾。<br><img src=\"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/extras\/Note03-2.gif?t=1657252021\" \/><h2 class=\"bb_tag\">云游四海,探索访求传说的厨具食材<\/h2>游历探访世界各地,寻找提升客栈的经典厨具以及取得生长异地的特色食材。<br><img src=\"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/extras\/Note04-2.gif?t=1657252021\" \/>","short_description":"《仙剑客栈2》是一款集餐馆经营、角色扮演、恋爱养成等诸多要素为一体的经营模拟游戏,由仙剑系列的众多角色共同经营「逍遥客栈」,打造一个温馨浪漫大团圆的平行异时空世界。仙剑系列内的知名角色李逍遥、赵灵儿、林月如、酒剑仙、景天、云天河等以及众多仙剑人物将在这个时空陆续重现,一段段全新的爱情故事将在此上演。","supported_languages":"简体中文<strong>*<\/strong>, 繁体中文<strong>*<\/strong>, 英语<br><strong>*<\/strong>具有完全音频支持的语言","header_image":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/header_schinese.jpg?t=1657252021","website":null,"pc_requirements":{"minimum":"<strong>最低配置:<\/strong><br><ul class=\"bb_ul\"><li>需要 64 位处理器和操作系统<br><\/li><li><strong>操作系统:<\/strong> Windows 7,8,10 (64-bit)<br><\/li><li><strong>处理器:<\/strong> Intel Core i5 4460 或 AMD 等同性能处理器<br><\/li><li><strong>内存:<\/strong> 8 GB RAM<br><\/li><li><strong>显卡:<\/strong> NVIDIA GeForce GTX 760 或其他厂牌同性能显示适配器<br><\/li><li><strong>DirectX 版本:<\/strong> 11<br><\/li><li><strong>存储空间:<\/strong> 需要 5 GB 可用空间<br><\/li><li><strong>声卡:<\/strong> Direct Sound 兼容声卡<\/li><\/ul>","recommended":"<strong>推荐配置:<\/strong><br><ul class=\"bb_ul\"><li>需要 64 位处理器和操作系统<br><\/li><li><strong>操作系统:<\/strong> Windows 7,8,10 (64-bit)<br><\/li><li><strong>处理器:<\/strong> Intel Core i7 3770 或 AMD 等同性能处理器(含以上)<br><\/li><li><strong>内存:<\/strong> 16 GB RAM<br><\/li><li><strong>显卡:<\/strong> NVIDIA GeForce GTX 1060 或其他厂牌同性能显示适配器<br><\/li><li><strong>DirectX 版本:<\/strong> 11<br><\/li><li><strong>存储空间:<\/strong> 需要 5 GB 可用空间<br><\/li><li><strong>声卡:<\/strong> Direct Sound 兼容声卡<\/li><\/ul>"},"mac_requirements":{"minimum":"<strong>最低配置:<\/strong><br><ul class=\"bb_ul\"><li>需要 64 位处理器和操作系统<\/li><\/ul>","recommended":"<strong>推荐配置:<\/strong><br><ul class=\"bb_ul\"><li>需要 64 位处理器和操作系统<\/li><\/ul>"},"linux_requirements":{"minimum":"<strong>最低配置:<\/strong><br><ul class=\"bb_ul\"><li>需要 64 位处理器和操作系统<\/li><\/ul>","recommended":"<strong>推荐配置:<\/strong><br><ul class=\"bb_ul\"><li>需要 64 位处理器和操作系统<\/li><\/ul>"},"developers":["SOFTSTAR ENTERTAINMENT"],"publishers":["方块游戏(CubeGame)","SOFTSTAR ENTERTAINMENT"],"price_overview":{"currency":"HKD","initial":8900,"final":8010,"discount_percent":10,"initial_formatted":"HK$ 89.00","final_formatted":"HK$ 80.10"},"packages":[691097],"package_groups":[{"name":"default","title":"购买 仙剑客栈 2","description":"","selection_text":"选择一个购买选项","save_text":"","display_type":0,"is_recurring_subscription":"false","subs":[{"packageid":691097,"percent_savings_text":"-10% ","percent_savings":0,"option_text":"仙劍客棧 2 - <span class=\"discount_original_price\">HK$ 89.00<\/span> HK$ 80.10","option_description":"","can_get_free_license":"0","is_free_license":false,"price_in_cents_with_discount":8010}]}],"platforms":{"windows":true,"mac":false,"linux":false},"categories":[{"id":2,"description":"单人"},{"id":1,"description":"多人"},{"id":49,"description":"玩家对战"},{"id":37,"description":"同屏\/分屏玩家对战"},{"id":24,"description":"同屏\/分屏"},{"id":22,"description":"Steam 成就"},{"id":44,"description":"远程同乐"}],"genres":[{"id":"4","description":"休闲"},{"id":"3","description":"角色扮演"},{"id":"28","description":"模拟"}],"screenshots":[{"id":0,"path_thumbnail":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/ss_6e90db3be074ab9671687a47a15448951c484f46.600x338.jpg?t=1657252021","path_full":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/ss_6e90db3be074ab9671687a47a15448951c484f46.1920x1080.jpg?t=1657252021"},{"id":1,"path_thumbnail":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/ss_830e330b73b02a7c920b97e98c600b63e16b6360.600x338.jpg?t=1657252021","path_full":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/ss_830e330b73b02a7c920b97e98c600b63e16b6360.1920x1080.jpg?t=1657252021"},{"id":2,"path_thumbnail":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/ss_53e6c2094b4ab6142a85edacda859b5670491997.600x338.jpg?t=1657252021","path_full":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/ss_53e6c2094b4ab6142a85edacda859b5670491997.1920x1080.jpg?t=1657252021"},{"id":3,"path_thumbnail":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/ss_2f6a5accab05b553e654ef52d62923c71c234202.600x338.jpg?t=1657252021","path_full":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/ss_2f6a5accab05b553e654ef52d62923c71c234202.1920x1080.jpg?t=1657252021"},{"id":4,"path_thumbnail":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/ss_c33b044654bfc0eacb53f2baceb41303dd16f2ef.600x338.jpg?t=1657252021","path_full":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/ss_c33b044654bfc0eacb53f2baceb41303dd16f2ef.1920x1080.jpg?t=1657252021"}],"movies":[{"id":256880275,"name":"仙剑客栈2 宣傳片CN1","thumbnail":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/256880275\/movie.293x165.jpg?t=1649296666","webm":{"480":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/256880275\/movie480_vp9.webm?t=1649296666","max":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/256880275\/movie_max_vp9.webm?t=1649296666"},"mp4":{"480":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/256880275\/movie480.mp4?t=1649296666","max":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/256880275\/movie_max.mp4?t=1649296666"},"highlight":true}],"recommendations":{"total":260},"achievements":{"total":50,"highlighted":[{"name":"执子之手","path":"https:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/1918680\/fbbaacda2ffbc4cc207f049607f6c158cfc5dba0.jpg"},{"name":"归家","path":"https:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/1918680\/8cebfe283661b6450fc048d2f324bc72fec3b697.jpg"},{"name":"灯火阑珊","path":"https:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/1918680\/a170f99e1d1a6ca4e022b33163c2b8b339384e0f.jpg"},{"name":"师徒","path":"https:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/1918680\/7fb995e8071c3a9439a831b488f320bc33c25800.jpg"},{"name":"同行","path":"https:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/1918680\/1b970ae9b7539d637dc174ef4b54093324eafcb1.jpg"},{"name":"云游四海","path":"https:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/1918680\/58f83d1cb3619828a9bd0d5cd01bf092930f7401.jpg"},{"name":"归隐山林","path":"https:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/1918680\/c37bb2dd2910d849f1e3ee388a5e4edde0b92534.jpg"},{"name":"与子成说","path":"https:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/1918680\/ab41bc8301fd84882dea0e0638c989bf3801a856.jpg"},{"name":"连理","path":"https:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/1918680\/6262eb579ff65e9b22611fdb637521edc57188ca.jpg"},{"name":"欢喜冤家","path":"https:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/1918680\/fa3c2ae268a46e83b89213e6cef7943557ac21cf.jpg"}]},"release_date":{"coming_soon":false,"date":"2022 年 7 月 7 日"},"support_info":{"url":"http:\/\/www.softstar.com.tw\/CSCenter\/Trouble.aspx","email":""},"background":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/page_bg_generated_v6b.jpg?t=1657252021","background_raw":"https:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/1918680\/page_bg_generated.jpg?t=1657252021","content_descriptors":{"ids":[],"notes":null}}}}
函数部分
# # -*- coding: utf8 -*-importjson,requestsimportrequestsimportsmtplibfromemail.headerimportHeaderfromemail.mime.textimportMIMETextfromemail.mime.imageimportMIMEImagefromemail.mime.multipartimportMIMEMultipartIDs=[111111,222222]#这里是需要监听的游戏APPIDurl='https://store.steampowered.com/api/appdetails/?appids='defmain_handler(event, context):#这个是默认入口text_info=""isbuy=FalseforIDinIDs: ID=str(ID) res=requests.get(url+ID).json() ifres[ID]["success"] isTrue: price=res[ID]["data"]["price_overview"]["final"]/100rawprice=res[ID]["data"]["price_overview"]["initial"]/100ifrawprice>price: buy="正"isbuy=True#有一个降价了就通知,else: buy="未"print("["+res[ID]["data"]["name"]+"@"+ID+"]当前价格:%s,历史价格:%s;【%s】在促销"%(price,rawprice,buy)) text_info+="["+res[ID]["data"]["name"]+"@"+ID+"]当前价格:%s,历史价格:%s;【%s】在促销"%(price,rawprice,buy)+"\n\n"else: print("["+res[ID]["data"]["name"]+"@"+ID+"]读取失败!") ifisbuyisTrue: sender_mail='发送邮箱@qq.com'#填发件人邮箱地址sender_pass='rgmxxxxxxceg'#这里填邮箱SMTP授权码to='接收邮箱,可自发自收'msg_root=MIMEMultipart('mixed')# 设置总的邮件体对象,对象类型为mixedmsg_root['From'] ='云函数steam价格监听'#发送人描述msg_root['To'] =tosubject='Steam游戏监听价格变动!'#邮件标题msg_root['subject'] =Header(subject, 'utf-8') text_sub=MIMEText(text_info, 'plain', 'utf-8') msg_root.attach(text_sub) try: sftp_obj=smtplib.SMTP('smtp.qq.com', 25) sftp_obj.login(sender_mail, sender_pass) sftp_obj.sendmail(sender_mail, to, msg_root.as_string()) sftp_obj.quit() print('邮件推送成功!') exceptExceptionase: print('邮件推送失败!错误代码:') print(e) returntext_info
函数计算支持配置定时触发器(Time Trigger),可以在指定的时间点自动触发函数执行。适合本示例的批量数据的定时处理,我们可以设定个触发器,定时执行价格比对,如有降价则向邮箱推送消息,非常nice。
测试截图
心得体会
阿里云函数计算FC是我理想中的server less产品,大大的简化了开发流程。我们不仅可以结合云开发平台的在线IDE和DB资源实现更大规模的项目开发,还可以通过云效的流水线实现持续部署。体验下来发现作为开发的同学真的可以借助阿里云函数计算省下来很多事,从此以后甚至可以说只要写好业务逻辑代码往serverless产品上一扔就能测试上线运行!
意见与建议:
希望函数计算FC可以提供一些免费的公网资源;
实测阿里云函数计算FC暂不支持TypeScript的转码,希望能在编码支持上再接再厉;
跟云效平台结合体验很棒!请让更多人知道它!
目前跨平台迁移仍有很大的基础成本,希望未来能更方便的迁移,而不用费尽周折重构业务代码。