Serverless 微服务实践-移动应用包分发服务
背景
阿里云函數(shù)計(jì)算是事件驅(qū)動(dòng)的全托管計(jì)算服務(wù)。通過函數(shù)計(jì)算,您無需管理服務(wù)器等基礎(chǔ)設(shè)施,只需編寫代碼并上傳。函數(shù)計(jì)算會(huì)為您準(zhǔn)備好計(jì)算資源,以彈性、可靠的方式運(yùn)行您的代碼,并提供日志查詢、性能監(jiān)控、報(bào)警等功能。借助于函數(shù)計(jì)算,您可以快速構(gòu)建任何類型的應(yīng)用和服務(wù),無需管理和運(yùn)維。而且,您只需要為代碼實(shí)際運(yùn)行所消耗的資源付費(fèi),代碼未運(yùn)行則不產(chǎn)生費(fèi)用。
移動(dòng)應(yīng)用的打包和分發(fā)呈現(xiàn)明顯的峰谷效用,用戶常常需要短時(shí)間內(nèi)準(zhǔn)備大量資源保障分發(fā)的實(shí)時(shí)性,完成分發(fā)后又需要及時(shí)釋放資源,降低成本。這里我們提供一個(gè) [fun](https://statistics.functioncompute.com/?title=Serverless 微服務(wù)實(shí)踐-移動(dòng)應(yīng)用包分發(fā)服務(wù)(Fun 3.0 升級(jí)版)&author=孫飛宇&src=&url=https://github.com/alibaba/funcraft) 模板,幫助我們更快地搭建一個(gè)基于[函數(shù)計(jì)算](https://statistics.functioncompute.com/?title=Serverless 微服務(wù)實(shí)踐-移動(dòng)應(yīng)用包分發(fā)服務(wù)(Fun 3.0 升級(jí)版)&author=孫飛宇&src=&url=https://helpcdn.aliyun.com/product/50980.html)構(gòu)建 Serverless 架構(gòu)的包分發(fā)服務(wù),在開發(fā)運(yùn)維效率,性能和成本間取得良好的平衡。
在分包過程中,下載/修改/上傳是一個(gè)比較消耗資源的任務(wù),需要消耗大量的計(jì)算/網(wǎng)絡(luò)資源。并且分包任務(wù)只在應(yīng)用發(fā)布新版本時(shí)才會(huì)發(fā)生,需要在盡可能短的時(shí)間內(nèi)完成。針對(duì)這種有明顯波峰波谷的場景,非常適合使用函數(shù)計(jì)算來完成。更重要的是這個(gè)服務(wù)是具有彈性伸縮和高可用能力的。
apk 分包簡介
更多參考[函數(shù)計(jì)算–Serverless 微服務(wù)實(shí)踐-移動(dòng)應(yīng)用包分發(fā)服務(wù)](https://statistics.functioncompute.com/?title=Serverless 微服務(wù)實(shí)踐-移動(dòng)應(yīng)用包分發(fā)服務(wù)(Fun 3.0 升級(jí)版)&author=孫飛宇&src=&url=https://yq.aliyun.com/articles/699972)。
準(zhǔn)備工作:
1.安裝 node
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash nvm install 82.安裝 fun 工具
npm install @alicloud/fun -gfun 工具的某些子命令可能會(huì)用到 docker,所以你需要安裝好 docker,具體參考文檔:[Fun 安裝教程](https://statistics.functioncompute.com/?title=Serverless 微服務(wù)實(shí)踐-移動(dòng)應(yīng)用包分發(fā)服務(wù)(Fun 3.0 升級(jí)版)&author=孫飛宇&src=&url=https://github.com/aliyun/fun/blob/master/docs/usage/installation-zh.md)。
3.apk 包準(zhǔn)備
在這個(gè)實(shí)驗(yàn)中,我們會(huì)使用一個(gè)示例的 apk 包,可以從這里下載 qq-v2.apk。
下載 qq-v2.apk ,上傳到自己的 oss bucket中:
快速開始:
1.通過 fun 模板生成項(xiàng)目骨架
使用 fun init 命令可以快捷的將本模板項(xiàng)目初始化到本地,執(zhí)行命令 :
$ fun init -n apk https://github.com/coco-super/package-distribution-service-for-serverless start cloning... Cloning into '.fun-init-cache-2fc2d680-eeff-11e9-a930-6fd4d1ac6506'... remote: Enumerating objects: 23, done. remote: Counting objects: 100% (23/23), done. remote: Compressing objects: 100% (16/16), done. remote: Total 23 (delta 0), reused 18 (delta 0), pack-reused 0 Unpacking objects: 100% (23/23), done. finish clone. ? Please input a oss bucket name? sunfeiyu Start rendering template.../Users/ellison/simple-fc-uncompress-service-for-oss/apk/Users/ellison/simple-fc-uncompress-service-for-oss/apk/.funignore/Users/ellison/simple-fc-uncompress-service-for-oss/apk/pom.xml/Users/ellison/simple-fc-uncompress-service-for-oss/apk/src/Users/ellison/simple-fc-uncompress-service-for-oss/apk/src/main/Users/ellison/simple-fc-uncompress-service-for-oss/apk/src/main/java/Users/ellison/simple-fc-uncompress-service-for-oss/apk/src/main/java/example/Users/ellison/simple-fc-uncompress-service-for-oss/apk/src/main/java/example/App.java/Users/ellison/simple-fc-uncompress-service-for-oss/apk/target/Users/ellison/simple-fc-uncompress-service-for-oss/apk/target/classes/Users/ellison/simple-fc-uncompress-service-for-oss/apk/target/classes/example/Users/ellison/simple-fc-uncompress-service-for-oss/apk/target/classes/example/App.class/Users/ellison/simple-fc-uncompress-service-for-oss/apk/template.yml finish rendering template.其中 -n 表示要作為文件夾生成的項(xiàng)目名稱。默認(rèn)值是 fun-app。更多fun init 命令格式選項(xiàng)說明請(qǐng)參考云棲文章[開發(fā)函數(shù)計(jì)算的正確姿勢 —— 使用 Fun Init 初始化項(xiàng)目](https://statistics.functioncompute.com/?title=Serverless 微服務(wù)實(shí)踐-移動(dòng)應(yīng)用包分發(fā)服務(wù)(Fun 3.0 升級(jí)版)&author=孫飛宇&src=&url=https://yq.aliyun.com/articles/674363)。
執(zhí)行 fun init后會(huì)提示:輸入一個(gè) oss 的 bucket,注意 oss Bucket 是全球唯一的,如已經(jīng)被占用了,請(qǐng)換一個(gè)新的名稱或者一個(gè)已經(jīng)創(chuàng)建好的(已經(jīng)創(chuàng)建好的,請(qǐng)確保 region 一致)。
ps: 輸入的內(nèi)容和 apk 包準(zhǔn)備中提到的 oss bucket name 是同一個(gè),本示例為:sunfeiyu
4. 編譯
在模版項(xiàng)目的根目錄下執(zhí)行 fun build 命令編譯:
$ fun build using template: template.yml start building function dependencies without dockerbuilding apk/apk running task flow MavenTaskFlow running task: MavenCompileTask running task: MavenCopyDependencies running task: CopyMavenArtifactsBuild SuccessBuilt artifacts: .fun/build/artifacts Built template: .fun/build/artifacts/template.ymlTips for next step ====================== * Invoke Event Function: fun local invoke * Invoke Http Function: fun local start * Deploy Resources: fun deploy關(guān)于 Fun build 構(gòu)建函數(shù)詳細(xì)請(qǐng)參照云棲文章 [開發(fā)函數(shù)計(jì)算的正確姿勢 —— 使用 Fun Build 構(gòu)建函數(shù)](https://statistics.functioncompute.com/?title=Serverless 微服務(wù)實(shí)踐-移動(dòng)應(yīng)用包分發(fā)服務(wù)(Fun 3.0 升級(jí)版)&author=孫飛宇&src=&url=https://yq.aliyun.com/articles/719102)。
5.服務(wù)部署
在模版項(xiàng)目的的根目錄下執(zhí)行 fun deploy 部署到云端。
$ fun deploy using template: .fun/build/artifacts/template.yml using region: cn-shanghai using accountId: ***********8320 using accessKeyId: ***********mTN4 using timeout: 60Waiting for service apk to be deployed...make sure role 'aliyunfcgeneratedrole-cn-shanghai-apk' is existrole 'aliyunfcgeneratedrole-cn-shanghai-apk' is already existattaching policies AliyunOSSFullAccess to role: aliyunfcgeneratedrole-cn-shanghai-apkattached policies AliyunOSSFullAccess to role: aliyunfcgeneratedrole-cn-shanghai-apkWaiting for function apk to be deployed...Waiting for packaging function apk code...The function apk has been packaged. A total of 15 files files were compressed and the final size was 3.13 MBfunction apk deploy success service apk deploy success6.執(zhí)行函數(shù)
提供兩種方式:
查看結(jié)果
登陸 oss 查看已經(jīng)生成新的 apk 包:
將 oss 中 qq-v2-signed.apk 下載到本地。
查看渠道信息是否寫入:
~/Downloads ? $ java -jar /Users/ellison/Downloads/walle-cli-all.jar show qq-v2-signed.apk /Users/ellison/Downloads/qq-v2-signed.apk : {channel=aliyun-fc}渠道信息 {channel=aliyun-fc} 已寫入,成功!
參考閱讀
“阿里巴巴云原生關(guān)注微服務(wù)、Serverless、容器、Service Mesh 等技術(shù)領(lǐng)域、聚焦云原生流行技術(shù)趨勢、云原生大規(guī)模的落地實(shí)踐,做最懂云原生開發(fā)者的技術(shù)圈。”
總結(jié)
以上是生活随笔為你收集整理的Serverless 微服务实践-移动应用包分发服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在一起 10 年了,这封信你一定要收下
- 下一篇: 2020 云原生技术 7 大领域趋势全预