maven Java 17运行时kinesis lambda代码不工作localstack(简单代码)

kq0g1dla  于 2023-10-17  发布在  Maven
关注(0)|答案(2)|浏览(137)

这很简单。这段代码以jar和lambda的形式工作(maven项目):

package test2;

import java.util.Map;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class test implements RequestHandler<Map<String,String>, String>{

      @Override
      public String handleRequest(Map<String,String> event, Context context)
      {
        String prueba = "Prueba";
        return prueba;
      }
      public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Testing def");
      }
    }

带有依赖项的Pom.xml:

<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>test2</groupId>
  <artifactId>test2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>17</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
      <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events</artifactId>
            <version>3.11.2</version>
        </dependency>
    </dependencies>
</project>

我将它导出为一个可运行的jar,并带有它的运行配置:export; runnable jar file; runable configuration and package required libraries
然后我去localstack:
aws--endpoint-url = http://localhost:4566 lambda函数--function-name Test3--runtime java17--handler test2.test::javaRequest--role arn:aws:iam::0000000000:role/RolLambda--zip-file fileb://./test.jar
aws--endpoint-url = http://localhost:4566 lambda invoke--function-name Test3 output.json
输出:It works!
现在是不能作为lambda工作的代码(也是maven项目)。我只添加了一个KinesisClient对象:

package main;

import java.net.URI;
import java.util.Map;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.kinesis.KinesisClient;

public class Test implements RequestHandler<Map<String,String>, String>{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Testing def");
    }
    @Override
    public String handleRequest(Map<String,String> event, Context context)
    {
        String Error = "kinesis_Error";
        String prueba = "Prueba";
        
        try {
            KinesisClient kinesis_client = KinesisClient.builder().region(Region.US_EAST_1).endpointOverride(URI.create("http://127.0.0.1:4566")).build();
        } catch (Exception e) {
            return Error;
        }
        
        return prueba;
    }
}

Pom.xml:

<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>definitive_test</groupId>
  <artifactId>definitive_test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>17</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
      <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events</artifactId>
            <version>3.11.2</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>kinesis</artifactId>
            <version>2.20.135</version>
        </dependency>
        
    </dependencies>
</project>

我做同样的步骤:- 使用properly configuration set运行jar-创建lambda并执行:
aws--endpoint-url = http://localhost:4566 lambda函数--function-name Test3--runtime java17--handler main. Test::JavaScript请求--role arn:aws:iam::0000000000:role/RolLambda--zip-file fileb://./test.jar
但现在我得到了未处理的:
aws--endpoint-url = http://localhost:4566 lambda invoke--function-name Test3 output.json

{
    "StatusCode": 200,
    "FunctionError": "Unhandled",
    "ExecutedVersion": "$LATEST"
}

