本文介绍如何通过Java SDK快速调用API执行创建服务、创建函数、删除函数和删除服务等操作。

前提条件

您已完成以下操作:

Java SDK示例

示例代码如下:

import com.aliyuncs.fc.client.FunctionComputeClient;
import com.aliyuncs.fc.constants.Const;
import com.aliyuncs.fc.model.Code;
import com.aliyuncs.fc.request.*;
import com.aliyuncs.fc.response.*;

import java.io.IOException;
import java.net.HttpURLConnection;

public class FcSample {
    private static final String CODE_DIR = "/tmp/fc_code";
    private static final String REGION = "cn-shanghai";
    private static final String SERVICE_NAME = "test_service";
    private static final String FUNCTION_NAME = "test_function";

    public static void main(final String[] args) throws IOException {
        String accessKey = System.getenv("ACCESS_KEY");
        String accessSecretKey = System.getenv("SECRET_KEY");
        String accountId = System.getenv("ACCOUNT_ID");
        String role = System.getenv("ROLE");

        // 初始化客户端。
        FunctionComputeClient fcClient = new FunctionComputeClient(REGION, accountId, accessKey, accessSecretKey);
        //accountId为您的阿里云账号(主账号)。
        // 按需设置特定的接入点Endpoint,例如:http://123456.cn-hangzhou.fc.aliyuncs.com。
        // 设置函数客户端接入点Endpoint:http://{accountId}.{regionId}.fc.aliyuncs.com。

        // 创建服务。
        CreateServiceRequest csReq = new CreateServiceRequest();
        csReq.setServiceName(SERVICE_NAME);
        csReq.setDescription("FC test service");
        csReq.setRole(role);
        CreateServiceResponse csResp = fcClient.createService(csReq);
        System.out.println("Created service, request ID " + csResp.getRequestId());

        // 创建函数。
        CreateFunctionRequest cfReq = new CreateFunctionRequest(SERVICE_NAME);
        cfReq.setFunctionName(FUNCTION_NAME);
        cfReq.setDescription("Function for test");
        cfReq.setMemorySize(128);
        cfReq.setRuntime("nodejs4.4");
        cfReq.setHandler("counter.handler");
        
        // 用于初始化的情况。
        cfReq.setInitializer("counter.initializer");
        Code code = new Code().setDir(CODE_DIR);
        cfReq.setCode(code);
        cfReq.setTimeout(10);
        CreateFunctionResponse cfResp = fcClient.createFunction(cfReq);
        System.out.println("Created function, request ID " + cfResp.getRequestId());

        // 以字符串作为函数事件参数调用函数,同步模式。
        InvokeFunctionRequest invkReq = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
        String payload = "Hello FunctionCompute!";
        invkReq.setPayload(payload.getBytes());
        InvokeFunctionResponse invkResp = fcClient.invokeFunction(invkReq);
        System.out.println(new String(invkResp.getContent()));

        // 调用函数,异步模式。
        invkReq.setInvocationType(Const.INVOCATION_TYPE_ASYNC);
        invkResp = fcClient.invokeFunction(invkReq);
        if (HttpURLConnection.HTTP_ACCEPTED == invkResp.getStatus()) {
            System.out.println("Async invocation has been queued for execution, request ID: " + invkResp.getRequestId());
        } else {
            System.out.println("Async invocation was not accepted");
        }
        
        // 删除函数。
        DeleteFunctionRequest dfReq = new DeleteFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
        DeleteFunctionResponse dfResp = fcClient.deleteFunction(dfReq);
        System.out.println("Deleted function, request ID " + dfResp.getRequestId());

        // 删除服务。
        DeleteServiceRequest dsReq = new DeleteServiceRequest(SERVICE_NAME);
        DeleteServiceResponse dsResp = fcClient.deleteService(dsReq);
        System.out.println("Deleted service, request ID " + dsResp.getRequestId());
    }
}