天天看點

Serverless 微服務實踐-移動應用包分發服務(Fun 3.0 更新版)

原文作者:孫飛宇

原文連結:

https://developer.aliyun.com/article/720914?spm=a2c6h.12873581.0.0.64cc115ejTPRfZ&groupCode=cloudnative

更多雲原生技術資訊可關注

阿裡巴巴雲原生技術圈

背景

阿裡雲函數計算是事件驅動的全托管計算服務。通過函數計算,您無需管理伺服器等基礎設施,隻需編寫代碼并上傳。函數計算會為您準備好計算資源,以彈性、可靠的方式運作您的代碼,并提供日志查詢、性能監控、報警等功能。借助于函數計算,您可以快速建構任何類型的應用和服務,無需管理和運維。而且,您隻需要為代碼實際運作所消耗的資源付費,代碼未運作則不産生費用。

移動應用的打包和分發呈現明顯的峰谷效用,使用者常常需要短時間内準備大量資源保障分發的實時性,完成分發後又需要及時釋放資源,降低成本。這裡我們提供一個

fun

模闆,幫助我們更快地搭建一個基于

函數計算

建構 Serverless 架構的包分發服務,在開發運維效率,性能和成本間取得良好的平衡。

在分包過程中,下載下傳/修改/上傳是一個比較消耗資源的任務,需要消耗大量的計算/網絡資源。并且分包任務隻在應用釋出新版本時才會發生,需要在盡可能短的時間内完成。針對這種有明顯波峰波谷的場景,非常适合使用函數計算來完成。更重要的是這個服務是具有彈性伸縮和高可用能力的。

apk 分包簡介

Serverless 微服務實踐-移動應用包分發服務(Fun 3.0 更新版)

更多參考

函數計算--Serverless 微服務實踐-移動應用包分發服務

準備工作:

1.安裝 node

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
nvm install 8           

2.安裝 fun 工具

npm install @alicloud/fun -g           

fun 工具的某些子指令可能會用到 docker,是以你需要安裝好 docker,具體參考文檔:

Fun 安裝教程

3.apk 包準備

在這個實驗中,我們會使用一個示例的 apk 包,可以從這裡下載下傳

qq-v2.apk

下載下傳

,上傳到自己的 oss bucket中:

Serverless 微服務實踐-移動應用包分發服務(Fun 3.0 更新版)

快速開始:

1.通過 fun 模闆生成項目骨架

使用

fun init

指令可以快捷的将本模闆項目初始化到本地,執行指令 :

$ 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 表示要作為檔案夾生成的項目名稱。預設值是 fun-app。更多fun init 指令格式選項說明請參考雲栖文章

開發函數計算的正确姿勢 —— 使用 Fun Init 初始化項目

執行

fun init

後會提示:輸入一個 oss 的 bucket,注意 oss Bucket 是全球唯一的,如已經被占用了,請換一個新的名稱或者一個已經建立好的(已經建立好的,請確定 region 一緻)。

ps: 輸入的内容和 apk 包準備中提到的 oss bucket name 是同一個,本示例為:

sunfeiyu

4. 編譯

在模版項目的根目錄下執行

fun build

指令編譯:

$ fun build 
using template: template.yml
start building function dependencies without docker
building apk/apk
running task flow MavenTaskFlow
running task: MavenCompileTask
running task: MavenCopyDependencies
running task: CopyMavenArtifacts
Build Success
Built artifacts: .fun/build/artifacts
Built template: .fun/build/artifacts/template.yml
Tips for next step
======================
* Invoke Event Function: fun local invoke
* Invoke Http Function: fun local start
* Deploy Resources: fun deploy           

關于 Fun build 建構函數詳細請參照雲栖文章

開發函數計算的正确姿勢 —— 使用 Fun Build 建構函數

5.服務部署

在模版項目的的根目錄下執行

fun deploy

部署到雲端。

$ fun deploy
using template: .fun/build/artifacts/template.yml
using region: cn-shanghai
using accountId: ***********8320
using accessKeyId: ***********mTN4
using timeout: 60
Waiting for service apk to be deployed...
        make sure role 'aliyunfcgeneratedrole-cn-shanghai-apk' is exist
        role 'aliyunfcgeneratedrole-cn-shanghai-apk' is already exist
        attaching policies AliyunOSSFullAccess to role: aliyunfcgeneratedrole-cn-shanghai-apk
        attached policies AliyunOSSFullAccess to role: aliyunfcgeneratedrole-cn-shanghai-apk
        Waiting 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 MB
        function apk deploy success
service apk deploy success           

6.執行函數

提供兩種方式:

  1. 登陸阿裡雲函數計算 控制台 ,手動執行。
Serverless 微服務實踐-移動應用包分發服務(Fun 3.0 更新版)
  1. 項目根目錄下

    fun invoke apk

    指令遠端調用:
$ fun invoke apk
using template: template.yml
========= FC invoke Logs begin =========
FC Invoke Start RequestId: 23f34cc4-0cd3-40e0-9a04-f6586cf29be6
2019-10-14 17:06:35.971 [INFO] [23f34cc4-0cd3-40e0-9a04-f6586cf29be6] cmd: java -jar /code/walle-cli-all.jar put -c aliyun-fc /tmp/input.apk /tmp/output.apk
2019-10-14 17:06:36.152 [INFO] [23f34cc4-0cd3-40e0-9a04-f6586cf29be6] Success!
FC Invoke End RequestId: 23f34cc4-0cd3-40e0-9a04-f6586cf29be6
Duration: 666.13 ms, Billed Duration: 700 ms, Memory Size: 1024 MB, Max Memory Used: 254.55 MB
========= FC invoke Logs end =========
FC Invoke Result:
Success           

檢視結果

登陸 oss 檢視已經生成新的 apk 包:

Serverless 微服務實踐-移動應用包分發服務(Fun 3.0 更新版)

将 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}

已寫入,成功!

參考閱讀

  1. Fun (Fun with Serverless) 工具
  2. Fun Init 自定義模闆
阿裡巴巴雲原生 關注微服務、Serverless、容器、Service Mesh 等技術領域、聚焦雲原生流行技術趨勢、雲原生大規模的落地實踐,做最懂雲原生開發者的技術圈。”