输出:
{"errorMessage ":"software/amazon/awssdk/services/kinesis/KinesisClient","errorType":" java 。lang。NoClassDefFoundError "," stackTrace ":["主。测试。请检查请求(Test.java:25)"," java. base/jdk.internal.reflect.NativeMethodSystemorImpl.invoke0(Native Method)"," java. base/jdk.内部的反思。NativeMethods工具实现invoke(Unknown Source)"," java. base/jdk.内部的反思。DelegatingMethods委托或实施invoke(Unknown Source)"," java. base/java. lang。反思。方法。invoke(Unknown Source)"]," cause ":{"errorMessage":"软件。亚马逊。awssdk。服务。运动KinesisClient "," errorType ":" java 。lang。ClassNotFoundException","stackTrace":[" java 。base/java. net. URLClassLoader。findClass(Unknown Source)"," java. base/java. lang。ClassLoader。loadClass(Unknown Source)"," java. base/java. lang。ClassLoader。loadClass(Unknown Source)"," main.测试。请检查请求(Test.java:25)"," java. base/jdk.internal.reflect.NativeMethodSystemorImpl.invoke0(Native Method)"," java. base/jdk.内部的反思。NativeMethods工具实现invoke(Unknown Source)"," java. base/jdk.内部的反思。DelegatingMethods委托或实施invoke(Unknown Source)"," java. base/java. lang。反思。方法。调用(未知源)"]}}
我不知道为什么会这样我已经检查了角色权限。我认为这是好的(它的arn arn:aws:iam::0000000000:role/RolLambda)

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "lambda:*",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::bucket1/*"
    },
    {
      "Effect": "Allow",
      "Action": "s3:PutObject",
      "Resource": "arn:aws:s3:::bucket1/*"
    },
    {
      "Effect": "Allow",
      "Action": [
                "kinesis:PutRecord",
                "kinesis:PutRecords",
                "kinesis:GetRecords",
                "kinesis:GetShardIterator",
                "kinesis:DescribeStream",
                "kinesis:ListStreams"
            ],
            "Resource": "*"
    }
  ]
}

有什么建议吗?谢谢你的时间

juzqafwq

juzqafwq1#

最后一个问题是jar编译。下面是一个basic pom.xml的构建,它将jar依赖项放入final jar:

<?xml version="1.0"?>

<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>Productor</groupId>
    <artifactId>Productor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <!-- Build an executable JAR with dependencies -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>productor.Productor</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- Otras dependencias aquí -->
        <!-- Dependencia para el servicio de Amazon Kinesis -->
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>kinesis</artifactId>
            <version>2.20.135</version>
        </dependency>

        <!-- Dependencia para el servicio de Amazon S3 -->
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3</artifactId>
            <version>2.20.135</version>
        </dependency>

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

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events</artifactId>
            <version>3.11.2</version>
        </dependency>
    </dependencies>
</project>

您可以在dependenciesflag中放置任何您需要的dependency。你需要在mainclassflag中输入你的包和主类(在我的例子中是productor.Productor)。属性设置java版本和编码。现在使用@smac2020命令mvn package,您可以正确地创建JavaScript文件。现在我在一个更复杂的代码中面临一个aws凭证问题,但那是另一个历史:

7nbnzgx9

7nbnzgx92#

我将测试这段代码。但是,我会做一些改变。首先,为了确保所有的依赖关系都包含在你的FAT文件中,我将使用以下命令创建一个FAT文件:

mvn包

然后,为了测试它,我将上传到Lambda AWS控制台,并使用AWS控制台直接调用Lambda函数。
顺便说一句-你的问题是这样的:

errorMessage”:“software/amazon/awssdk/services/kinesis/KinesisClient”,“errorType”:“java.lang.NoClassDefFoundError”,“stackTrace”:

找不到Kinesisclient。这意味着您没有正确地创建文件夹。

更新

正如所承诺的-这是结果。当使用mvn包构建并通过www.example.com上的AWS Lambda控制台部署时,代码可以正常工作https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/functions
Java代码。

package org.example;

import java.util.Map;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.kinesis.KinesisClient;
import software.amazon.awssdk.services.kinesis.model.ListShardsRequest;
import software.amazon.awssdk.services.kinesis.model.ListShardsResponse;

public class TestHandler implements RequestHandler<Map<String,String>, String>{

    @Override
    public String handleRequest(Map<String,String> event, Context context)
    {
        String name = "LamDataStream";
        context.getLogger().log("*** Stream Name: " + name );
        KinesisClient kinesisClient = KinesisClient.builder()
            .region(Region.US_EAST_1)
            .build();

        ListShardsRequest request = ListShardsRequest.builder()
            .streamName(name)
            .build();

        ListShardsResponse response = kinesisClient.listShards(request);
        context.getLogger().log(name + " has " + response.shards());
        return name;
    }
}

我使用AWS Lambda控制台部署了它:

我测试了它,它工作得很好:

以下是日志中的输出:

最后是我的POM(可能有一些额外的依赖项):

<?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>TestLambda</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>software.amazon.awssdk</groupId>
                <artifactId>bom</artifactId>
                <version>2.20.45</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-bom</artifactId>
                <version>2.19.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>dynamodb</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>apache-client</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3</artifactId>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk.crt</groupId>
            <artifactId>aws-crt</artifactId>
            <version>0.25.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j2-impl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events</artifactId>
            <version>3.11.1</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>textract</artifactId>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3control</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>translate</artifactId>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>comprehend</artifactId>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20230227</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>polly</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-booter</artifactId>
            <version>3.0.0-M3</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>lambda</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3-transfer-manager</artifactId>
            <version>2.20.26</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>kinesis</artifactId>
            <version>2.20.45</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

相关问题