我正在尝试在 AWS Lamdba 上部署我的 Spring Cloud 函数,但它看起来像我的函数 没有添加到函数目录中,所以我认为我的 bean 注册有问题,但无法弄清楚。
我的 Spring 应用程序:
@SpringBootApplication
public class TranscribeEventHandlerApplication implements ApplicationContextInitializer<GenericApplicationContext> {
public static void main(String[] args) {
FunctionalSpringApplication.run(TranscribeEventHandlerApplication.class, args);
}
public Function<String, String> handle() {
return value -> "OK";
}
@Override
public void initialize(GenericApplicationContext context) {
context.registerBean("handle", FunctionRegistration.class,
() -> new FunctionRegistration<>(handle())
.type(FunctionType.from(String.class).to(String.class).getType()));
}
}
我的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.4.7</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>ai.alabs</groupId>
<artifactId>transcribe-event-handler</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>transcribe-event-handler</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-aws</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>1.0.17.RELEASE</version>
</dependency>
</dependencies>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</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>
Lamdba 配置:
从日志中我可以看到应用程序接收到测试事件并且定义的处理程序处理它,但它无法找到我的函数。
2021-07-07 01:16:12.932 INFO 8 --- [ main] o.s.c.f.adapter.aws.FunctionInvoker : Received: {"version":"0","id":"999cccaa-eaaa-0000-1111-123456789012","detail-type":"Transcribe Job State Change","source":"aws.transcribe","account":"123456789012","time":"2016-12-16T20:57:47Z","region":"us-east-1","resources":[],"detail":{"TranscriptionJobStatus":["COMPLETED"]}}
2021-07-07 01:16:12.962 INFO 8 --- [ main] o.s.c.f.adapter.aws.AWSLambdaUtils : Incoming JSON Event: {"version":"0","id":"999cccaa-eaaa-0000-1111-123456789012","detail-type":"Transcribe Job State Change","source":"aws.transcribe","account":"123456789012","time":"2016-12-16T20:57:47Z","region":"us-east-1","resources":[],"detail":{"TranscriptionJobStatus":["COMPLETED"]}}
2021-07-07 01:16:13.129 INFO 8 --- [ main] o.s.c.f.adapter.aws.AWSLambdaUtils : Incoming request headers: {id=22800a64-d3b5-000e-3be9-48418b185b32, timestamp=1625620572948}
当我没有设置任何环境变量时,我收到此错误
Failed to establish route, since neither were provided: 'spring.cloud.function.definition' as Message header or as application property or 'spring.cloud.function.routing-expression' as application property.
当我设置环境变量
spring_cloud_function_routingExpression=handle
时,我收到此错误EL1008E: Property or field 'handle' cannot be found on object of type 'org.springframework.messaging.support.GenericMessage' - maybe not public or not valid?
。
当我设置环境变量
spring_cloud_function_definition=handle
时,我收到此错误Failed to lookup function to route based on the value of 'spring.cloud.function.definition' property 'handle'
功能组件扫描 添加 spring.cloud.function.scan.packages 可能会有所帮助。