函数计算的互调方法,可从函数计算的官方文档>常见问题中找到链接,具体链接请点击此处。
首先需要编写被调用的函数
引入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省略
...
}
测试body:{"name":"小王","age":18,"gender":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省略
...
}
上传控制台新建函数
测试body:{"bString":"demoB"}