AWS Lambda与Java Spring

e4eetjau  于 2023-03-21  发布在  Java
关注(0)|答案(3)|浏览(152)

我正在尝试使用Java Spring实现AWS Lambda服务。我已经通过了一大堆教程,这些教程告诉我一大堆方法来实现它,但我无法让它工作。
我遇到的主要问题是AWS Lambda库要求类具有零参数构造函数,这使得很难使用Spring IOC容器。我发现a tutorial有一个解决方案,并且能够在创建后将Spring bean注入到类中,但这种方法似乎不适用于从定义的接口创建的匿名类的bean示例化(例如Spring Data Repositories),这是我的应用程序使用的东西。
我找到了一个关于AWS lambda库的Spring Package 器的教程,但似乎也不起作用;我想我已经从their sample code或多或少正确地复制了他们的代码,但是当我在Lambda上运行它时,我得到了以下错误:

START RequestId: d9e62756-e72b-45f0-8df4-2164f3979dd7 Version: $LATEST
Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry in META-INF/MANIFEST.MF (in that order).: java.lang.IllegalStateException
java.lang.IllegalStateException: Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry in META-INF/MANIFEST.MF (in that order).
    at org.springframework.cloud.function.utils.FunctionClassUtils.getStartClass(FunctionClassUtils.java:83)
    at org.springframework.cloud.function.utils.FunctionClassUtils.getStartClass(FunctionClassUtils.java:60)
    at org.springframework.cloud.function.adapter.aws.FunctionInvoker.start(FunctionInvoker.java:104)
    at org.springframework.cloud.function.adapter.aws.FunctionInvoker.<init>(FunctionInvoker.java:70)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

下面是我的代码:
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">
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>JobsTest</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-adapter-aws</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>aws</shadedClassifierName>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

JobTestApplication.java (我正在使用一个示例PoC应用程序来尝试让这个该死的东西工作,然后我将一个接一个地添加更多高级功能):

package jobtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.function.Function;

@SpringBootApplication
public class JobTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(JobTestApplication.class, args);
    }

    @Bean
    public Function<Object, String> uppercase() {
        return input -> "Hello World!";
    }
}

有人能给我解释一下我做错了什么吗?我已经花了2天半的时间在这上面,我还是想不出来。谢谢。

5f0d552i

5f0d552i1#

我遇到了同样的问题。根据此链接,https://github.com/spring-cloud/spring-cloud-function/issues/439,Spring Cloud Function版本3.0.2可能会解决您的问题。

dffbzjpn

dffbzjpn2#

aws lambda update-function-configuration --function-name testFunction--environment“Variables={MAIN_CLASS=path.to.mainClass}”

omtl5h9j

omtl5h9j3#

你必须在MANIFEST.MF文件中添加主类.如果你的项目中没有这个文件夹和文件,你可以自己创建.

相关问题