0

这很奇怪,但是我的 spring boot api 在部署到 aws lambda 时花费的时间比预期的要长得多。在 cloudwatch 日志中,我看到 spring boot 启动了两次,第一次使用默认配置文件,第二次使用我设置的配置文件。为什么要启动两次.. 这非常耗时.. 源代码: lambdahandler.java

public class LambdaHandler implements RequestStreamHandler {

    private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;

    static {
        try {
            handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
            handler.activateSpringProfiles("lambda");
        } catch (ContainerInitializationException e) {
            // Re-throw the exception to force another cold start
            e.printStackTrace();
            throw new RuntimeException("Could not initialize Spring Boot application", e);
        }
    }

应用程序.java

@SpringBootApplication
    public class Application extends SpringBootServletInitializer {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }

这两个文件都在同一个包中

配置.java

@Configuration
@EnableWebMvc
@Profile("lambda")
public class Config {

    /**
     * Create required HandlerMapping, to avoid several default HandlerMapping instances being created
     */
    @Bean
    public HandlerMapping handlerMapping() {
        return new RequestMappingHandlerMapping();
    }

    /**
     * Create required HandlerAdapter, to avoid several default HandlerAdapter instances being created
     */
    @Bean
    public HandlerAdapter handlerAdapter() {
        return new RequestMappingHandlerAdapter();
    }
..
..

}

pom.xml

<dependency>
      <groupId>com.amazonaws.serverless</groupId>
      <artifactId>aws-serverless-java-container-spring</artifactId>
      <version>[0.1,)</version>
    </dependency>
<dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.2.1</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-events</artifactId>
      <version>3.1.0</version>
    </dependency>

云观察日志

    07:16:51.546 [main] INFO com.amazonaws.serverless.proxy.internal.LambdaContainerHandler - Starting Lambda Container Handler
:: Spring Boot ::                        
    2020-09-05 07:16:52.724  INFO 1 --- [           main] lambdainternal.LambdaRTEntry             : Starting LambdaRTEntry on 169.254.184.173 with PID 1 (/var/runtime/lib/LambdaJavaRTEntry-1.0.jar started by sbx_user1051 in /)
    2020-09-05 07:16:52.726  INFO 1 --- [           main] lambdainternal.LambdaRTEntry             : No active profile set, falling back to default profiles: default
    2020-09-05 07:16:52.906  INFO 1 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1e81f4dc: startup date [Sat Sep 05 07:16:52 UTC 2020]; root of context hierarchy
    
    ..
    ..
    2020-09-05 07:16:57.222  INFO 1 --- [           main] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 40 ms
    :: Spring Boot ::                        
    2020-09-05 07:16:57.442  INFO 1 --- [           main] lambdainternal.LambdaRTEntry             : Starting LambdaRTEntry on 169.254.184.173 with PID 1 (/var/runtime/lib/LambdaJavaRTEntry-1.0.jar started by sbx_user1051 in /)
    2020-09-05 07:16:57.442  INFO 1 --- [           main] lambdainternal.LambdaRTEntry             : The following profiles are active: lambda
    2020-09-05 07:16:57.445  INFO 1 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5ef60048: startup date [Sat Sep 05 07:16:57 UTC 2020]; root of context hierarchy
4

1 回答 1

1

为什么要启动两次?

我怀疑您的代码更改为 activateSpringProfiles 强制重新初始化。

handler.activateSpringProfiles("lambda");

https://github.com/awslabs/aws-serverless-java-container/blob/master/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringBootLambdaContainerHandler。爪哇#L149

尝试使用 env 变量设置活动SPRING_PROFILES_ACTIVE配置文件作为 lambda 配置文件的一部分。

Java 和无服务器

如果您将 java 用于 AWS lambdas 之类的无服务器应用程序,我建议您寻找一个支持 Ahead-of-Time 编译的框架,这将大大促进您的应用程序启动。

例如,看看 Micronaut,Quarkus 与 Graalvm 一起使用。Spring Boot 不是直接与 AWS lambda 一起使用的最佳选择。

于 2020-09-05T21:38:02.820 回答