gpt4 book ai didi

java - 将应用程序部署到 AWS Lambda 会引发 Postgres 驱动程序错误

转载 作者:行者123 更新时间:2023-12-01 16:30:19 25 4
gpt4 key购买 nike

我创建了一个 spring-boot2 应用程序,还在 AWS 上启动了一个 POSTGRES 数据库。当我在本地启动应用程序时,我可以很好地连接到它。但是,当我右键单击 template.yaml 并选择“部署无服务器应用程序”时,部署进展顺利。但在日志中我看到这个错误:引起:java.lang.IllegalStateException:无法加载驱动程序类:org.postgresql.Driver我的所有端点都返回错误网关 502。

这是我的应用程序属性:

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=myurl
spring.datasource.username=postgres
spring.datasource.password=mypassword
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create

我的模板.yaml:

Globals:
Function:
Timeout: 20

Resources:
EmployeeFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: EmployeeTest
Handler: com.test.LambdaHandler::handleRequest
Runtime: java8
MemorySize: 512
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
Variables:
PARAM1: VALUE
Events:
GetResource:
Type: Api
Properties:
Path: /{proxy+}
Method: any
Outputs:
EmployeeTestAPI:
Description: URL for application
Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Stage/employee'
Export:
Name: EmployeeTestAPI

还有我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>EmployeeTest</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</parent>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

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


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>boot</id>
<activation>
<property>
<name>boot</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<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>
</profiles>


</project>

此外,当我不在任何端点中使用 postgres 并从 pom.xml 中的依赖项中删除它并删除我的 application.properties 时,不会出现错误并且可以正常访问我的端点。为什么 AWS Lambda 无法获取 postgres 驱动程序?

最佳答案

我让它工作了,替换了注释掉的依赖项:

    <!--        <dependency>-->
<!-- <groupId>org.postgresql</groupId>-->
<!-- <artifactId>postgresql</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>

关于java - 将应用程序部署到 AWS Lambda 会引发 Postgres 驱动程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62054959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com