Function calculation: calculation function calls the function calculation

Intermodulation method of calculation function, the function can be calculated from official documents> FAQ find links to specific links, click here.

First, we need to write a function to be called

Introduced maven dependence

<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省略
    ...
}

Test body: { "name": "Wang", "age": 18, "gender": 1}

After the new console function, write the code of the caller. The introduction of Github herein rely on the need to introduce maven:

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

Detailed code

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省略
    ...
}

Upload new console function

Test body: { "bString": "demoB"}

Reproduced in: https: //my.oschina.net/u/4108765/blog/3059590

Guess you like

Origin blog.csdn.net/weixin_33832340/article/details/92427512