全部產品
Search
文件中心

Function Compute:編譯部署程式碼封裝

更新時間:Aug 15, 2024

您可以在本地Java運行環境(Maven或Serverless Devs工具)編譯器,打包為ZIP包或JAR包,然後在Function Compute控制台或使用Serverless Devs工具上傳程式碼封裝,並正確運行您的代碼。

Java運行時依賴庫

要建立部署程式碼封裝,請將函數代碼和依賴庫共同編譯並打包為ZIP包或JAR包。

Function Compute平台為Java運行時提供以下依賴庫:

以上依賴庫可通過Maven中央存放庫擷取。擷取以上依賴庫後將其添加到您的pom.xml檔案中,如下所示:

<!-- https://mvnrepository.com/artifact/com.aliyun.fc.runtime/fc-java-core -->
<dependency>
    <groupId>com.aliyun.fc.runtime</groupId>
    <artifactId>fc-java-core</artifactId>
    <version>1.4.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.aliyun.fc.runtime/fc-java-event -->
<dependency>
    <groupId>com.aliyun.fc.runtime</groupId>
    <artifactId>fc-java-event</artifactId>
    <version>1.2.0</version>
</dependency>
說明

如果依賴包太大,可將依賴打包到層中,以減少程式碼封裝體積。具體操作,請參見建立自訂層

使用Maven編譯並部署

前提條件

安裝Java和Maven。關於Java的詳細資料,請參見官網。關於Maven的詳細資料,請參見Installing Apache Maven

操作步驟

  1. 建立一個Java專案,其中App.java檔案路徑如下。

    src/main/java/example/App.java  

    App.java檔案中輸入範例程式碼。具體範例程式碼,請參見請求處理常式(Handler)上下文

    package example;
    
    import com.aliyun.fc.runtime.Context;
    import com.aliyun.fc.runtime.StreamRequestHandler;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class App implements StreamRequestHandler {
    
        @Override
        public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
            outputStream.write(new String("hello world").getBytes());
        }
    }
  2. pom.xml檔案中配置build,樣本如下。

    <build>
            <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-shade-plugin</artifactId>
                      <version>3.2.1</version>
                      <executions>
                        <execution>
                          <phase>package</phase>
                          <goals>
                            <goal>shade</goal>
                          </goals>
                          <configuration>
                            <filters>
                              <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                  <exclude>META-INF/*.SF</exclude>
                                  <exclude>META-INF/*.DSA</exclude>
                                  <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                              </filter>
                            </filters>
                          </configuration>
                        </execution>
                      </executions>
                  </plugin>
            </plugins>
    </build>
    說明

    您可以使用Apache Maven Shade外掛程式或Apache Maven Assembly外掛程式,以上樣本僅以Apache Maven Shade外掛程式為例。

    展開查看完整的pom.xml檔案樣本。

    <project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>example</groupId>
      <artifactId>HelloFCJava</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>HelloFCJava</name>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>com.aliyun.fc.runtime</groupId>
          <artifactId>fc-java-core</artifactId>
          <version>1.4.1</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-shade-plugin</artifactId>
              <version>3.2.1</version>
              <executions>
                <execution>
                  <phase>package</phase>
                  <goals>
                    <goal>shade</goal>
                  </goals>
                  <configuration>
                    <filters>
                      <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                          <exclude>META-INF/*.SF</exclude>
                          <exclude>META-INF/*.DSA</exclude>
                          <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                      </filter>
                    </filters>
                  </configuration>
                </execution>
              </executions>
            </plugin>
        </plugins>
      </build>
    
      <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.test.skip>true</maven.test.skip>
      </properties>
    </project>
  3. 開啟命令列視窗,切換至專案的根目錄,然後執行mvn clean package命令打包。

    範例程式碼如下。

    [INFO] Scanning for projects...
     ...  ....   ....
    [INFO] --------------------------< example:example >---------------------------
    [INFO] Building java-example 1.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
     ...  ....   ....
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  11.681 s
    [INFO] Finished at: 2020-03-26T15:55:05+08:00
    [INFO] ------------------------------------------------------------------------
    • 如果顯示編譯失敗,請根據輸出的編譯錯誤資訊調整代碼。

    • 如果編譯成功,編譯後的JAR包位於專案檔夾內的target目錄內,並根據pom.xml內的artifactIdversion欄位命名為HelloFCJava-1.0-SNAPSHOT.jar

      重要

      針對macOS和Linux作業系統,壓縮前請確保代碼檔案具有可讀和可執行許可權。

  4. 登入Function Compute控制台,上傳程式碼封裝。

    image

  5. 函數詳情配置頁簽,確認請求處理常式正確性,您的請求處理常式需配置為[包名].[類名]::[方法名]。例如,您的包名為example,類名為App,方法名為handleRequest,則請求處理常式可配置為example.App::handleRequest,如果不正確點擊編輯進行修改。

    image

  6. 代碼頁簽,單擊測試函數進行測試。

    image

使用Serverless Devs編譯並部署

前提條件

操作步驟

  1. 執行以下命令,初始化專案。

    s init 

    根據介面提示依次選擇阿里雲廠商、模板、運行時以及部署應用的地區和函數名稱等。

  2. 執行以下命令,進入專案目錄。

    cd start-fc-event-java8

    代碼目錄結構如下:

    start-fc-event-java8
    ├── src
    │   └── main
    │       └── java
    │           └── example
    │               └── App.java
    ├── pom.xml
    ├── readme
    └── s.yaml
  3. 執行以下命令,部署專案。

    s deploy

    執行該命令會先執行pre-deploypre-deploy會執行mvn package編譯打包,然後上傳部署程式碼封裝。輸出樣本如下:

    [2022-04-07 12:00:09] [INFO] [S-CORE] - Start the pre-action
    [2022-04-07 12:00:09] [INFO] [S-CORE] - Action: mvn package
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------< example:HelloFCJava >-------------------------
    [INFO] Building HelloFCJava 1.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    ......
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  3.617 s
    [INFO] Finished at: 2022-04-07T20:00:14+08:00
    [INFO] ------------------------------------------------------------------------
    [2022-04-07 12:00:14] [INFO] [S-CORE] - End the pre-action
    
    ✔ Checking Service, Function (0.64s)
    ✔ Creating Service, Function (0.71s)
    
    Tips for next step
    ======================
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
    
    
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       start-fc-event-java8
        runtime:    java8
        handler:    example.App::handleRequest
        memorySize: 128
        timeout:    60
  4. 執行s invoke命令進行測試。

    輸出樣本如下:

    ➜  start-fc-event-java8 s invoke
    ========= FC invoke Logs begin =========
    FC Initialize Start RequestId: b246c3bf-06bc-49e5-92b8-xxxxxxxx
    FC Initialize End RequestId: b246c3bf-06bc-49e5-92b8-xxxxxxxx
    FC Invoke Start RequestId: b246c3bf-06bc-49e5-92b8-xxxxxxxx
    FC Invoke End RequestId: b246c3bf-06bc-49e5-92b8-xxxxxxxx
    
    Duration: 7.27 ms, Billed Duration: 8 ms, Memory Size: 128 MB, Max Memory Used: 65.75 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    hello world
    
    
    
    End of method: invoke