2

我创建了一个带有 Spring 本机可执行映像的 zip 文件,使用 GaalVm 编译。我想通过 Cloudwatch 事件触发该功能。因此,我从 type 创建了一个 bean,Consumer<ScheduledEvent>并将该 bean 设置为handler. 当我通过控制台触发预定事件时,我的 lambda 启动但不执行任何代码。

我的代码:

@SpringBootApplication
public class TestMarkApplication {

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

}

我的豆子:

@Component
public class MarkFunction implements Consumer<ScheduledEvent> {

 @Override
 public void accept(ScheduledEvent scheduledEvent) {
     System.out.println("Native output");
 }
}

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 https://maven.apache.org/xsd/maven- 
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.6</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test-mark</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-mark</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>11</java.version>
    <repackage.classifier/>
    <spring-cloud-function.version>3.2.1</spring-cloud-function.version>
    <spring-native.version>0.10.5</spring-native.version>
</properties>
<dependencies>

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>1.2.0</version>
    </dependency>

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-events</artifactId>
        <version>2.2.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-function-adapter-aws</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-function-webflux -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-function-webflux</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-function-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.experimental</groupId>
        <artifactId>spring-native</artifactId>
        <version>${spring-native.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-dependencies</artifactId>
            <version>${spring-cloud-function.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.experimental</groupId>
            <artifactId>spring-aot-maven-plugin</artifactId>
            <version>${spring-native.version}</version>
            <executions>
                <execution>
                    <id>test-generate</id>
                    <goals>
                        <goal>test-generate</goal>
                    </goals>
                </execution>
                <execution>
                    <id>generate</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-release</id>
        <name>Spring release</name>
        <url>https://repo.spring.io/release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-release</id>
        <name>Spring release</name>
        <url>https://repo.spring.io/release</url>
    </pluginRepository>
</pluginRepositories>

<profiles>
    <!-- Enable building a native image using a local installation of native-image with GraalVM native-image-maven-plugin -->
    <profile>
        <id>native</id>
        <properties>
            <!-- Avoid a clash between Spring Boot repackaging and native-image-maven-plugin -->
            <classifier>exec</classifier>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.graalvm.nativeimage</groupId>
                    <artifactId>native-image-maven-plugin</artifactId>
                    <version>21.1.0</version>
                    <configuration>
                        <imageName>${project.artifactId}</imageName>
                        <buildArgs>${native.build.args}</buildArgs>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>native-image</goal>
                            </goals>
                            <phase>package</phase>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <!-- packages native image and bootstrap file for uploading to AWS -->
                    <artifactId>maven-assembly-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>native-zip</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                            <inherited>false</inherited>
                        </execution>
                    </executions>
                    <configuration>
                        <descriptors>
                            <descriptor>assembly/native.xml</descriptor>
                        </descriptors>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

从 cloudwatch 记录:

o.s.nativex.NativeListener               : This application is bootstrapped with code 
generated with Spring AOT
c.example.testmark.TestMarkApplication   : Starting TestMarkApplication v0.0.1-SNAPSHOT using 
Java 11.0.12 on 169.254.51.77 with PID 9 (/var/task/test-mark started by sbx_user1051 in 
/var/task)
c.example.testmark.TestMarkApplication   : No active profile set, falling back to default 
profiles: default
o.s.c.f.web.flux.FunctionHandlerMapping  : FunctionCatalog: 
org.springframework.cloud.function.context.catalog.BeanFactoryAwareFunctionRegistry@9b65836
o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8080
c.example.testmark.TestMarkApplication   : Started TestMarkApplication in 0.257 seconds (JVM 
running for 0.394)

我期待在 Cloudwatch 中看到“本机输出”。我在这里想念什么?

4

0 回答 0