代码之家  ›  专栏  ›  技术社区  ›  user3754524

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

  •  0
  • user3754524  · 技术社区  · 4 年前

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

    以下是我的application.properties:

    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
    

    我的template.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驱动程序?

    0 回复  |  直到 4 年前
        1
  •  0
  •   user3754524    4 年前

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

        <!--        <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>