天天看點

開發函數計算的正确姿勢——網頁截圖服務前言依賴工具初始化安裝依賴部署驗證調試參考閱讀招聘

前言

首先介紹下在本文出現的幾個比較重要的概念:

函數計算(Function Compute): 函數計算 是一個事件驅動的服務,通過函數計算,使用者無需管理伺服器等運作情況,隻需編寫代碼并上傳。函數計算準備計算資源,并以彈性伸縮的方式運作使用者代碼,而使用者隻需根據實際代碼運作所消耗的資源進行付費。函數計算更多資訊 參考

Fun: Fun 是一個用于支援 Serverless 應用部署的工具,能幫助您便捷地管理函數計算、API 網關、日志服務等資源。它通過一個資源配置檔案(template.yml),協助您進行開發、建構、部署操作。Fun 的更多文檔

備注: 本文介紹的技巧需要 Fun 版本大于等于 3.5.0。

依賴工具

本項目是在 MacOS 下開發的,涉及到的工具是平台無關的,對于 Linux 和 Windows 桌面系統應該也同樣适用。在開始本例之前請確定如下工具已經正确的安裝,更新到最新版本,并進行正确的配置。

Fun 工具依賴于 docker 來模拟本地環境。

對于 MacOS 使用者可以使用

homebrew

進行安裝:

brew cask install docker
brew tap vangie/formula
brew install fun           

Windows 和 Linux 使用者安裝請參考:

  1. https://github.com/aliyun/fun/blob/master/docs/usage/installation.md
  2. https://github.com/aliyun/fcli/releases

安裝好後,記得先執行

fun config

初始化一下配置。

初始化

使用 fun init 指令可以快捷的将本模闆項目初始化到本地。

fun init vangie/puppeteer-example           

安裝依賴

fun install           

fun install

會執行 Funfile 檔案裡的指令,依次執行如下任務:

  1. 安裝 chrome headless 二進制檔案;
  2. 安裝 puppeteer 依賴的 apt 包;
  3. 安裝 npm 依賴。

部署

同步大檔案到 nas 盤:

fun nas sync           

部署代碼:

$ fun deploy
using template: template.yml
using region: cn-hangzhou
using accountId: ***********3743
using accessKeyId: ***********Ptgk
using timeout: 600
Waiting for service puppeteer to be deployed...
        make sure role 'aliyunfcgeneratedrole-cn-hangzhou-puppeteer' is exist
        role 'aliyunfcgeneratedrole-cn-hangzhou-puppeteer' is already exist
        attaching police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hangzhou-puppeteer
        attached police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hangzhou-puppeteer
        using 'VpcConfig: Auto', Fun will try to generate related vpc resources automatically
                vpc already generated, vpcId is: vpc-bp1wv9al02opqahkizmvr
                vswitch already generated, vswitchId is: vsw-bp1kablus0jrcdeth8v35
                security group already generated, security group is: sg-bp1h2swzeb5vgjfu6gpo
        generated auto VpcConfig done:  {"vpcId":"vpc-bp1wv9al02opqahkizmvr","vswitchIds":["vsw-bp1kablus0jrcdeth8v35"],"securityGroupId":"sg-bp1h2swzeb5vgjfu6gpo"}
        using 'NasConfig: Auto', Fun will try to generate related nas file system automatically
                nas file system already generated, fileSystemId is: 0825a4a395
                nas file system mount target is already created, mountTargetDomain is: 0825a4a395-rrf16.cn-hangzhou.nas.aliyuncs.com
        generated auto NasConfig done:  {"UserId":10003,"GroupId":10003,"MountPoints":[{"ServerAddr":"0825a4a395-rrf16.cn-hangzhou.nas.aliyuncs.com:/puppeteer","MountDir":"/mnt/auto"}]}
        Checking if nas directories /puppeteer exists, if not, it will be created automatically
        Checking nas directories done ["/puppeteer"]
        Waiting for function html2png to be deployed...
                Waiting for packaging function html2png code...
                The function html2png has been packaged. A total of 7 files files were compressed and the final size was 2.56 KB
                Waiting for HTTP trigger httpTrigger to be deployed...
                triggerName: httpTrigger
                methods: [ 'GET' ]
                url: https://xxxxxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/puppeteer/html2png/
                Http Trigger will forcefully add a 'Content-Disposition: attachment' field to the response header, which cannot be overwritten 
                and will cause the response to be downloaded as an attachment in the browser. This issue can be avoided by using CustomDomain.
                trigger httpTrigger deploy success
        function html2png deploy success
