本文介紹Java運行時實現函數執行個體生命週期回調的方法。
背景資訊
當您實現並配置函數執行個體生命週期回調後,Function Compute系統將在相關執行個體生命週期事件發生時調用對應的回調程式。函數執行個體生命週期涉及Initializer、PreStop和PreFreeze三種回調。Java運行時已支援三種回調方式。更多資訊,請參見函數執行個體生命週期回調。
函數執行個體生命週期回調程式與正常調用請求計費規則一致,但其執行日誌只能在函數日誌、執行個體日誌或進階日誌中查詢,調用請求列表不會展示回調程式日誌。具體操作,請參見查看執行個體生命週期回呼函數日誌。
回調方法簽名
Initializer回調簽名
初始化回調程式(Initializer回調)是在函數執行個體啟動成功之後,運行請求處理常式(Handler)之前執行。Function Compute保證在一個執行個體生命週期內,成功且只成功執行一次Initializer回調。例如您的Initializer回調第一次執行失敗了,系統會重試,直到成功為止,然後再執行您的請求處理常式。因此,您在實現Initializer回調時,需要保證它被重複調用時的正確性。
Initializer回調只有一個context輸入參數,使用方法同事件請求處理常式。
initialize方法,介面定義如下。package com.aliyun.fc.runtime;
import java.io.IOException;
/**
* This is the interface for the initialization operation
*/
public interface FunctionInitializer {
/**
* The interface to handle a function compute initialize request
*
* @param context The function compute initialize environment context object.
* @throws IOException IOException during I/O handling
*/
void initialize(Context context) throws IOException;
}PreStop回調簽名
preStop方法,介面定義如下。package com.aliyun.fc.runtime;
import java.io.IOException;
/**
* This is the interface for the preStop operation
*/
public interface PreStopHandler {
/**
* The interface to handle a function compute preStop request
*
* @param context The function compute preStop environment context object.
* @throws IOException IOException during I/O handling
*/
void preStop(Context context) throws IOException;
}PreFreeze回調簽名
preFreeze方法,介面定義如下。package com.aliyun.fc.runtime;
import java.io.IOException;
/**
* This is the interface for the preFreeze operation
*/
public interface PreFreezeHandler {
/**
* The interface to handle a function compute preFreeze request
*
* @param context The function compute preFreeze environment context object.
* @throws IOException IOException during I/O handling
*/
void preFreeze(Context context) throws IOException;
}簡單樣本:流式事件請求處理常式
package example;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.StreamRequestHandler;
import com.aliyun.fc.runtime.FunctionInitializer;
import com.aliyun.fc.runtime.PreFreezeHandler;
import com.aliyun.fc.runtime.PreStopHandler;
public class App implements StreamRequestHandler, FunctionInitializer, PreFreezeHandler, PreStopHandler {
@Override
public void initialize(Context context) throws IOException {
context.getLogger().info("initialize start ...");
}
@Override
public void handleRequest(
InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
context.getLogger().info("handlerRequest ...");
outputStream.write(new String("hello world\n").getBytes());
}
@Override
public void preFreeze(Context context) throws IOException {
context.getLogger().info("preFreeze start ...");
}
@Override
public void preStop(Context context) throws IOException {
context.getLogger().info("preStop start ...");
}
}配置生命週期回呼函數
通過控制台配置
[包名].[類名]::[方法名]。樣本如下:
- Initializer回調程式:設定為
example.App::initialize,表示實現example包中App.java檔案裡的initialize方法。 - PreFreeze回調程式:設定為
example.App::preFreeze,表示實現example包中App.java檔案裡的preFreeze方法。 - PreStop回調程式:設定為
example.App::preStop,表示實現example包中App.java檔案裡的preStop方法。
通過Serverless Devs配置
s.yaml設定檔中添加Initializer 回調程式、PreFreeze 回調程式和PreStop 回調程式。- Initializer回調配置
在function配置下添加initializer和initializationTimeout兩個欄位。
- PreFreeze回調配置
在function配置下添加instanceLifecycleConfig.preFreeze欄位,包括handler和timeout兩個欄位。
- PreStop回調配置
在function配置下添加instanceLifecycleConfig.preStop欄位,包括handler和timeout兩個欄位。
具體的樣本如下所示。
edition: 1.0.0
name: hello-world # 專案名稱
access: default # 密鑰別名
vars: # 全域變數
region: cn-shanghai # 地區
service:
name: fc-example
description: 'fc example by serverless devs'
services:
helloworld: # 業務名稱/模組名稱
component: fc
actions: # 自訂執行邏輯
pre-deploy: # 在deploy之前運行
- run: mvn package # 要啟動並執行命令列
path: ./ # 命令列啟動並執行路徑
props: # 組件的屬性值
region: ${vars.region}
service: ${vars.service}
function:
name: java8-lifecycle-hook-demo
description: 'fc example by serverless devs'
runtime: java8
codeUri: ./target
handler: example.App::handleRequest
memorySize: 128
timeout: 60
initializationTimeout: 60
initializer: example.App::initialize
instanceLifecycleConfig:
preFreeze:
handler: example.App::preFreeze
timeout: 30
preStop:
handler: example.App::preStop
timeout: 30關於Serverless Devs的YAML配置規範,請參見Serverless Devs常用命令。
查看執行個體生命週期回呼函數日誌
您可以通過函數日誌功能查看回呼函數日誌。
- 登入Function Compute控制台,在左側導覽列,單擊服務及函數。
- 在頂部功能表列,選擇地區,然後在服務列表頁面,單擊目標服務。
- 在函數管理頁面,單擊目標函數名稱,然後在函數詳情頁面,單擊測試函數頁簽。
- 在測試函數頁簽,單擊測試函數,然後選擇。在函數日誌頁簽,您可以查看函數的調用日誌、Initializer回調日誌和PreFreeze回調日誌,樣本如下。
2022-10-09 19:26:17 FunctionCompute dotnetcore3.1 runtime inited. 2022-10-09 19:26:17 FC Initialize Start RequestId: 793ad2f1-9826-4d9a-90d9-5bf39e****** 2022-10-09 19:26:17 2022-10-09 19:26:17 793ad2f1-9826-4d9a-90d9-5bf39e****** [INFO] Initialize start 2022-10-09 19:26:17 2022-10-09 19:26:17 793ad2f1-9826-4d9a-90d9-5bf39e****** [INFO] Handle initializer: 793ad2f1-9826-4d9a-90d9-5bf39e****** 2022-10-09 19:26:17 2022-10-09 19:26:17 793ad2f1-9826-4d9a-90d9-5bf39e****** [INFO] Initialize end 2022-10-09 19:26:17 FC Initialize End RequestId: 793ad2f1-9826-4d9a-90d9-5bf39e****** 2022-10-09 19:26:17 FC Invoke Start RequestId: 793ad2f1-9826-4d9a-90d9-5bf39e****** 2022-10-09 19:26:17 2022-10-09 19:26:17 793ad2f1-9826-4d9a-90d9-5bf39e****** [INFO] Handle request: 793ad2f1-9826-4d9a-90d9-5bf39e****** 2022-10-09 19:26:17 FC Invoke End RequestId: 793ad2f1-9826-4d9a-90d9-5bf39e****** 2022-10-09 19:26:17 FC PreFreeze Start RequestId: 793ad2f1-9826-4d9a-90d9-5bf39e****** 2022-10-09 19:26:17 2022-10-09 19:26:17 793ad2f1-9826-4d9a-90d9-5bf39e****** [INFO] PreFreeze start 2022-10-09 19:26:17 2022-10-09 19:26:17 793ad2f1-9826-4d9a-90d9-5bf39e****** [INFO] Handle PreFreeze: 793ad2f1-9826-4d9a-90d9-5bf39e****** 2022-10-09 19:26:17 2022-10-09 19:26:17 793ad2f1-9826-4d9a-90d9-5bf39e****** [INFO] PreFreeze end 2022-10-09 19:26:17 FC PreFreeze End RequestId: 793ad2f1-9826-4d9a-90d9-5bf39e******因為每個函數執行個體會緩衝一段時間,不會馬上銷毀,因此不能立即查看PreStop回調日誌。如需快速觸發PreStop回調,可更新函數配置或者函數代碼。更新完成後,再次查看函數日誌,您可以查看PreStop回調日誌。樣本如下。2022-10-09 19:32:17 FC PreStop Start RequestId: 03be685c-378b-4736-8b08-a67c1d***** 2022-10-09 19:32:17 2022-10-09 19:32:17 03be685c-378b-4736-8b08-a67c1d***** [INFO] PreStop start 2022-10-09 19:32:17 2022-10-09 19:32:17 03be685c-378b-4736-8b08-a67c1d***** [INFO] Handle PreStop: 03be685c-378b-4736-8b08-a67c1d***** 2022-10-09 19:32:17 2022-10-09 19:32:17 03be685c-378b-4736-8b08-a67c1d***** [INFO] PreStop end 2022-10-09 19:32:17 FC PreStop End RequestId: 03be685c-378b-4736-8b08-a67c1d*****
樣本程式
- java11-mysql:Function Compute提供的Initializer回調和PreStop回調的樣本程式。
該樣本為您展示了如何使用Java運行時的Initializer回調從環境變數中擷取資料庫配置並建立MySQL串連,以及如何使用PreStop回調負責關閉MySQL串連。