関数の計算:計算関数は、関数の計算を呼び出します

演算機能の相互変調方法、機能は公式文書から計算することができる> FAQには、特定のリンクへのリンクを見つけ、こちらをクリックしてください。

まず、呼び出される関数を記述する必要があります

導入Mavenの依存性

<dependency>
    <groupId>com.aliyun.fc.runtime</groupId>
    <artifactId>fc-java-core</artifactId>
    <version>1.2.0</version>
</dependency>
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.PojoRequestHandler;
import com.example.demo.utils.ApiRequest;
import com.example.demo.utils.ApiResponse;

//被调用的函数计算
public class FCDemoA implements PojoRequestHandler<ApiRequest, ApiResponse> {
    @Override
    public ApiResponse handleRequest(ApiRequest request, Context context) {
        context.getLogger().info(request.getName());
        context.getLogger().info(String.valueOf(request.getAge()));
        context.getLogger().info(String.valueOf(request.getGender()));
        context.getLogger().info("----------FCDemoA被调用----------");
        String msg = "My info: " + request.getName() + request.getAge() + request.getGender();
        return new ApiResponse(200, msg);
    }

}
public class ApiRequest {
    private String name;
    private int age;
    private int gender;
    private String remark;

    ...
    getter和setter省略
    ...
}
public class ApiResponse {
    private Integer code;
    private String msg;
    public ApiResponse(int code,String msg){
        this.code = code;
        this.msg = msg;
    }

    ...
    getter和setter省略
    ...
}

試験体:{ "名前": "王"、 "年齢" 18、 "性別":1}

新しいコンソール機能した後、呼び出し元のコードを記述します。GitHubのの導入は、本明細書にMavenを導入する必要性に依存しています:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-fc</artifactId>
    <version>1.3.2</version>
</dependency>

詳細コード

import java.net.HttpURLConnection;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.PojoRequestHandler;
import com.aliyuncs.fc.client.FunctionComputeClient;
import com.aliyuncs.fc.constants.Const;
import com.aliyuncs.fc.request.InvokeFunctionRequest;
import com.aliyuncs.fc.response.InvokeFunctionResponse;
import com.example.demo.utils.BRequest;
import com.example.demo.utils.BResponse;

public class FCDemoB implements PojoRequestHandler<BRequest, BResponse> {
    @Override
    public BResponse handleRequest(BRequest request, Context context) {
        // Set to a specific endpoint in case needed, endpoint sample:
        // http://123456.cn-hangzhou.fc.aliyuncs.com
        // fcClient.setEndpoint("http://{accountId}.{regionId}.fc.aliyuncs.com.");
        final String REGION = "cn-hangzhou";
        final String SERVICE_NAME = "demoService";
        final String FUNCTION_NAME = "FCDemoA";
        final String accountId = "accountId";
        final String accessKey = "accessKey";
        final String accessSecretKey = "accessSecretKey";
        // Initialize FC client
        FunctionComputeClient fcClient = new FunctionComputeClient(REGION, accountId, accessKey, accessSecretKey);
        InvokeFunctionRequest invkReq = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
        String payload = "{\"name\":\"小王\",\"age\":18,\"gender\":1}";
        invkReq.setPayload(payload.getBytes());
        InvokeFunctionResponse invkResp = fcClient.invokeFunction(invkReq);
        context.getLogger().info("FCDemoB获得的参数是:" + request.getbString());
        String result = new String(invkResp.getContent());
        context.getLogger().info("FCDemoA返回的数据是:" + result);

        // Invoke the function, Async mode
        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");
        }
        return new BResponse(200, "FCDemoB返回的信息", "FCDemoA返回的信息:" + result);
    }
}
public class BRequest {
    private String bString;
    public String getbString() {
        return bString;
    }
    public void setbString(String bString) {
        this.bString = bString;
    }
}
public class BResponse {
    private int code;
    private String bRet;
    private String fcaStr;

    public BResponse(int code, String bRet, String fcaStr) {
        this.code = code;
        this.bRet = bRet;
        this.fcaStr = fcaStr;
    }

    ...
    getter和setter省略
    ...
}

新しいコンソール機能をアップロード

試験体:{ "bString": "demoB"}

ます。https://my.oschina.net/u/4108765/blog/3059590で再現

おすすめ

転載: blog.csdn.net/weixin_33832340/article/details/92427512