首页 > 解决方案 > 如何通过 AWS lambda 连接超时修复对 kafka 服务器的 http 请求?

问题描述

首先我想提一下,我是 AWS 和 Kafka 的新手。

每当生产者在主题中生成时,我部署在 AWS EC2 实例上的 Kafka 服务器都会触发 lambda。lambda 函数尝试调用消费者 api 以从该主题消费,但 lambda 尝试创建的 http 连接由于超时而失败。lambda 函数与 kafka 服务器在同一个 vpc 中。

我尝试了以下事情:

  1. 检查问题是否仅与该特定 api 或每个 api 有关。我发现其他 api 例如http://google.co.in给了我没有任何错误的响应。
  2. 明显使用了kafka服务器的公网ip
  3. 从 vpc 中删除并尝试不使用 vpc,仍然是同样的问题
  4. 附加的完全访问 vpc 的策略。
            URL url = new URL("http://ec2-*-*-*-*.ap-south-1.compute.amazonaws.com/consumers/db_json_consumer/instances/lambda_consumer/topics/kbtest");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            logger.log("Connection : " + conn);

            conn.connect();

            if (conn.getResponseCode() != 200) {
                logger.log("Failed : HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }
            logger.log("\nSending 'GET' request to URL : " + url);
            logger.log("Response Code : " + conn.getResponseCode());
            conn.disconnect();
}
Execution result: failed(logs)
Details
The area below shows the result returned by your function execution. Learn more about returning results from your function.
{
  "errorMessage": "2019-06-24T12:25:08.529Z 406d0e22-3283-4f02-9c31-cb2163c1b950 Task timed out after 15.02 seconds"
}
Summary
Code SHA-256
Mn3/26Ky/hBE1ONL+LDdYaLEiCQZo6VHdlfcRpF8WYw=
Request ID
406d0e22-3283-4f02-9c31-cb2163c1b950
Duration
15015.27 ms
Billed duration
15000 ms
Resources configured
512 MB
Max memory used
55 MB
Log output
The section below shows the logging calls in your code. These correspond to a single row within the CloudWatch log group corresponding to this Lambda function. Click here to view the CloudWatch log group.
START RequestId: 406d0e22-3283-4f02-9c31-cb2163c1b950 Version: $LATEST
Heyy lambda object : HeyConnection : sun.net.www.protocol.http.HttpURLConnection:http://13.235.76.198:8082/consumers/db_json_consumer/instances/lambda_consumer/topics/kbtestEND RequestId: 406d0e22-3283-4f02-9c31-cb2163c1b950
REPORT RequestId: 406d0e22-3283-4f02-9c31-cb2163c1b950  Duration: 15015.27 ms   Billed Duration: 15000 ms   Memory Size: 512 MB Max Memory Used: 55 MB  
2019-06-24T12:25:08.529Z 406d0e22-3283-4f02-9c31-cb2163c1b950 Task timed out after 15.02 seconds

标签: javaamazon-web-servicesapache-kafkaaws-lambda

解决方案


推荐阅读