天天看點

開發函數計算的正确姿勢——運作 Selenium Java

前言

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

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

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

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

依賴工具

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

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

對于 MacOS 使用者可以使用

homebrew

進行安裝:

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

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

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

安裝好後,記得先執行

fun config

初始化一下配置。

注意, 如果你已經安裝過了 fun,確定 fun 的版本在 2.10.2 以上。

$ fun --version
2.10.1           

快速開始

初始化

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

fun init vangie/selenium-java-example           

安裝依賴

$ fun install
...           

本地測試

測試代碼 ChromeDemo 的内容為:

public class ChromeDemo implements StreamRequestHandler {


    public void handleRequest(InputStream inputStream,
                              OutputStream outputStream,
                              Context context) throws IOException {

        System.setProperty("webdriver.chrome.driver", "/code/chromedriver");

        ChromeOptions options = new ChromeOptions();
        options.setBinary("/code/headless-chromium");
        options.addArguments("--disable-extensions"); // disabling extensions
        options.addArguments("--disable-gpu"); // applicable to windows os only
        options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
        options.addArguments("--no-sandbox"); // Bypass OS security model
        options.addArguments("--headless");

        WebDriver driver = new ChromeDriver(options);

        driver.get("https://ide.fc.aliyun.com");

        outputStream.write(("Page title is: " + driver.getTitle() + "\n").getBytes());

        driver.quit();

    }

}           

本地運作

$ mvn package && fun local invoke selenium
...
FC Invoke Start RequestId: 68c83b4c-b053-479c-9b0e-9503582ccb56
handle user request is com.aliyun.fc.selenium.ChromeDemo::handleRequest
cache is null!
Starting ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) on port 20652
Only local connections are allowed.
Mar 05, 2019 11:34:27 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Page title is: 雲端內建開發環境
FC Invoke End RequestId: 68c83b4c-b053-479c-9b0e-9503582ccb56


RequestId: 68c83b4c-b053-479c-9b0e-9503582ccb56          Billed Duration: 5265 ms        Memory Size: 1998 MB    Max Memory Used: 240 MB
           

部署

$ mvn package && fun deploy           

執行

$  fcli function invoke -s chrome -f selenium
  Page title is: 雲端內建開發環境           

關于檔案尺寸

由于 chromedriver 和 headless-chromium 壓縮後體積已經非常接近 50MB,留給使用者 Jar 的空間非常少,是以另外制作了一個高壓縮比版本,使用壓縮比更高的 brotli 算法進行壓縮,壓縮後的大小為 32.7MB。然後在運作時使用 initializer 進行解壓,解壓耗時大約為 3.7 S。

https://github.com/vangie/packed-selenium-java-example

參考閱讀

  1. https://github.com/smithclay/lambdium
  2. https://medium.com/clog/running-selenium-and-headless-chrome-on-aws-lambda-fb350458e4df