java - 在 AWS lambda 中使用 Spring data jpa 部署 Spring boot

标签 java spring-boot aws-lambda spring-data-jpa amazon-rds

我已经创建了一个带有 spring 数据 JPA 的 spring boot 应用程序,它应该连接到一个 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>

我的初始化类如下,

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 日志中,我可以看到,

enter image description here

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

最佳答案

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

LambdaContainerHandler.getContainerConfig().setInitializationTimeout(60_000); handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);

关于java - 在 AWS lambda 中使用 Spring data jpa 部署 Spring boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65983490/

相关文章:

java - Spring Boot 2.2.6 - 安全 |更新用户信息后如何更新Principal

java - Tomcat v9.0 服务器在 Eclipse 中启动错误

amazon-web-services - AWS Lambda 计划任务

java - 我正在尝试从不同的 URL 获取数据,然后在后台加载

java - 从 URI 检索 RDF 时如何设置 HTTP 超时?

java - 没有找到接口(interface) java.util.List Rest API Spring boot 的主构造函数或默认构造函数

aws-lambda - lambda 函数内的跨堆栈引用

aws-lambda - 从冷启动的角度来看,我们是否在 AWS Lambda 的同一个文件或多个文件中编写处理程序重要吗?

java - 如果同一个实例已经在运行,如何关闭 javax 应用程序?

java - 如何在java中清除SerialPort的OutputStream,而不需要关闭它?