簡體   English   中英

Spring Boot 應用程序的 AWS Lambda 超時問題

[英]AWS Lambda timeout issue with spring boot application

我有一個 spring boot 應用程序,我試圖在 AWS lambda 上部署它。

我將 StreamLambdaHandler 添加為處理程序類

公共類 StreamLambdaHandler 實現 RequestStreamHandler {

private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;

static {
    try {
        //handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(SituationalFlexibilityApp.class);

        // For applications that take longer than 10 seconds to start, use the async builder:

         handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
                            .defaultProxy()
                            .asyncInit()
                            .springBootApplication(SituationalFlexibilityApp.class)
                            .buildAndInitialize();

        // we use the onStartup method of the handler to register our custom filter
        handler.onStartup(servletContext -> {
            FilterRegistration.Dynamic registration = servletContext.addFilter("CognitoIdentityFilter",CognitoIdentityFilter.class);
            registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
        });
    } catch (ContainerInitializationException e) {
        // if we fail here. We re-throw the exception to force another cold start
        e.printStackTrace();
        throw new RuntimeException("Could not initialize Spring Boot application", e);
    }
}

public StreamLambdaHandler() {
    Timer.enable();
}
 

/*
 * public StreamLambdaHandler() throws ContainerInitializationException {
 * 
 * handler = new SpringBootProxyHandlerBuilder() .defaultProxy() .asyncInit()
 * .springBootApplication(SlowApplication.class) .buildAndInitialize(); }
 */

@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
    handler.proxyStream(input, output, context);

}

當我在 AWS lambda 上測試它時,我得到以下異常 com.amazonaws.serverless.exceptions.ContainerInitializationException: Could not initialize framework within the 20000ms timeout

所以我更新了 lambda 配置,超時時間為 5 分鍾,並在 StreamLambdaHandler 類 LambdaContainerHandler.getContainerConfig().setInitializationTimeout(2000000) 的靜態塊中添加了以下行;

現在,我看到下面的異常 Exception in thread "Thread-0" java.lang.IllegalArgumentException: Could not find timer SPRINGBOOT2_COLD_START

有人能給我指出正確的方向嗎,因為我是 AWS 服務和 lambda 的菜鳥

StreamLambdaHandler方法中注釋掉以下代碼后,我沒有看到此錯誤

// Timer.enable();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM