在 AWS lambda 中使用 Spring 数据 jpa 部署 Spring 启动

Deploying Spring boot with Spring data jpa in AWS lambda

我已经创建了一个 spring 引导应用程序,其中包含 spring 数据 JPA,它应该与 RDS 实例连接,我已经公开了 api,这将 deo CRUD 我已经提供了application.yml 文件中的数据库连接详细信息,

spring:
  datasource:
    url: url
    username: username
    password: password

我也添加了下面的依赖,

  <dependency>
        <groupId>com.amazonaws.serverless</groupId>
        <artifactId>aws-serverless-java-container-springboot2</artifactId>
        <version>1.4</version>
    </dependency>

我的初始化程序 class 如下所示,

private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
    static {
        try {
            
            if (handler == null) {
                LambdaContainerHandler.getContainerConfig().setInitializationTimeout(60_000);
                handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);



                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);
        }
    }

一切似乎都是正确的,我已经从 AWS 控制台创建了一个 lambda,还使用下面的汇编程序打包了 jar,

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>lambda-package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <!-- copy runtime dependencies with some exclusions -->
        <fileSet>
            <directory>${project.build.directory}${file.separator}lib</directory>
            <outputDirectory>lib</outputDirectory>
            <excludes>
                <exclude>tomcat-embed*</exclude>
            </excludes>
        </fileSet>
        <!-- copy all classes -->
        <fileSet>
            <directory>${project.build.directory}${file.separator}classes</directory>
            <includes>
                <include>**</include>
            </includes>
            <outputDirectory>${file.separator}</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

我正在使用下面的插件

<profiles>
        <profile>
            <id>shaded-jar</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>2.3</version>
                        <configuration>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>
                                </goals>
                                <configuration>
                                    <artifactSet>
                                        <excludes>
                                            <exclude>org.apache.tomcat.embed:*</exclude>
                                        </excludes>
                                    </artifactSet>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>assembly-zip</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <!-- select and copy only runtime dependencies to a temporary lib folder -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>3.1.1</version>
                        <executions>
                            <execution>
                                <id>copy-dependencies</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                    <includeScope>runtime</includeScope>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>3.1.0</version>
                        <executions>
                            <execution>
                                <id>zip-assembly</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <finalName>${project.artifactId}-${project.version}</finalName>
                                    <descriptors>
                                        <descriptor>src${file.separator}assembly${file.separator}bin.xml</descriptor>
                                    </descriptors>
                                    <attach>false</attach>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>


</project>

已创建一个 zip,正在将 zip 上传到 Lambda 函数并将 s3 URL 提供给 Lambda 函数。

之后我创建了一个 API 网关 HTTP API 并选择集成作为之前创建的 Lambda。如果我尝试访问 APi,我会收到 {"message":"Internal Server Error"}

在 cloudwatch 日志中,我可以看到,

正在建立数据库连接。我选择了执行规则作为 lambda 的管理员,并提供了与 RDS 相同的 VPC,但运气不好,请告诉我一个解决方案。

增加 Lambda 超时解决了这个问题。 也添加了以下几行,

LambdaContainerHandler.getContainerConfig().setInitializationTimeout(60_000); 处理程序 = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);