service puppeteer deploy success
===================================== Tips for nas resources ==================================================
Fun has detected the .nas.yml file in your working directory, which contains the local directory:
        /Users/vangie/Workspace/puppeteer-example/{{ projectName }}/.fun/root
        /Users/vangie/Workspace/puppeteer-example/{{ projectName }}/node_modules
  
The above directories will be automatically ignored when 'fun deploy'.
Any content of the above directories changes,you need to use 'fun nas sync' to sync local resources to remote.
===============================================================================================================           

驗證

curl https://xxxxxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/puppeteer/html2png/ > screenshot.png           

如果不傳遞查詢參數,預設會截取阿裡雲的首頁。

開發函數計算的正确姿勢——網頁截圖服務前言依賴工具初始化安裝依賴部署驗證調試參考閱讀招聘

如果想換一個網址,可以使用如下指令格式:

curl https://xxxxxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/puppeteer/html2png/?url=http://www.alibaba.com > screenshot.png           
開發函數計算的正确姿勢——網頁截圖服務前言依賴工具初始化安裝依賴部署驗證調試參考閱讀招聘

調試

如果需要在本地調試代碼,可以使用如下指令:

$ fun local start
using template: template.yml
HttpTrigger httpTrigger of puppeteer/html2png was registered
        url: http://localhost:8000/2016-08-15/proxy/puppeteer/html2png
        methods: [ 'GET' ]
        authType: ANONYMOUS
function compute app listening on port 8000!           

浏覽器打開

http://localhost:8000/2016-08-15/proxy/puppeteer/html2png

即可。

參考閱讀

招聘

TL;DR

阿裡雲 - 雲原生應用平台 - 基礎軟體中台團隊(原容器平台基礎軟體團隊)誠邀 Kubernetes/容器/ Serverless/應用傳遞技術領域專家( P6-P8 )加盟。

工作年限:建議 P6-7 三年起,P8 五年起,具體看實際能力。

工作地點:

  • 國内:北京,杭州,深圳;
  • 海外:舊金山灣區、西雅圖

履歷立刻回複,2~3 周出結果。節後入職。

工作内容

基礎産品事業部是阿裡雲智能事業群的核心研發部門,負責計算、存儲、網絡、安全、中間件、系統軟體等研發。而雲原生應用平台基礎軟體終态團隊緻力于打造穩定、标準、先進的雲原生應用系統平台,推動行業面向雲原生技術更新與革命。

在這裡,既有 CNCF TOC 和 SIG 聯席主席,也有 etcd 創始人、K8s Operator 創始人與 Kubernetes 核心維護成員組成的、國内最頂尖的 Kubernetes 技術團隊。

在這裡,你将同來自全球的雲原生技術領域專家們(如 Helm 項目的創始人、Istio 項目的創始人)密切合作,在獨一無二的場景與規模中從事 Kubernetes、Service Mesh、Serverless、Open Application Model ( OAM )等雲計算生态核心技術的研發與落地工作,在業界标杆級的平台上,既賦能阿裡巴巴全球經濟體,更服務全世界的開發者使用者。

  1. 以 Kubernetes 為核心,推動并打造下一代 "以應用為中心" 的基礎技術體系;在阿裡經濟體場景中,研發和落地“以應用為中心”的基礎設施架構和基于 Open Application Model ( OAM )的下一代 NoOps 體系,讓 Kubernetes 與雲原生技術棧發揮出真正的價值和能量;
  2. 研發多環境複雜應用傳遞核心技術;結合阿裡與生态中的核心業務場景,打造多環境複雜應用傳遞的業界标準與核心依賴(對标 Google Cloud Anthos 和 Microsoft Azure Arc );
  3. 雲原生應用平台核心産品及後端架構設計與開發工作;在生态核心技術與前沿架構的加持下,在世界級雲廠商的平台場景中,用技術打造持續的雲産品生命力與競争力;
  4. 持續推動阿裡經濟體應用平台架構演進,包括 Serverless 基礎設施、标準雲原生标準 PaaS 建構、新一代應用傳遞體系建構等核心技術工作。

技術要求:Go/Rust/Java/C++,Linux,分布式系統

履歷送出

lei.zhang AT

alibaba-inc.com
阿裡巴巴雲原生 關注微服務、Serverless、容器、Service Mesh 等技術領域、聚焦雲原生流行技術趨勢、雲原生大規模的落地實踐,做最懂雲原生開發者的技術圈。”

繼續閱讀