gpt4 book ai didi

java - 如何将 Java Lambda 从 AWS API Gateway 转换为负载均衡器

转载 作者:行者123 更新时间:2023-12-01 19:02:41 24 4
gpt4 key购买 nike

我有一个 Java Lambda 函数,由 AWS Api Gateway 调用。 Lambda 函数接受请求正文中的PolicyNo,并在响应中返回相同的PolicyNo。下面是 Lambda 代码。

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

public class AssuranceDataRequestHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent, Context context) {
APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent = new APIGatewayProxyResponseEvent();
try {
LambdaLogger logger = context.getLogger();
final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient();
apiGatewayProxyRequestEvent.getPathParameters();
String requestString = apiGatewayProxyRequestEvent.getBody();
logger.log("API Gateway requestString : " + requestString);
JSONParser parser = new JSONParser();
JSONObject requestJsonObject = (JSONObject) parser.parse(requestString);
String policyNo = null;

if (requestJsonObject != null) {
if (requestJsonObject.get("policyNo") != null) {
policyNo = requestJsonObject.get("policyNo").toString();
}
}
PolicyNo policyNoBo = new PolicyNo();
policyNoBo.setPolicyNo();
ObjectMapper mapper = new ObjectMapper();
try {
responseMessage = mapper.writeValueAsString(policyNoBo);
/// responseMessage = new JSONObject(salesVideoResponse).toJSONString();
System.out.println("ResultingJSONstring = " + responseMessage);
//System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
generateResponse(apiGatewayProxyResponseEvent, responseMessage);
} catch (ParseException e) {
e.printStackTrace();
}
return apiGatewayProxyResponseEvent;
}


private void generateResponse(APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent, String requestMessage) {
apiGatewayProxyResponseEvent.setHeaders(Collections.singletonMap("timeStamp", String.valueOf(System.currentTimeMillis())));
apiGatewayProxyResponseEvent.setStatusCode(200);
apiGatewayProxyResponseEvent.setBody(requestMessage);
}
}

我需要将此实现更改为负载均衡器,其中下面是 URL,我需要从 URL 中获取 PolicyNo。

https://XXXXXXX/api/hk/health/policy/v1/policies/{policyNo}/coverage/benefit/

总之,我需要将 Lambda 函数转换为从负载均衡器调用。

最佳答案

您可以添加应用程序负载均衡器作为 lambda 函数的触发器,这与添加 API 网关触发器的方式类似。创建目标组时请小心...您必须选择 Lambda 函数而不是 ec2 实例。(引用屏幕截图)不需要更改代码,如果您的 Lambda 正在使用 API 网关接口(interface),那么它应该可以与 ALB 很好地配合。

更多帮助您可以引用: https://aws.amazon.com/blogs/networking-and-content-delivery/lambda-functions-as-targets-for-application-load-balancers/

如果您在实现过程中遇到任何问题,请告诉我。

enter image description here

关于java - 如何将 Java Lambda 从 AWS API Gateway 转换为负载均衡器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59608877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com