我已经创建了一个带有 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 日志中,我可以看到,
正在建立数据库连接。我为 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/