【发布时间】:2018-08-06 02:01:09
【问题描述】:
我正在向我的 OpenWhisk/IBM Cloud 函数添加 CORS 支持。 但是在对函数进行修改( -a web-custom-options true )之后,我注意到性能下降了。 为了隔离问题,我创建了一个简单的函数,如下所示:
public static JsonObject main(JsonObject args) throws IOException {
String method = args.get("__ow_method").getAsString();
System.out.println(method+" handle");
if (method.equalsIgnoreCase("OPTIONS")) {
JsonObject responseJSON = new JsonObject();
//add CORS headers
JsonObject headers = new JsonObject();
headers.addProperty("Access-Control-Allow-Headers", "*");
headers.addProperty("Access-Control-Allow-Origin", "https://mjonker.github.io");
headers.addProperty("Access-Control-Allow-Credentials", "true");
responseJSON.add("headers", headers);
responseJSON.addProperty("statusCode", 200);
return responseJSON;
} else {
JsonObject responseJSON = new JsonObject();
JsonObject headers = new JsonObject();
headers.addProperty("Access-Control-Allow-Headers", "*");
headers.addProperty("Access-Control-Allow-Origin", "https://mjonker.github.io");
headers.addProperty("Access-Control-Allow-Credentials", "true");
headers.addProperty("Content-Type", "application/json");
responseJSON.add("headers", headers);
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
JsonObject answerJSON=new JsonObject();
JsonArray timeArray = new JsonArray();
timeArray.add( "It is "+sdf.format(now));
answerJSON.add("text",timeArray);
responseJSON.add("body",answerJSON );
responseJSON.addProperty("statusCode", 200);
return responseJSON;
}
}
有两种情况 1. .http 端点和 web-custom-options true 2. .json 端点和 web-custom-options false
从屏幕截图中可以看出,OPTIONS 的差异很大,但 POST 回复的差异也很明显。 我可以做些什么来获得 CORS 的一些性能支持吗? 我在 JAVA 代码中做错了吗?
【问题讨论】:
-
我也将该功能部署到了 eu-gb 区域,并且速度要快得多?这(几秒钟)可能是由延迟引起的吗?我在其他基于美国南部的服务方面没有这种经验......
标签: java cors openwhisk ibm-cloud-functions